71154ce12bb5a230f01a8d2c98399bc1ef3db4ae
[blender.git] / source / blender / render / intern / source / pipeline.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
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  * The Original Code is Copyright (C) 2006 Blender Foundation.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/render/intern/source/pipeline.c
29  *  \ingroup render
30  */
31
32 #include <math.h>
33 #include <limits.h>
34 #include <string.h>
35 #include <stdlib.h>
36 #include <stddef.h>
37
38 #include "DNA_anim_types.h"
39 #include "DNA_image_types.h"
40 #include "DNA_node_types.h"
41 #include "DNA_object_types.h"
42 #include "DNA_scene_types.h"
43 #include "DNA_sequence_types.h"
44 #include "DNA_userdef_types.h"
45
46 #include "MEM_guardedalloc.h"
47
48 #include "BLI_math.h"
49 #include "BLI_rect.h"
50 #include "BLI_listbase.h"
51 #include "BLI_string.h"
52 #include "BLI_path_util.h"
53 #include "BLI_timecode.h"
54 #include "BLI_fileops.h"
55 #include "BLI_threads.h"
56 #include "BLI_rand.h"
57 #include "BLI_callbacks.h"
58
59 #include "BLT_translation.h"
60
61 #include "BKE_animsys.h"  /* <------ should this be here?, needed for sequencer update */
62 #include "BKE_camera.h"
63 #include "BKE_colortools.h"
64 #include "BKE_depsgraph.h"
65 #include "BKE_global.h"
66 #include "BKE_image.h"
67 #include "BKE_library.h"
68 #include "BKE_main.h"
69 #include "BKE_modifier.h"
70 #include "BKE_node.h"
71 #include "BKE_pointcache.h"
72 #include "BKE_report.h"
73 #include "BKE_scene.h"
74 #include "BKE_sequencer.h"
75 #include "BKE_writeavi.h"  /* <------ should be replaced once with generic movie module */
76 #include "BKE_object.h"
77
78 #include "PIL_time.h"
79 #include "IMB_colormanagement.h"
80 #include "IMB_imbuf.h"
81 #include "IMB_imbuf_types.h"
82
83 #include "RE_engine.h"
84 #include "RE_pipeline.h"
85
86 #ifdef WITH_FREESTYLE
87 #  include "FRS_freestyle.h"
88 #endif
89
90 #include "DEG_depsgraph.h"
91
92 /* internal */
93 #include "render_result.h"
94 #include "render_types.h"
95 #include "renderpipeline.h"
96 #include "renderdatabase.h"
97 #include "rendercore.h"
98 #include "initrender.h"
99 #include "pixelblending.h"
100 #include "zbuf.h"
101
102 /* render flow
103  *
104  * 1) Initialize state
105  * - state data, tables
106  * - movie/image file init
107  * - everything that doesn't change during animation
108  *
109  * 2) Initialize data
110  * - camera, world, matrices
111  * - make render verts, faces, halos, strands
112  * - everything can change per frame/field
113  *
114  * 3) Render Processor
115  * - multiple layers
116  * - tiles, rect, baking
117  * - layers/tiles optionally to disk or directly in Render Result
118  *
119  * 4) Composite Render Result
120  * - also read external files etc
121  *
122  * 5) Image Files
123  * - save file or append in movie
124  *
125  */
126
127
128 /* ********* globals ******** */
129
130 /* here we store all renders */
131 static struct {
132         ListBase renderlist;
133 } RenderGlobal = {{NULL, NULL}}; 
134
135 /* hardcopy of current render, used while rendering for speed */
136 Render R;
137
138 /* ********* alloc and free ******** */
139
140 static int do_write_image_or_movie(Render *re, Main *bmain, Scene *scene, bMovieHandle *mh, const int totvideos, const char *name_override);
141
142 static volatile int g_break = 0;
143 static int thread_break(void *UNUSED(arg))
144 {
145         return g_break;
146 }
147
148 /* default callbacks, set in each new render */
149 static void result_nothing(void *UNUSED(arg), RenderResult *UNUSED(rr)) {}
150 static void result_rcti_nothing(void *UNUSED(arg), RenderResult *UNUSED(rr), volatile struct rcti *UNUSED(rect)) {}
151 static void current_scene_nothing(void *UNUSED(arg), Scene *UNUSED(scene)) {}
152 static void stats_nothing(void *UNUSED(arg), RenderStats *UNUSED(rs)) {}
153 static void float_nothing(void *UNUSED(arg), float UNUSED(val)) {}
154 static int default_break(void *UNUSED(arg)) { return G.is_break == true; }
155
156 static void stats_background(void *UNUSED(arg), RenderStats *rs)
157 {
158         uintptr_t mem_in_use, mmap_in_use, peak_memory;
159         float megs_used_memory, mmap_used_memory, megs_peak_memory;
160         char info_time_str[32];
161
162         mem_in_use = MEM_get_memory_in_use();
163         mmap_in_use = MEM_get_mapped_memory_in_use();
164         peak_memory = MEM_get_peak_memory();
165
166         megs_used_memory = (mem_in_use - mmap_in_use) / (1024.0 * 1024.0);
167         mmap_used_memory = (mmap_in_use) / (1024.0 * 1024.0);
168         megs_peak_memory = (peak_memory) / (1024.0 * 1024.0);
169
170         fprintf(stdout, IFACE_("Fra:%d Mem:%.2fM (%.2fM, Peak %.2fM) "), rs->cfra,
171                 megs_used_memory, mmap_used_memory, megs_peak_memory);
172
173         if (rs->curfield)
174                 fprintf(stdout, IFACE_("Field %d "), rs->curfield);
175         if (rs->curblur)
176                 fprintf(stdout, IFACE_("Blur %d "), rs->curblur);
177
178         BLI_timecode_string_from_time_simple(info_time_str, sizeof(info_time_str), PIL_check_seconds_timer() - rs->starttime);
179         fprintf(stdout, IFACE_("| Time:%s | "), info_time_str);
180
181         if (rs->infostr) {
182                 fprintf(stdout, "%s", rs->infostr);
183         }
184         else {
185                 if (rs->tothalo)
186                         fprintf(stdout, IFACE_("Sce: %s Ve:%d Fa:%d Ha:%d La:%d"),
187                                 rs->scene_name, rs->totvert, rs->totface, rs->tothalo, rs->totlamp);
188                 else
189                         fprintf(stdout, IFACE_("Sce: %s Ve:%d Fa:%d La:%d"), rs->scene_name, rs->totvert, rs->totface, rs->totlamp);
190         }
191
192         /* Flush stdout to be sure python callbacks are printing stuff after blender. */
193         fflush(stdout);
194
195         BLI_callback_exec(G.main, NULL, BLI_CB_EVT_RENDER_STATS);
196
197         fputc('\n', stdout);
198         fflush(stdout);
199 }
200
201 void RE_FreeRenderResult(RenderResult *res)
202 {
203         render_result_free(res);
204 }
205
206 float *RE_RenderLayerGetPass(volatile RenderLayer *rl, int passtype, const char *viewname)
207 {
208         RenderPass *rpass = RE_pass_find_by_type(rl, passtype, viewname);
209         return rpass ? rpass->rect : NULL;
210 }
211
212 RenderLayer *RE_GetRenderLayer(RenderResult *rr, const char *name)
213 {
214         if (rr == NULL) {
215                 return NULL;
216         }
217         else {
218                 return BLI_findstring(&rr->layers, name, offsetof(RenderLayer, name));
219         }
220 }
221
222 RenderResult *RE_MultilayerConvert(void *exrhandle, const char *colorspace, bool predivide, int rectx, int recty)
223 {
224         return render_result_new_from_exr(exrhandle, colorspace, predivide, rectx, recty);
225 }
226
227 RenderLayer *render_get_active_layer(Render *re, RenderResult *rr)
228 {
229         RenderLayer *rl = BLI_findlink(&rr->layers, re->r.actlay);
230         
231         if (rl)
232                 return rl;
233         else 
234                 return rr->layers.first;
235 }
236
237 static int render_scene_needs_vector(Render *re)
238 {
239         SceneRenderLayer *srl;
240         
241         for (srl = re->r.layers.first; srl; srl = srl->next)
242                 if (!(srl->layflag & SCE_LAY_DISABLE))
243                         if (srl->passflag & SCE_PASS_VECTOR)
244                                 return 1;
245
246         return 0;
247 }
248
249 static bool render_scene_has_layers_to_render(Scene *scene)
250 {
251         SceneRenderLayer *srl;
252         for (srl = scene->r.layers.first; srl; srl = srl->next) {
253                 if (!(srl->layflag & SCE_LAY_DISABLE)) {
254                         return true;
255                 }
256         }
257         return false;
258 }
259
260 /* *************************************************** */
261
262 Render *RE_GetRender(const char *name)
263 {
264         Render *re;
265
266         /* search for existing renders */
267         for (re = RenderGlobal.renderlist.first; re; re = re->next)
268                 if (STREQLEN(re->name, name, RE_MAXNAME))
269                         break;
270
271         return re;
272 }
273
274
275 /* if you want to know exactly what has been done */
276 RenderResult *RE_AcquireResultRead(Render *re)
277 {
278         if (re) {
279                 BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_READ);
280                 return re->result;
281         }
282
283         return NULL;
284 }
285
286 RenderResult *RE_AcquireResultWrite(Render *re)
287 {
288         if (re) {
289                 BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
290                 return re->result;
291         }
292
293         return NULL;
294 }
295
296 void RE_SwapResult(Render *re, RenderResult **rr)
297 {
298         /* for keeping render buffers */
299         if (re) {
300                 SWAP(RenderResult *, re->result, *rr);
301         }
302 }
303
304
305 void RE_ReleaseResult(Render *re)
306 {
307         if (re)
308                 BLI_rw_mutex_unlock(&re->resultmutex);
309 }
310
311 /* displist.c util.... */
312 Scene *RE_GetScene(Render *re)
313 {
314         if (re)
315                 return re->scene;
316         return NULL;
317 }
318
319 /**
320  * Same as #RE_AcquireResultImage but creating the necessary views to store the result
321  * fill provided result struct with a copy of thew views of what is done so far the
322  * #RenderResult.views #ListBase needs to be freed after with #RE_ReleaseResultImageViews
323 */
324 void RE_AcquireResultImageViews(Render *re, RenderResult *rr)
325 {
326         memset(rr, 0, sizeof(RenderResult));
327
328         if (re) {
329                 BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_READ);
330
331                 if (re->result) {
332                         RenderLayer *rl;
333                         RenderView *rv, *rview;
334
335                         rr->rectx = re->result->rectx;
336                         rr->recty = re->result->recty;
337
338                         /* creates a temporary duplication of views */
339                         render_result_views_shallowcopy(rr, re->result);
340
341                         rv = rr->views.first;
342
343                         /* active layer */
344                         rl = render_get_active_layer(re, re->result);
345
346                         if (rl) {
347                                 if (rv->rectf == NULL) {
348                                         for (rview = (RenderView *)rr->views.first; rview; rview = rview->next) {
349                                                 rview->rectf = RE_RenderLayerGetPass(rl, SCE_PASS_COMBINED, rview->name);
350                                         }
351                                 }
352
353                                 if (rv->rectz == NULL) {
354                                         for (rview = (RenderView *)rr->views.first; rview; rview = rview->next) {
355                                                 rview->rectz = RE_RenderLayerGetPass(rl, SCE_PASS_Z, rview->name);
356                                         }
357                                 }
358                         }
359
360                         rr->have_combined = (rv->rectf != NULL);
361                         rr->layers = re->result->layers;
362                         rr->xof = re->disprect.xmin;
363                         rr->yof = re->disprect.ymin;
364                         rr->stamp_data = re->result->stamp_data;
365                 }
366         }
367 }
368
369 /* clear temporary renderresult struct */
370 void RE_ReleaseResultImageViews(Render *re, RenderResult *rr)
371 {
372         if (re) {
373                 if (rr) {
374                         render_result_views_shallowdelete(rr);
375                 }
376                 BLI_rw_mutex_unlock(&re->resultmutex);
377         }
378 }
379
380 /* fill provided result struct with what's currently active or done */
381 /* this RenderResult struct is the only exception to the rule of a RenderResult */
382 /* always having at least one RenderView */
383 void RE_AcquireResultImage(Render *re, RenderResult *rr, const int view_id)
384 {
385         memset(rr, 0, sizeof(RenderResult));
386
387         if (re) {
388                 BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_READ);
389
390                 if (re->result) {
391                         RenderLayer *rl;
392                         RenderView *rv;
393                         
394                         rr->rectx = re->result->rectx;
395                         rr->recty = re->result->recty;
396                         
397                         /* actview view */
398                         rv = RE_RenderViewGetById(re->result, view_id);
399
400                         rr->rectf = rv->rectf;
401                         rr->rectz = rv->rectz;
402                         rr->rect32 = rv->rect32;
403
404                         /* active layer */
405                         rl = render_get_active_layer(re, re->result);
406
407                         if (rl) {
408                                 if (rv->rectf == NULL)
409                                         rr->rectf = RE_RenderLayerGetPass(rl, SCE_PASS_COMBINED, rv->name);
410
411                                 if (rv->rectz == NULL)
412                                         rr->rectz = RE_RenderLayerGetPass(rl, SCE_PASS_Z, rv->name);
413                         }
414
415                         rr->have_combined = (rv->rectf != NULL);
416                         rr->layers = re->result->layers;
417                         rr->views = re->result->views;
418
419                         rr->xof = re->disprect.xmin;
420                         rr->yof = re->disprect.ymin;
421                 }
422         }
423 }
424
425 void RE_ReleaseResultImage(Render *re)
426 {
427         if (re)
428                 BLI_rw_mutex_unlock(&re->resultmutex);
429 }
430
431 /* caller is responsible for allocating rect in correct size! */
432 void RE_ResultGet32(Render *re, unsigned int *rect)
433 {
434         RenderResult rres;
435         const int view_id = BKE_scene_multiview_view_id_get(&re->r, re->viewname);
436
437         RE_AcquireResultImageViews(re, &rres);
438         render_result_rect_get_pixels(&rres, rect, re->rectx, re->recty, &re->scene->view_settings, &re->scene->display_settings, view_id);
439         RE_ReleaseResultImageViews(re, &rres);
440 }
441
442 /* caller is responsible for allocating rect in correct size! */
443 /* Only for acquired results, for lock */
444 void RE_AcquiredResultGet32(Render *re, RenderResult *result, unsigned int *rect, const int view_id)
445 {
446         render_result_rect_get_pixels(result, rect, re->rectx, re->recty, &re->scene->view_settings, &re->scene->display_settings, view_id);
447 }
448
449 RenderStats *RE_GetStats(Render *re)
450 {
451         return &re->i;
452 }
453
454 Render *RE_NewRender(const char *name)
455 {
456         Render *re;
457
458         /* only one render per name exists */
459         re = RE_GetRender(name);
460         if (re == NULL) {
461                 
462                 /* new render data struct */
463                 re = MEM_callocN(sizeof(Render), "new render");
464                 BLI_addtail(&RenderGlobal.renderlist, re);
465                 BLI_strncpy(re->name, name, RE_MAXNAME);
466                 BLI_rw_mutex_init(&re->resultmutex);
467                 BLI_rw_mutex_init(&re->partsmutex);
468                 re->eval_ctx = DEG_evaluation_context_new(DAG_EVAL_RENDER);
469         }
470         
471         RE_InitRenderCB(re);
472
473         /* init some variables */
474         re->ycor = 1.0f;
475         
476         return re;
477 }
478
479 /* called for new renders and when finishing rendering so
480  * we always have valid callbacks on a render */
481 void RE_InitRenderCB(Render *re)
482 {
483         /* set default empty callbacks */
484         re->display_init = result_nothing;
485         re->display_clear = result_nothing;
486         re->display_update = result_rcti_nothing;
487         re->current_scene_update = current_scene_nothing;
488         re->progress = float_nothing;
489         re->test_break = default_break;
490         if (G.background)
491                 re->stats_draw = stats_background;
492         else
493                 re->stats_draw = stats_nothing;
494         /* clear callback handles */
495         re->dih = re->dch = re->duh = re->sdh = re->prh = re->tbh = NULL;
496 }
497
498 /* only call this while you know it will remove the link too */
499 void RE_FreeRender(Render *re)
500 {
501         if (re->engine)
502                 RE_engine_free(re->engine);
503
504         BLI_rw_mutex_end(&re->resultmutex);
505         BLI_rw_mutex_end(&re->partsmutex);
506
507         BLI_freelistN(&re->r.layers);
508         BLI_freelistN(&re->r.views);
509
510         curvemapping_free_data(&re->r.mblur_shutter_curve);
511
512         /* main dbase can already be invalid now, some database-free code checks it */
513         re->main = NULL;
514         re->scene = NULL;
515         
516         RE_Database_Free(re);   /* view render can still have full database */
517         free_sample_tables(re);
518         
519         render_result_free(re->result);
520         render_result_free(re->pushedresult);
521         
522         BLI_remlink(&RenderGlobal.renderlist, re);
523         MEM_freeN(re->eval_ctx);
524         MEM_freeN(re);
525 }
526
527 /* exit blender */
528 void RE_FreeAllRender(void)
529 {
530         while (RenderGlobal.renderlist.first) {
531                 RE_FreeRender(RenderGlobal.renderlist.first);
532         }
533
534 #ifdef WITH_FREESTYLE
535         /* finalize Freestyle */
536         FRS_exit();
537 #endif
538 }
539
540 void RE_FreeAllPersistentData(void)
541 {
542         Render *re;
543         for (re = RenderGlobal.renderlist.first; re != NULL; re = re->next) {
544                 if ((re->r.mode & R_PERSISTENT_DATA) != 0 && re->engine != NULL) {
545                         RE_engine_free(re->engine);
546                         re->engine = NULL;
547                 }
548         }
549 }
550
551 /* on file load, free all re */
552 void RE_FreeAllRenderResults(void)
553 {
554         Render *re;
555
556         for (re = RenderGlobal.renderlist.first; re; re = re->next) {
557                 render_result_free(re->result);
558                 render_result_free(re->pushedresult);
559
560                 re->result = NULL;
561                 re->pushedresult = NULL;
562         }
563 }
564
565 void RE_FreePersistentData(void)
566 {
567         Render *re;
568
569         /* render engines can be kept around for quick re-render, this clears all */
570         for (re = RenderGlobal.renderlist.first; re; re = re->next) {
571                 if (re->engine) {
572                         /* if engine is currently rendering, just tag it to be freed when render is finished */
573                         if (!(re->engine->flag & RE_ENGINE_RENDERING))
574                                 RE_engine_free(re->engine);
575
576                         re->engine = NULL;
577                 }
578         }
579 }
580
581 /* ********* initialize state ******** */
582
583 /* clear full sample and tile flags if needed */
584 static int check_mode_full_sample(RenderData *rd)
585 {
586         int scemode = rd->scemode;
587
588         if (!STREQ(rd->engine, RE_engine_id_BLENDER_RENDER) &&
589             !STREQ(rd->engine, RE_engine_id_BLENDER_GAME))
590         {
591                 scemode &= ~R_FULL_SAMPLE;
592         }
593
594         if ((rd->mode & R_OSA) == 0)
595                 scemode &= ~R_FULL_SAMPLE;
596
597 #ifdef WITH_OPENEXR
598         if (scemode & R_FULL_SAMPLE)
599                 scemode |= R_EXR_TILE_FILE;   /* enable automatic */
600
601         /* Until use_border is made compatible with save_buffers/full_sample, render without the later instead of not rendering at all.*/
602         if (rd->mode & R_BORDER) {
603                 scemode &= ~(R_EXR_TILE_FILE | R_FULL_SAMPLE);
604         }
605
606 #else
607         /* can't do this without openexr support */
608         scemode &= ~(R_EXR_TILE_FILE | R_FULL_SAMPLE);
609 #endif
610
611         return scemode;
612 }
613
614 static void re_init_resolution(Render *re, Render *source,
615                                int winx, int winy, rcti *disprect)
616 {
617         re->winx = winx;
618         re->winy = winy;
619         if (source && (source->r.mode & R_BORDER)) {
620                 /* eeh, doesn't seem original bordered disprect is storing anywhere
621                  * after insertion on black happening in do_render_fields_blur_3d(),
622                  * so for now simply re-calculate disprect using border from source
623                  * renderer (sergey)
624                  */
625
626                 re->disprect.xmin = source->r.border.xmin * winx;
627                 re->disprect.xmax = source->r.border.xmax * winx;
628
629                 re->disprect.ymin = source->r.border.ymin * winy;
630                 re->disprect.ymax = source->r.border.ymax * winy;
631
632                 re->rectx = BLI_rcti_size_x(&re->disprect);
633                 re->recty = BLI_rcti_size_y(&re->disprect);
634
635                 /* copy border itself, since it could be used by external engines */
636                 re->r.border = source->r.border;
637         }
638         else if (disprect) {
639                 re->disprect = *disprect;
640                 re->rectx = BLI_rcti_size_x(&re->disprect);
641                 re->recty = BLI_rcti_size_y(&re->disprect);
642         }
643         else {
644                 re->disprect.xmin = re->disprect.ymin = 0;
645                 re->disprect.xmax = winx;
646                 re->disprect.ymax = winy;
647                 re->rectx = winx;
648                 re->recty = winy;
649         }
650
651         /* we clip faces with a minimum of 2 pixel boundary outside of image border. see zbuf.c */
652         re->clipcrop = 1.0f + 2.0f / (float)(re->winx > re->winy ? re->winy : re->winx);
653 }
654
655 /* what doesn't change during entire render sequence */
656 /* disprect is optional, if NULL it assumes full window render */
657 void RE_InitState(Render *re, Render *source, RenderData *rd,
658                   SceneRenderLayer *srl,
659                   int winx, int winy, rcti *disprect)
660 {
661         bool had_freestyle = (re->r.mode & R_EDGE_FRS) != 0;
662
663         re->ok = true;   /* maybe flag */
664         
665         re->i.starttime = PIL_check_seconds_timer();
666
667         /* copy render data and render layers for thread safety */
668         BLI_freelistN(&re->r.layers);
669         BLI_freelistN(&re->r.views);
670         curvemapping_free_data(&re->r.mblur_shutter_curve);
671         re->r = *rd;
672         BLI_duplicatelist(&re->r.layers, &rd->layers);
673         BLI_duplicatelist(&re->r.views, &rd->views);
674         curvemapping_copy_data(&re->r.mblur_shutter_curve, &rd->mblur_shutter_curve);
675
676         if (source) {
677                 /* reuse border flags from source renderer */
678                 re->r.mode &= ~(R_BORDER | R_CROP);
679                 re->r.mode |= source->r.mode & (R_BORDER | R_CROP);
680
681                 /* dimensions shall be shared between all renderers */
682                 re->r.xsch = source->r.xsch;
683                 re->r.ysch = source->r.ysch;
684                 re->r.size = source->r.size;
685         }
686
687         re_init_resolution(re, source, winx, winy, disprect);
688
689         if (re->rectx < 1 || re->recty < 1 || (BKE_imtype_is_movie(rd->im_format.imtype) &&
690                                                (re->rectx < 16 || re->recty < 16) ))
691         {
692                 BKE_report(re->reports, RPT_ERROR, "Image too small");
693                 re->ok = 0;
694                 return;
695         }
696
697         re->r.scemode = check_mode_full_sample(&re->r);
698         
699         /* fullsample wants uniform osa levels */
700         if (source && (re->r.scemode & R_FULL_SAMPLE)) {
701                 /* but, if source has no full sample we disable it */
702                 if ((source->r.scemode & R_FULL_SAMPLE) == 0)
703                         re->r.scemode &= ~R_FULL_SAMPLE;
704                 else
705                         re->r.osa = re->osa = source->osa;
706         }
707         else {
708                 /* check state variables, osa? */
709                 if (re->r.mode & (R_OSA)) {
710                         re->osa = re->r.osa;
711                         if (re->osa > 16) re->osa = 16;
712                 }
713                 else re->osa = 0;
714         }
715         
716         if (srl) {
717                 int index = BLI_findindex(&rd->layers, srl);
718                 if (index != -1) {
719                         re->r.actlay = index;
720                         re->r.scemode |= R_SINGLE_LAYER;
721                 }
722         }
723                 
724         /* always call, checks for gamma, gamma tables and jitter too */
725         make_sample_tables(re);
726         
727         /* if preview render, we try to keep old result */
728         BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
729
730         if (re->r.scemode & (R_BUTS_PREVIEW|R_VIEWPORT_PREVIEW)) {
731                 if (had_freestyle || (re->r.mode & R_EDGE_FRS)) {
732                         /* freestyle manipulates render layers so always have to free */
733                         render_result_free(re->result);
734                         re->result = NULL;
735                 }
736                 else if (re->result) {
737                         SceneRenderLayer *actsrl = BLI_findlink(&re->r.layers, re->r.actlay);
738                         RenderLayer *rl;
739                         bool have_layer = false;
740
741                         for (rl = re->result->layers.first; rl; rl = rl->next)
742                                 if (STREQ(rl->name, actsrl->name))
743                                         have_layer = true;
744
745                         if (re->result->rectx == re->rectx && re->result->recty == re->recty &&
746                             have_layer)
747                         {
748                                 /* keep render result, this avoids flickering black tiles
749                                  * when the preview changes */
750                         }
751                         else {
752                                 /* free because resolution changed */
753                                 render_result_free(re->result);
754                                 re->result = NULL;
755                         }
756                 }
757         }
758         else {
759                 
760                 /* make empty render result, so display callbacks can initialize */
761                 render_result_free(re->result);
762                 re->result = MEM_callocN(sizeof(RenderResult), "new render result");
763                 re->result->rectx = re->rectx;
764                 re->result->recty = re->recty;
765                 render_result_view_new(re->result, "new temporary view");
766         }
767         
768         if (re->r.scemode & R_VIEWPORT_PREVIEW)
769                 re->eval_ctx->mode = DAG_EVAL_PREVIEW;
770         else
771                 re->eval_ctx->mode = DAG_EVAL_RENDER;
772         
773         /* ensure renderdatabase can use part settings correct */
774         RE_parts_clamp(re);
775
776         BLI_rw_mutex_unlock(&re->resultmutex);
777         
778         re->mblur_offs = re->field_offs = 0.f;
779         
780         RE_init_threadcount(re);
781 }
782
783 /* This function is only called by view3d rendering, which doesn't support
784  * multiview at the moment. so handle only one view here */
785 static void render_result_rescale(Render *re)
786 {
787         RenderResult *result = re->result;
788         RenderView *rv;
789         int x, y;
790         float scale_x, scale_y;
791         float *src_rectf;
792
793         rv = RE_RenderViewGetById(result, 0);
794         src_rectf = rv->rectf;
795
796         if (src_rectf == NULL) {
797                 RenderLayer *rl = render_get_active_layer(re, re->result);
798                 if (rl != NULL) {
799                         src_rectf = RE_RenderLayerGetPass(rl, SCE_PASS_COMBINED, NULL);
800                 }
801         }
802
803         if (src_rectf != NULL) {
804                 float *dst_rectf = NULL;
805                 re->result = render_result_new(re,
806                                                &re->disprect,
807                                                0,
808                                                RR_USE_MEM,
809                                                RR_ALL_LAYERS,
810                                                "");
811
812                 if (re->result != NULL) {
813                         dst_rectf = RE_RenderViewGetById(re->result, 0)->rectf;
814                         if (dst_rectf == NULL) {
815                                 RenderLayer *rl;
816                                 rl = render_get_active_layer(re, re->result);
817                                 if (rl != NULL) {
818                                         dst_rectf = RE_RenderLayerGetPass(rl, SCE_PASS_COMBINED, NULL);
819                                 }
820                         }
821
822                         scale_x = (float) result->rectx / re->result->rectx;
823                         scale_y = (float) result->recty / re->result->recty;
824                         for (x = 0; x < re->result->rectx; ++x) {
825                                 for (y = 0; y < re->result->recty; ++y) {
826                                         int src_x = x * scale_x;
827                                         int src_y = y * scale_y;
828                                         int dst_index = y * re->result->rectx + x;
829                                         int src_index = src_y * result->rectx + src_x;
830                                         copy_v4_v4(dst_rectf + dst_index * 4,
831                                                    src_rectf + src_index * 4);
832                                 }
833                         }
834                 }
835                 render_result_free(result);
836         }
837 }
838
839 void RE_ChangeResolution(Render *re, int winx, int winy, rcti *disprect)
840 {
841         re_init_resolution(re, NULL, winx, winy, disprect);
842         RE_parts_clamp(re);
843
844         if (re->result) {
845                 BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
846                 render_result_rescale(re);
847                 BLI_rw_mutex_unlock(&re->resultmutex);
848         }
849 }
850
851 /* TODO(sergey): This is a bit hackish, used to temporary disable freestyle when
852  * doing viewport render. Needs some better integration of BI viewport rendering
853  * into the pipeline.
854  */
855 void RE_ChangeModeFlag(Render *re, int flag, bool clear)
856 {
857         if (clear) {
858                 re->r.mode &= ~flag;
859         }
860         else {
861                 re->r.mode |= flag;
862         }
863 }
864
865 /* update some variables that can be animated, and otherwise wouldn't be due to
866  * RenderData getting copied once at the start of animation render */
867 void render_update_anim_renderdata(Render *re, RenderData *rd)
868 {
869         /* filter */
870         re->r.gauss = rd->gauss;
871
872         /* motion blur */
873         re->r.mblur_samples = rd->mblur_samples;
874         re->r.blurfac = rd->blurfac;
875
876         /* freestyle */
877         re->r.line_thickness_mode = rd->line_thickness_mode;
878         re->r.unit_line_thickness = rd->unit_line_thickness;
879
880         /* render layers */
881         BLI_freelistN(&re->r.layers);
882         BLI_duplicatelist(&re->r.layers, &rd->layers);
883
884         /* render views */
885         BLI_freelistN(&re->r.views);
886         BLI_duplicatelist(&re->r.views, &rd->views);
887 }
888
889 void RE_SetWindow(Render *re, rctf *viewplane, float clipsta, float clipend)
890 {
891         /* re->ok flag? */
892         
893         re->viewplane = *viewplane;
894         re->clipsta = clipsta;
895         re->clipend = clipend;
896         re->r.mode &= ~R_ORTHO;
897
898         perspective_m4(re->winmat,
899                        re->viewplane.xmin, re->viewplane.xmax,
900                        re->viewplane.ymin, re->viewplane.ymax, re->clipsta, re->clipend);
901         
902 }
903
904 void RE_SetOrtho(Render *re, rctf *viewplane, float clipsta, float clipend)
905 {
906         /* re->ok flag? */
907         
908         re->viewplane = *viewplane;
909         re->clipsta = clipsta;
910         re->clipend = clipend;
911         re->r.mode |= R_ORTHO;
912
913         orthographic_m4(re->winmat,
914                         re->viewplane.xmin, re->viewplane.xmax,
915                         re->viewplane.ymin, re->viewplane.ymax, re->clipsta, re->clipend);
916 }
917
918 void RE_SetView(Render *re, float mat[4][4])
919 {
920         /* re->ok flag? */
921         copy_m4_m4(re->viewmat, mat);
922         invert_m4_m4(re->viewinv, re->viewmat);
923 }
924
925 void RE_GetViewPlane(Render *re, rctf *viewplane, rcti *disprect)
926 {
927         *viewplane = re->viewplane;
928         
929         /* make disprect zero when no border render, is needed to detect changes in 3d view render */
930         if (re->r.mode & R_BORDER)
931                 *disprect = re->disprect;
932         else
933                 BLI_rcti_init(disprect, 0, 0, 0, 0);
934 }
935
936 void RE_GetView(Render *re, float mat[4][4])
937 {
938         copy_m4_m4(mat, re->viewmat);
939 }
940
941 /* image and movie output has to move to either imbuf or kernel */
942 void RE_display_init_cb(Render *re, void *handle, void (*f)(void *handle, RenderResult *rr))
943 {
944         re->display_init = f;
945         re->dih = handle;
946 }
947 void RE_display_clear_cb(Render *re, void *handle, void (*f)(void *handle, RenderResult *rr))
948 {
949         re->display_clear = f;
950         re->dch = handle;
951 }
952 void RE_display_update_cb(Render *re, void *handle, void (*f)(void *handle, RenderResult *rr, volatile rcti *rect))
953 {
954         re->display_update = f;
955         re->duh = handle;
956 }
957 void RE_current_scene_update_cb(Render *re, void *handle, void (*f)(void *handle, Scene *scene))
958 {
959         re->current_scene_update = f;
960         re->suh = handle;
961 }
962 void RE_stats_draw_cb(Render *re, void *handle, void (*f)(void *handle, RenderStats *rs))
963 {
964         re->stats_draw = f;
965         re->sdh = handle;
966 }
967 void RE_progress_cb(Render *re, void *handle, void (*f)(void *handle, float))
968 {
969         re->progress = f;
970         re->prh = handle;
971 }
972
973 void RE_draw_lock_cb(Render *re, void *handle, void (*f)(void *handle, int i))
974 {
975         re->draw_lock = f;
976         re->dlh = handle;
977 }
978
979 void RE_test_break_cb(Render *re, void *handle, int (*f)(void *handle))
980 {
981         re->test_break = f;
982         re->tbh = handle;
983 }
984
985
986 /* ********* add object data (later) ******** */
987
988 /* object is considered fully prepared on correct time etc */
989 /* includes lights */
990 #if 0
991 void RE_AddObject(Render *UNUSED(re), Object *UNUSED(ob))
992 {
993         
994 }
995 #endif
996
997 /* *************************************** */
998
999 static int render_display_update_enabled(Render *re)
1000 {
1001         /* don't show preprocess for previewrender sss */
1002         if (re->sss_points)
1003                 return !(re->r.scemode & (R_BUTS_PREVIEW|R_VIEWPORT_PREVIEW));
1004         else
1005                 return 1;
1006 }
1007
1008 /* the main thread call, renders an entire part */
1009 static void *do_part_thread(void *pa_v)
1010 {
1011         RenderPart *pa = pa_v;
1012
1013         pa->status = PART_STATUS_IN_PROGRESS;
1014
1015         /* need to return nicely all parts on esc */
1016         if (R.test_break(R.tbh) == 0) {
1017                 
1018                 if (!R.sss_points && (R.r.scemode & R_FULL_SAMPLE))
1019                         pa->result = render_result_new_full_sample(&R, &pa->fullresult, &pa->disprect, pa->crop, RR_USE_MEM, R.viewname);
1020                 else
1021                         pa->result = render_result_new(&R, &pa->disprect, pa->crop, RR_USE_MEM, RR_ALL_LAYERS, R.viewname);
1022
1023                 /* Copy EXR tile settings, so pipeline knows whether this is a result
1024                  * for Save Buffers enabled rendering.
1025                  *
1026                  * TODO(sergey): This actually duplicates logic with external engine, so
1027                  * worth looking into more generic solution.
1028                  */
1029                 pa->result->do_exr_tile = R.result->do_exr_tile;
1030
1031                 if (R.sss_points)
1032                         zbufshade_sss_tile(pa);
1033                 else if (R.osa)
1034                         zbufshadeDA_tile(pa);
1035                 else
1036                         zbufshade_tile(pa);
1037                 
1038                 /* we do actually write pixels, but don't allocate/deallocate anything,
1039                  * so it is safe with other threads reading at the same time */
1040                 BLI_rw_mutex_lock(&R.resultmutex, THREAD_LOCK_READ);
1041                 
1042                 /* merge too on break! */
1043                 if (R.result->do_exr_tile) {
1044                         render_result_exr_file_merge(R.result, pa->result, R.viewname);
1045                 }
1046                 else if (render_display_update_enabled(&R)) {
1047                         /* on break, don't merge in result for preview renders, looks nicer */
1048                         if (R.test_break(R.tbh) && (R.r.scemode & (R_BUTS_PREVIEW|R_VIEWPORT_PREVIEW))) {
1049                                 /* pass */
1050                         }
1051                         else {
1052                                 render_result_merge(R.result, pa->result);
1053                         }
1054                 }
1055                 
1056                 BLI_rw_mutex_unlock(&R.resultmutex);
1057         }
1058         
1059         pa->status = PART_STATUS_READY;
1060         
1061         return NULL;
1062 }
1063
1064 /* calculus for how much 1 pixel rendered should rotate the 3d geometry */
1065 /* is not that simple, needs to be corrected for errors of larger viewplane sizes */
1066 /* called in initrender.c, RE_parts_init() and convertblender.c, for speedvectors */
1067 float panorama_pixel_rot(Render *re)
1068 {
1069         float psize, phi, xfac;
1070         float borderfac = (float)BLI_rcti_size_x(&re->disprect) / (float)re->winx;
1071         int xparts = (re->rectx + re->partx - 1) / re->partx;
1072         
1073         /* size of 1 pixel mapped to viewplane coords */
1074         psize = BLI_rctf_size_x(&re->viewplane) / (float)re->winx;
1075         /* angle of a pixel */
1076         phi = atan(psize / re->clipsta);
1077         
1078         /* correction factor for viewplane shifting, first calculate how much the viewplane angle is */
1079         xfac = borderfac * BLI_rctf_size_x(&re->viewplane) / (float)xparts;
1080         xfac = atan(0.5f * xfac / re->clipsta);
1081         /* and how much the same viewplane angle is wrapped */
1082         psize = 0.5f * phi * ((float)re->partx);
1083         
1084         /* the ratio applied to final per-pixel angle */
1085         phi *= xfac / psize;
1086         
1087         return phi;
1088 }
1089
1090 /* for panorama, we render per Y slice, and update
1091  * camera parameters when we go the next slice */
1092 static bool find_next_pano_slice(Render *re, int *slice, int *minx, rctf *viewplane)
1093 {
1094         RenderPart *pa, *best = NULL;
1095         bool found = false;
1096         
1097         *minx = re->winx;
1098         
1099         if (!(re->r.mode & R_PANORAMA)) {
1100                 /* for regular render, just one 'slice' */
1101                 found = (*slice == 0);
1102                 (*slice)++;
1103                 return found;
1104         }
1105
1106         /* most left part of the non-rendering parts */
1107         for (pa = re->parts.first; pa; pa = pa->next) {
1108                 if (pa->status == PART_STATUS_NONE && pa->nr == 0) {
1109                         if (pa->disprect.xmin < *minx) {
1110                                 found = true;
1111                                 best = pa;
1112                                 *minx = pa->disprect.xmin;
1113                         }
1114                 }
1115         }
1116         
1117         if (best) {
1118                 float phi = panorama_pixel_rot(re);
1119
1120                 R.panodxp = (re->winx - (best->disprect.xmin + best->disprect.xmax) ) / 2;
1121                 R.panodxv = (BLI_rctf_size_x(viewplane) * R.panodxp) / (float)(re->winx);
1122
1123                 /* shift viewplane */
1124                 R.viewplane.xmin = viewplane->xmin + R.panodxv;
1125                 R.viewplane.xmax = viewplane->xmax + R.panodxv;
1126                 RE_SetWindow(re, &R.viewplane, R.clipsta, R.clipend);
1127                 copy_m4_m4(R.winmat, re->winmat);
1128                 
1129                 /* rotate database according to part coordinates */
1130                 project_renderdata(re, projectverto, 1, -R.panodxp * phi, 1);
1131                 R.panosi = sinf(R.panodxp * phi);
1132                 R.panoco = cosf(R.panodxp * phi);
1133         }
1134         
1135         (*slice)++;
1136         
1137         return found;
1138 }
1139
1140 typedef struct SortRenderPart {
1141         RenderPart *pa;
1142         long long int dist;
1143 } SortRenderPart;
1144
1145 static int sort_render_part(const void *pa1, const void *pa2) {
1146         const SortRenderPart *rpa1 = pa1;
1147         const SortRenderPart *rpa2 = pa2;
1148
1149         if (rpa1->dist > rpa2->dist) return 1;
1150         else if (rpa1->dist < rpa2->dist) return -1;
1151
1152         return 0;
1153 }
1154
1155 static int sort_and_queue_parts(Render *re, int minx, ThreadQueue *workqueue)
1156 {
1157         RenderPart *pa;
1158
1159         /* long long int's needed because of overflow [#24414] */
1160         long long int centx = re->winx / 2, centy = re->winy / 2, tot = 1;
1161         int totsort = 0;
1162         
1163         /* find center of rendered parts, image center counts for 1 too */
1164         for (pa = re->parts.first; pa; pa = pa->next) {
1165                 if (pa->status == PART_STATUS_READY) {
1166                         centx += BLI_rcti_cent_x(&pa->disprect);
1167                         centy += BLI_rcti_cent_y(&pa->disprect);
1168                         tot++;
1169                 }
1170                 else if (pa->status == PART_STATUS_NONE && pa->nr == 0) {
1171                         if (!(re->r.mode & R_PANORAMA) || pa->disprect.xmin == minx) {
1172                                 totsort++;
1173                         }
1174                 }
1175         }
1176         centx /= tot;
1177         centy /= tot;
1178         
1179         if (totsort > 0) {
1180                 SortRenderPart *sortlist = MEM_mallocN(sizeof(*sortlist) * totsort, "renderpartsort");
1181                 long int i = 0;
1182
1183                 /* prepare the list */
1184                 for (pa = re->parts.first; pa; pa = pa->next) {
1185                         if (pa->status == PART_STATUS_NONE && pa->nr == 0) {
1186                                 if (!(re->r.mode & R_PANORAMA) || pa->disprect.xmin == minx) {
1187                                         long long int distx = centx - BLI_rcti_cent_x(&pa->disprect);
1188                                         long long int disty = centy - BLI_rcti_cent_y(&pa->disprect);
1189                                         sortlist[i].dist = (long long int)sqrt(distx * distx + disty * disty);
1190                                         sortlist[i].pa = pa;
1191                                         i++;
1192                                 }
1193                         }
1194                 }
1195
1196                 /* Now sort it */
1197                 qsort(sortlist, totsort, sizeof(*sortlist), sort_render_part);
1198
1199                 /* Finally flush it to the workqueue */
1200                 for (i = 0; i < totsort; i++) {
1201                         pa = sortlist[i].pa;
1202                         pa->nr = i + 1; /* for nicest part, and for stats */
1203                         BLI_thread_queue_push(workqueue, pa);
1204                 }
1205
1206                 MEM_freeN(sortlist);
1207
1208                 return totsort;
1209         }
1210
1211         return 0;
1212 }
1213
1214 static void print_part_stats(Render *re, RenderPart *pa)
1215 {
1216         char str[64];
1217         
1218         BLI_snprintf(str, sizeof(str), IFACE_("%s, Part %d-%d"), re->scene->id.name + 2, pa->nr, re->i.totpart);
1219         re->i.infostr = str;
1220         re->stats_draw(re->sdh, &re->i);
1221         re->i.infostr = NULL;
1222 }
1223
1224 typedef struct RenderThread {
1225         ThreadQueue *workqueue;
1226         ThreadQueue *donequeue;
1227         
1228         int number;
1229
1230         void (*display_update)(void *handle, RenderResult *rr, volatile rcti *rect);
1231         void *duh;
1232 } RenderThread;
1233
1234 static void *do_render_thread(void *thread_v)
1235 {
1236         RenderThread *thread = thread_v;
1237         RenderPart *pa;
1238         
1239         while ((pa = BLI_thread_queue_pop(thread->workqueue))) {
1240                 pa->thread = thread->number;
1241                 do_part_thread(pa);
1242
1243                 if (thread->display_update) {
1244                         thread->display_update(thread->duh, pa->result, NULL);
1245                 }
1246
1247                 BLI_thread_queue_push(thread->donequeue, pa);
1248                 
1249                 if (R.test_break(R.tbh))
1250                         break;
1251         }
1252         
1253         return NULL;
1254 }
1255
1256 static void main_render_result_end(Render *re)
1257 {
1258         if (re->result->do_exr_tile) {
1259                 BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
1260                 render_result_exr_file_end(re);
1261                 BLI_rw_mutex_unlock(&re->resultmutex);
1262         }
1263
1264         if (re->r.scemode & R_EXR_CACHE_FILE) {
1265                 BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
1266                 render_result_exr_file_cache_write(re);
1267                 BLI_rw_mutex_unlock(&re->resultmutex);
1268         }
1269 }
1270
1271 static void main_render_result_new(Render *re)
1272 {
1273         BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
1274
1275         /* first step; free the entire render result, make new, and/or prepare exr buffer saving */
1276         if (re->result == NULL || !(re->r.scemode & (R_BUTS_PREVIEW|R_VIEWPORT_PREVIEW))) {
1277                 render_result_free(re->result);
1278
1279                 if (re->sss_points && render_display_update_enabled(re))
1280                         re->result = render_result_new(re, &re->disprect, 0, RR_USE_MEM, RR_ALL_LAYERS, RR_ALL_VIEWS);
1281                 else if (re->r.scemode & R_FULL_SAMPLE)
1282                         re->result = render_result_new_full_sample(re, &re->fullresult, &re->disprect, 0, RR_USE_EXR, RR_ALL_VIEWS);
1283                 else
1284                         re->result = render_result_new(re, &re->disprect, 0,
1285                                 (re->r.scemode & R_EXR_TILE_FILE) ? RR_USE_EXR : RR_USE_MEM, RR_ALL_LAYERS, RR_ALL_VIEWS);
1286         }
1287
1288         BLI_rw_mutex_unlock(&re->resultmutex);
1289
1290         if (re->result) {
1291                 if (re->result->do_exr_tile) {
1292                         render_result_exr_file_begin(re);
1293                 }
1294         }
1295 }
1296
1297 static void threaded_tile_processor(Render *re)
1298 {
1299         RenderThread thread[BLENDER_MAX_THREADS];
1300         ThreadQueue *workqueue, *donequeue;
1301         ListBase threads;
1302         RenderPart *pa;
1303         rctf viewplane = re->viewplane;
1304         double lastdraw, elapsed, redrawtime = 1.0f;
1305         int totpart = 0, minx = 0, slice = 0, a, wait;
1306         
1307         if (re->result == NULL)
1308                 return;
1309
1310         /* warning; no return here without closing exr file */
1311         RE_parts_init(re, true);
1312         
1313         /* assuming no new data gets added to dbase... */
1314         R = *re;
1315         
1316         /* set threadsafe break */
1317         R.test_break = thread_break;
1318         
1319         /* create and fill work queue */
1320         workqueue = BLI_thread_queue_init();
1321         donequeue = BLI_thread_queue_init();
1322         
1323         /* for panorama we loop over slices */
1324         while (find_next_pano_slice(re, &slice, &minx, &viewplane)) {
1325                 /* gather parts into queue */
1326                 totpart = sort_and_queue_parts(re, minx, workqueue);
1327                 
1328                 BLI_thread_queue_nowait(workqueue);
1329                 
1330                 /* start all threads */
1331                 BLI_init_threads(&threads, do_render_thread, re->r.threads);
1332                 
1333                 for (a = 0; a < re->r.threads; a++) {
1334                         thread[a].workqueue = workqueue;
1335                         thread[a].donequeue = donequeue;
1336                         thread[a].number = a;
1337
1338                         if (render_display_update_enabled(re)) {
1339                                 thread[a].display_update = re->display_update;
1340                                 thread[a].duh = re->duh;
1341                         }
1342                         else {
1343                                 thread[a].display_update = NULL;
1344                                 thread[a].duh = NULL;
1345                         }
1346
1347                         BLI_insert_thread(&threads, &thread[a]);
1348                 }
1349                 
1350                 /* wait for results to come back */
1351                 lastdraw = PIL_check_seconds_timer();
1352                 
1353                 while (1) {
1354                         elapsed = PIL_check_seconds_timer() - lastdraw;
1355                         wait = (redrawtime - elapsed)*1000;
1356                         
1357                         /* handle finished part */
1358                         if ((pa=BLI_thread_queue_pop_timeout(donequeue, wait))) {
1359                                 if (pa->result) {
1360                                         print_part_stats(re, pa);
1361                                         
1362                                         render_result_free_list(&pa->fullresult, pa->result);
1363                                         pa->result = NULL;
1364                                         re->i.partsdone++;
1365                                         re->progress(re->prh, re->i.partsdone / (float)re->i.totpart);
1366                                 }
1367                                 
1368                                 totpart--;
1369                         }
1370                         
1371                         /* check for render cancel */
1372                         if ((g_break=re->test_break(re->tbh)))
1373                                 break;
1374                         
1375                         /* or done with parts */
1376                         if (totpart == 0)
1377                                 break;
1378                         
1379                         /* redraw in progress parts */
1380                         elapsed = PIL_check_seconds_timer() - lastdraw;
1381                         if (elapsed > redrawtime) {
1382                                 if (render_display_update_enabled(re))
1383                                         for (pa = re->parts.first; pa; pa = pa->next)
1384                                                 if ((pa->status == PART_STATUS_IN_PROGRESS) && pa->nr && pa->result)
1385                                                         re->display_update(re->duh, pa->result, &pa->result->renrect);
1386                                 
1387                                 lastdraw = PIL_check_seconds_timer();
1388                         }
1389                 }
1390                 
1391                 BLI_end_threads(&threads);
1392                 
1393                 if ((g_break=re->test_break(re->tbh)))
1394                         break;
1395         }
1396
1397         if (g_break) {
1398                 /* review the done queue and handle all the render parts,
1399                  * so no unfreed render result are lurking around
1400                  */
1401                 BLI_thread_queue_nowait(donequeue);
1402                 while ((pa = BLI_thread_queue_pop(donequeue))) {
1403                         if (pa->result) {
1404                                 render_result_free_list(&pa->fullresult, pa->result);
1405                                 pa->result = NULL;
1406                         }
1407                 }
1408         }
1409
1410         BLI_thread_queue_free(donequeue);
1411         BLI_thread_queue_free(workqueue);
1412
1413         if (re->result->do_exr_tile) {
1414                 BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
1415                 render_result_save_empty_result_tiles(re);
1416                 BLI_rw_mutex_unlock(&re->resultmutex);
1417         }
1418
1419         /* unset threadsafety */
1420         g_break = 0;
1421         BLI_rw_mutex_lock(&re->partsmutex, THREAD_LOCK_WRITE);
1422         RE_parts_free(re);
1423         BLI_rw_mutex_unlock(&re->partsmutex);
1424         re->viewplane = viewplane; /* restore viewplane, modified by pano render */
1425 }
1426
1427 #ifdef WITH_FREESTYLE
1428 static void init_freestyle(Render *re);
1429 static void add_freestyle(Render *re, int render);
1430 static void free_all_freestyle_renders(void);
1431 #endif
1432
1433 /* currently only called by preview renders and envmap */
1434 void RE_TileProcessor(Render *re)
1435 {
1436         main_render_result_new(re);
1437         threaded_tile_processor(re);
1438         
1439         re->i.lastframetime = PIL_check_seconds_timer() - re->i.starttime;
1440         re->stats_draw(re->sdh, &re->i);
1441
1442 #ifdef WITH_FREESTYLE
1443         /* Freestyle */
1444         if (re->r.mode & R_EDGE_FRS) {
1445                 if (!re->test_break(re->tbh)) {
1446                         init_freestyle(re);
1447                         add_freestyle(re, 1);
1448                         free_all_freestyle_renders();
1449                         
1450                         re->i.lastframetime = PIL_check_seconds_timer() - re->i.starttime;
1451                         re->stats_draw(re->sdh, &re->i);
1452                 }
1453         }
1454 #endif
1455
1456 }
1457
1458 /* ************  This part uses API, for rendering Blender scenes ********** */
1459
1460 static void do_render_3d(Render *re)
1461 {
1462         RenderView *rv;
1463         int cfra_backup;
1464
1465         re->current_scene_update(re->suh, re->scene);
1466
1467         /* try external */
1468         if (RE_engine_render(re, 0))
1469                 return;
1470
1471         /* internal */
1472         RE_parts_clamp(re);
1473         
1474         /* add motion blur and fields offset to frames */
1475         cfra_backup = re->scene->r.cfra;
1476
1477         BKE_scene_frame_set(re->scene, (double)re->scene->r.cfra + (double)re->mblur_offs + (double)re->field_offs);
1478
1479         /* init main render result */
1480         main_render_result_new(re);
1481
1482 #ifdef WITH_FREESTYLE
1483         if (re->r.mode & R_EDGE_FRS) {
1484                 init_freestyle(re);
1485         }
1486 #endif
1487
1488         /* we need a new database for each view */
1489         for (rv = re->result->views.first; rv; rv = rv->next) {
1490                 RE_SetActiveRenderView(re, rv->name);
1491
1492                 /* lock drawing in UI during data phase */
1493                 if (re->draw_lock)
1494                         re->draw_lock(re->dlh, 1);
1495
1496                 /* make render verts/faces/halos/lamps */
1497                 if (render_scene_needs_vector(re))
1498                         RE_Database_FromScene_Vectors(re, re->main, re->scene, re->lay);
1499                 else {
1500                         RE_Database_FromScene(re, re->main, re->scene, re->lay, 1);
1501                         RE_Database_Preprocess(re);
1502                 }
1503         
1504                 /* clear UI drawing locks */
1505                 if (re->draw_lock)
1506                         re->draw_lock(re->dlh, 0);
1507         
1508                 threaded_tile_processor(re);
1509         
1510 #ifdef WITH_FREESTYLE
1511                 /* Freestyle */
1512                 if (re->r.mode & R_EDGE_FRS)
1513                         if (!re->test_break(re->tbh))
1514                                 add_freestyle(re, 1);
1515 #endif
1516         
1517                 /* do left-over 3d post effects (flares) */
1518                 if (re->flag & R_HALO)
1519                         if (!re->test_break(re->tbh))
1520                                 add_halo_flare(re);
1521
1522                 /* free all render verts etc */
1523                 RE_Database_Free(re);
1524         }
1525
1526         main_render_result_end(re);
1527
1528         re->scene->r.cfra = cfra_backup;
1529         re->scene->r.subframe = 0.f;
1530 }
1531
1532 /* called by blur loop, accumulate RGBA key alpha */
1533 static void addblur_rect_key(RenderResult *rr, float *rectf, float *rectf1, float blurfac)
1534 {
1535         float mfac = 1.0f - blurfac;
1536         int a, b, stride = 4 * rr->rectx;
1537         int len = stride * sizeof(float);
1538         
1539         for (a = 0; a < rr->recty; a++) {
1540                 if (blurfac == 1.0f) {
1541                         memcpy(rectf, rectf1, len);
1542                 }
1543                 else {
1544                         float *rf = rectf, *rf1 = rectf1;
1545                         
1546                         for (b = rr->rectx; b > 0; b--, rf += 4, rf1 += 4) {
1547                                 if (rf1[3] < 0.01f)
1548                                         rf[3] = mfac * rf[3];
1549                                 else if (rf[3] < 0.01f) {
1550                                         rf[0] = rf1[0];
1551                                         rf[1] = rf1[1];
1552                                         rf[2] = rf1[2];
1553                                         rf[3] = blurfac * rf1[3];
1554                                 }
1555                                 else {
1556                                         rf[0] = mfac * rf[0] + blurfac * rf1[0];
1557                                         rf[1] = mfac * rf[1] + blurfac * rf1[1];
1558                                         rf[2] = mfac * rf[2] + blurfac * rf1[2];
1559                                         rf[3] = mfac * rf[3] + blurfac * rf1[3];
1560                                 }
1561                         }
1562                 }
1563                 rectf += stride;
1564                 rectf1 += stride;
1565         }
1566 }
1567
1568 /* called by blur loop, accumulate renderlayers */
1569 static void addblur_rect(RenderResult *rr, float *rectf, float *rectf1, float blurfac, int channels)
1570 {
1571         float mfac = 1.0f - blurfac;
1572         int a, b, stride = channels * rr->rectx;
1573         int len = stride * sizeof(float);
1574         
1575         for (a = 0; a < rr->recty; a++) {
1576                 if (blurfac == 1.0f) {
1577                         memcpy(rectf, rectf1, len);
1578                 }
1579                 else {
1580                         float *rf = rectf, *rf1 = rectf1;
1581                         
1582                         for (b = rr->rectx * channels; b > 0; b--, rf++, rf1++) {
1583                                 rf[0] = mfac * rf[0] + blurfac * rf1[0];
1584                         }
1585                 }
1586                 rectf += stride;
1587                 rectf1 += stride;
1588         }
1589 }
1590
1591
1592 /* called by blur loop, accumulate renderlayers */
1593 static void merge_renderresult_blur(RenderResult *rr, RenderResult *brr, float blurfac, bool key_alpha)
1594 {
1595         RenderLayer *rl, *rl1;
1596         RenderPass *rpass, *rpass1;
1597         
1598         rl1 = brr->layers.first;
1599         for (rl = rr->layers.first; rl && rl1; rl = rl->next, rl1 = rl1->next) {
1600                 /* passes are allocated in sync */
1601                 rpass1 = rl1->passes.first;
1602                 for (rpass = rl->passes.first; rpass && rpass1; rpass = rpass->next, rpass1 = rpass1->next) {
1603                         if ((rpass->passtype & SCE_PASS_COMBINED) && key_alpha)
1604                                 addblur_rect_key(rr, rpass->rect, rpass1->rect, blurfac);
1605                         else
1606                                 addblur_rect(rr, rpass->rect, rpass1->rect, blurfac, rpass->channels);
1607                 }
1608         }
1609 }
1610
1611 /* main blur loop, can be called by fields too */
1612 static void do_render_blur_3d(Render *re)
1613 {
1614         RenderResult *rres;
1615         float blurfac;
1616         int blur = re->r.mblur_samples;
1617         
1618         /* create accumulation render result */
1619         rres = render_result_new(re, &re->disprect, 0, RR_USE_MEM, RR_ALL_LAYERS, RR_ALL_VIEWS);
1620         
1621         /* do the blur steps */
1622         while (blur--) {
1623                 re->mblur_offs = re->r.blurfac * ((float)(re->r.mblur_samples - blur)) / (float)re->r.mblur_samples;
1624                 
1625                 re->i.curblur = re->r.mblur_samples - blur;    /* stats */
1626                 
1627                 do_render_3d(re);
1628                 
1629                 blurfac = 1.0f / (float)(re->r.mblur_samples - blur);
1630                 
1631                 merge_renderresult_blur(rres, re->result, blurfac, false);
1632                 if (re->test_break(re->tbh)) break;
1633         }
1634         
1635         /* swap results */
1636         BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
1637         render_result_free(re->result);
1638         re->result = rres;
1639         BLI_rw_mutex_unlock(&re->resultmutex);
1640         
1641         re->mblur_offs = 0.0f;
1642         re->i.curblur = 0;   /* stats */
1643         
1644         /* make sure motion blur changes get reset to current frame */
1645         if ((re->r.scemode & (R_NO_FRAME_UPDATE|R_BUTS_PREVIEW|R_VIEWPORT_PREVIEW))==0) {
1646                 BKE_scene_update_for_newframe(re->eval_ctx, re->main, re->scene, re->lay);
1647         }
1648         
1649         /* weak... the display callback wants an active renderlayer pointer... */
1650         re->result->renlay = render_get_active_layer(re, re->result);
1651         re->display_update(re->duh, re->result, NULL);
1652 }
1653
1654
1655 /* function assumes rectf1 and rectf2 to be half size of rectf */
1656 static void interleave_rect(RenderResult *rr, float *rectf, float *rectf1, float *rectf2, int channels)
1657 {
1658         int a, stride = channels * rr->rectx;
1659         int len = stride * sizeof(float);
1660         
1661         for (a = 0; a < rr->recty; a += 2) {
1662                 memcpy(rectf, rectf1, len);
1663                 rectf += stride;
1664                 rectf1 += stride;
1665                 memcpy(rectf, rectf2, len);
1666                 rectf += stride;
1667                 rectf2 += stride;
1668         }
1669 }
1670
1671 /* merge render results of 2 fields */
1672 static void merge_renderresult_fields(RenderResult *rr, RenderResult *rr1, RenderResult *rr2)
1673 {
1674         RenderLayer *rl, *rl1, *rl2;
1675         RenderPass *rpass, *rpass1, *rpass2;
1676         
1677         rl1 = rr1->layers.first;
1678         rl2 = rr2->layers.first;
1679         for (rl = rr->layers.first; rl && rl1 && rl2; rl = rl->next, rl1 = rl1->next, rl2 = rl2->next) {
1680                 
1681                 /* passes are allocated in sync */
1682                 rpass1 = rl1->passes.first;
1683                 rpass2 = rl2->passes.first;
1684                 for (rpass = rl->passes.first;
1685                      rpass && rpass1 && rpass2;
1686                      rpass = rpass->next, rpass1 = rpass1->next, rpass2 = rpass2->next)
1687                 {
1688                         interleave_rect(rr, rpass->rect, rpass1->rect, rpass2->rect, rpass->channels);
1689                 }
1690         }
1691 }
1692
1693
1694 /* interleaves 2 frames */
1695 static void do_render_fields_3d(Render *re)
1696 {
1697         Object *camera = RE_GetCamera(re);
1698         RenderResult *rr1, *rr2 = NULL;
1699         
1700         /* no render result was created, we can safely halve render y */
1701         re->winy /= 2;
1702         re->recty /= 2;
1703         re->disprect.ymin /= 2;
1704         re->disprect.ymax /= 2;
1705         
1706         re->i.curfield = 1;  /* stats */
1707         
1708         /* first field, we have to call camera routine for correct aspect and subpixel offset */
1709         RE_SetCamera(re, camera);
1710         if (re->r.mode & R_MBLUR && (re->r.scemode & R_FULL_SAMPLE) == 0)
1711                 do_render_blur_3d(re);
1712         else
1713                 do_render_3d(re);
1714
1715         BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
1716         rr1 = re->result;
1717         re->result = NULL;
1718         BLI_rw_mutex_unlock(&re->resultmutex);
1719         
1720         /* second field */
1721         if (!re->test_break(re->tbh)) {
1722                 
1723                 re->i.curfield = 2;  /* stats */
1724                 
1725                 re->flag |= R_SEC_FIELD;
1726                 if ((re->r.mode & R_FIELDSTILL) == 0) {
1727                         re->field_offs = 0.5f;
1728                 }
1729                 RE_SetCamera(re, camera);
1730                 if (re->r.mode & R_MBLUR && (re->r.scemode & R_FULL_SAMPLE) == 0)
1731                         do_render_blur_3d(re);
1732                 else
1733                         do_render_3d(re);
1734                 re->flag &= ~R_SEC_FIELD;
1735                 
1736                 re->field_offs = 0.0f;
1737                 
1738                 rr2 = re->result;
1739         }
1740         
1741         /* allocate original height new buffers */
1742         re->winy *= 2;
1743         re->recty *= 2;
1744         re->disprect.ymin *= 2;
1745         re->disprect.ymax *= 2;
1746
1747         BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
1748         re->result = render_result_new(re, &re->disprect, 0, RR_USE_MEM, RR_ALL_LAYERS, RR_ALL_VIEWS);
1749
1750         if (rr2) {
1751                 if (re->r.mode & R_ODDFIELD)
1752                         merge_renderresult_fields(re->result, rr2, rr1);
1753                 else
1754                         merge_renderresult_fields(re->result, rr1, rr2);
1755                 
1756                 render_result_free(rr2);
1757         }
1758
1759         render_result_free(rr1);
1760         
1761         re->i.curfield = 0;  /* stats */
1762         
1763         /* weak... the display callback wants an active renderlayer pointer... */
1764         re->result->renlay = render_get_active_layer(re, re->result);
1765
1766         BLI_rw_mutex_unlock(&re->resultmutex);
1767
1768         re->display_update(re->duh, re->result, NULL);
1769 }
1770
1771 /* make sure disprect is not affected by the render border */
1772 static void render_result_disprect_to_full_resolution(Render *re)
1773 {
1774         re->disprect.xmin = re->disprect.ymin = 0;
1775         re->disprect.xmax = re->winx;
1776         re->disprect.ymax = re->winy;
1777         re->rectx = re->winx;
1778         re->recty = re->winy;
1779 }
1780
1781 /* main render routine, no compositing */
1782 static void do_render_fields_blur_3d(Render *re)
1783 {
1784         Object *camera = RE_GetCamera(re);
1785         /* also check for camera here */
1786         if (camera == NULL) {
1787                 BKE_report(re->reports, RPT_ERROR, "Cannot render, no camera");
1788                 G.is_break = true;
1789                 return;
1790         }
1791
1792         /* now use renderdata and camera to set viewplane */
1793         RE_SetCamera(re, camera);
1794         
1795         if (re->r.mode & R_FIELDS)
1796                 do_render_fields_3d(re);
1797         else if (re->r.mode & R_MBLUR && (re->r.scemode & R_FULL_SAMPLE) == 0)
1798                 do_render_blur_3d(re);
1799         else
1800                 do_render_3d(re);
1801         
1802         /* when border render, check if we have to insert it in black */
1803         if (re->result) {
1804                 if (re->r.mode & R_BORDER) {
1805                         if ((re->r.mode & R_CROP) == 0) {
1806                                 RenderResult *rres;
1807
1808                                 /* backup */
1809                                 const rcti orig_disprect = re->disprect;
1810                                 const int  orig_rectx = re->rectx, orig_recty = re->recty;
1811                                 
1812                                 BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
1813
1814                                 /* sub-rect for merge call later on */
1815                                 re->result->tilerect = re->disprect;
1816                                 
1817                                 /* weak is: it chances disprect from border */
1818                                 render_result_disprect_to_full_resolution(re);
1819                                 
1820                                 rres = render_result_new(re, &re->disprect, 0, RR_USE_MEM, RR_ALL_LAYERS, RR_ALL_VIEWS);
1821                                 
1822                                 render_result_merge(rres, re->result);
1823                                 render_result_free(re->result);
1824                                 re->result = rres;
1825                                 
1826                                 /* weak... the display callback wants an active renderlayer pointer... */
1827                                 re->result->renlay = render_get_active_layer(re, re->result);
1828                                 
1829                                 BLI_rw_mutex_unlock(&re->resultmutex);
1830                 
1831                                 re->display_init(re->dih, re->result);
1832                                 re->display_update(re->duh, re->result, NULL);
1833
1834                                 /* restore the disprect from border */
1835                                 re->disprect = orig_disprect;
1836                                 re->rectx = orig_rectx;
1837                                 re->recty = orig_recty;
1838                         }
1839                         else {
1840                                 /* set offset (again) for use in compositor, disprect was manipulated. */
1841                                 re->result->xof = 0;
1842                                 re->result->yof = 0;
1843                         }
1844                 }
1845         }
1846 }
1847
1848
1849 /* within context of current Render *re, render another scene.
1850  * it uses current render image size and disprect, but doesn't execute composite
1851  */
1852 static void render_scene(Render *re, Scene *sce, int cfra)
1853 {
1854         Render *resc = RE_NewRender(sce->id.name);
1855         int winx = re->winx, winy = re->winy;
1856         
1857         sce->r.cfra = cfra;
1858
1859         BKE_scene_camera_switch_update(sce);
1860
1861         /* exception: scene uses own size (unfinished code) */
1862         if (0) {
1863                 winx = (sce->r.size * sce->r.xsch) / 100;
1864                 winy = (sce->r.size * sce->r.ysch) / 100;
1865         }
1866         
1867         /* initial setup */
1868         RE_InitState(resc, re, &sce->r, NULL, winx, winy, &re->disprect);
1869
1870         /* We still want to use 'rendercache' setting from org (main) scene... */
1871         resc->r.scemode = (resc->r.scemode & ~R_EXR_CACHE_FILE) | (re->r.scemode & R_EXR_CACHE_FILE);
1872
1873         /* still unsure entity this... */
1874         resc->main = re->main;
1875         resc->scene = sce;
1876         resc->lay = sce->lay;
1877         resc->scene_color_manage = BKE_scene_check_color_management_enabled(sce);
1878         
1879         /* ensure scene has depsgraph, base flags etc OK */
1880         BKE_scene_set_background(re->main, sce);
1881
1882         /* copy callbacks */
1883         resc->display_update = re->display_update;
1884         resc->duh = re->duh;
1885         resc->test_break = re->test_break;
1886         resc->tbh = re->tbh;
1887         resc->stats_draw = re->stats_draw;
1888         resc->sdh = re->sdh;
1889         resc->current_scene_update = re->current_scene_update;
1890         resc->suh = re->suh;
1891         
1892         do_render_fields_blur_3d(resc);
1893 }
1894
1895 /* helper call to detect if this scene needs a render, or if there's a any render layer to render */
1896 static int composite_needs_render(Scene *sce, int this_scene)
1897 {
1898         bNodeTree *ntree = sce->nodetree;
1899         bNode *node;
1900         
1901         if (ntree == NULL) return 1;
1902         if (sce->use_nodes == false) return 1;
1903         if ((sce->r.scemode & R_DOCOMP) == 0) return 1;
1904         
1905         for (node = ntree->nodes.first; node; node = node->next) {
1906                 if (node->type == CMP_NODE_R_LAYERS && (node->flag & NODE_MUTED) == 0)
1907                         if (this_scene == 0 || node->id == NULL || node->id == &sce->id)
1908                                 return 1;
1909         }
1910         return 0;
1911 }
1912
1913 static bool rlayer_node_uses_alpha(bNodeTree *ntree, bNode *node)
1914 {
1915         bNodeSocket *sock;
1916
1917         for (sock = node->outputs.first; sock; sock = sock->next) {
1918                 /* Weak! but how to make it better? */
1919                 if (STREQ(sock->name, "Alpha") && nodeCountSocketLinks(ntree, sock) > 0)
1920                         return true;
1921         }
1922
1923         return false;
1924 }
1925
1926 bool RE_allow_render_generic_object(Object *ob)
1927 {
1928         /* override not showing object when duplis are used with particles */
1929         if (ob->transflag & OB_DUPLIPARTS) {
1930                 /* pass */  /* let particle system(s) handle showing vs. not showing */
1931         }
1932         else if ((ob->transflag & OB_DUPLI) && !(ob->transflag & OB_DUPLIFRAMES)) {
1933                 return false;
1934         }
1935         return true;
1936 }
1937
1938 /* Issue here is that it's possible that object which is used by boolean,
1939  * array or shrinkwrap modifiers weren't displayed in the viewport before
1940  * rendering. This leads to situations when apply() of this modifiers
1941  * could not get ob->derivedFinal and modifiers are not being applied.
1942  *
1943  * This was worked around by direct call of get_derived_final() from those
1944  * modifiers, but such approach leads to write conflicts with threaded
1945  * update.
1946  *
1947  * Here we make sure derivedFinal will be calculated by update_for_newframe
1948  * function later in the pipeline and all the modifiers are applied
1949  * properly without hacks from their side.
1950  *                                                  - sergey -
1951  */
1952 #define DEPSGRAPH_WORKAROUND_HACK
1953
1954 #ifdef DEPSGRAPH_WORKAROUND_HACK
1955 static void tag_dependend_objects_for_render(Scene *scene, int renderlay)
1956 {
1957         Scene *sce_iter;
1958         Base *base;
1959         for (SETLOOPER(scene, sce_iter, base)) {
1960                 Object *object = base->object;
1961
1962                 if ((base->lay & renderlay) == 0) {
1963                         continue;
1964                 }
1965
1966                 if (object->type == OB_MESH) {
1967                         if (RE_allow_render_generic_object(object)) {
1968                                 ModifierData *md;
1969                                 VirtualModifierData virtualModifierData;
1970
1971                                 for (md = modifiers_getVirtualModifierList(object, &virtualModifierData);
1972                                      md;
1973                                      md = md->next)
1974                                 {
1975                                         if (!modifier_isEnabled(scene, md, eModifierMode_Render)) {
1976                                                 continue;
1977                                         }
1978
1979                                         if (md->type == eModifierType_Boolean) {
1980                                                 BooleanModifierData *bmd = (BooleanModifierData *)md;
1981                                                 if (bmd->object && bmd->object->type == OB_MESH) {
1982                                                         DAG_id_tag_update(&bmd->object->id, OB_RECALC_DATA);
1983                                                 }
1984                                         }
1985                                         else if (md->type == eModifierType_Array) {
1986                                                 ArrayModifierData *amd = (ArrayModifierData *)md;
1987                                                 if (amd->start_cap && amd->start_cap->type == OB_MESH) {
1988                                                         DAG_id_tag_update(&amd->start_cap->id, OB_RECALC_DATA);
1989                                                 }
1990                                                 if (amd->end_cap && amd->end_cap->type == OB_MESH) {
1991                                                         DAG_id_tag_update(&amd->end_cap->id, OB_RECALC_DATA);
1992                                                 }
1993                                         }
1994                                         else if (md->type == eModifierType_Shrinkwrap) {
1995                                                 ShrinkwrapModifierData *smd = (ShrinkwrapModifierData *)md;
1996                                                 if (smd->target  && smd->target->type == OB_MESH) {
1997                                                         DAG_id_tag_update(&smd->target->id, OB_RECALC_DATA);
1998                                                 }
1999                                         }
2000                                 }
2001                         }
2002                 }
2003         }
2004 }
2005 #endif
2006
2007 static void tag_scenes_for_render(Render *re)
2008 {
2009         bNode *node;
2010         Scene *sce;
2011 #ifdef DEPSGRAPH_WORKAROUND_HACK
2012         int renderlay = re->lay;
2013 #endif
2014         
2015         for (sce = re->main->scene.first; sce; sce = sce->id.next) {
2016                 sce->id.flag &= ~LIB_DOIT;
2017 #ifdef DEPSGRAPH_WORKAROUND_HACK
2018                 tag_dependend_objects_for_render(sce, renderlay);
2019 #endif
2020         }
2021         
2022 #ifdef WITH_FREESTYLE
2023         if (re->freestyle_bmain) {
2024                 for (sce = re->freestyle_bmain->scene.first; sce; sce = sce->id.next) {
2025                         sce->id.flag &= ~LIB_DOIT;
2026 #ifdef DEPSGRAPH_WORKAROUND_HACK
2027                         tag_dependend_objects_for_render(sce, renderlay);
2028 #endif
2029                 }
2030         }
2031 #endif
2032
2033         if (RE_GetCamera(re) && composite_needs_render(re->scene, 1)) {
2034                 re->scene->id.flag |= LIB_DOIT;
2035 #ifdef DEPSGRAPH_WORKAROUND_HACK
2036                 tag_dependend_objects_for_render(re->scene, renderlay);
2037 #endif
2038         }
2039         
2040         if (re->scene->nodetree == NULL) return;
2041         
2042         /* check for render-layers nodes using other scenes, we tag them LIB_DOIT */
2043         for (node = re->scene->nodetree->nodes.first; node; node = node->next) {
2044                 node->flag &= ~NODE_TEST;
2045                 if (node->type == CMP_NODE_R_LAYERS && (node->flag & NODE_MUTED) == 0) {
2046                         if (node->id) {
2047                                 if (!MAIN_VERSION_ATLEAST(re->main, 265, 5)) {
2048                                         if (rlayer_node_uses_alpha(re->scene->nodetree, node)) {
2049                                                 Scene *scene = (Scene *)node->id;
2050
2051                                                 if (scene->r.alphamode != R_ALPHAPREMUL) {
2052                                                         BKE_reportf(re->reports, RPT_WARNING, "Setting scene %s alpha mode to Premul", scene->id.name + 2);
2053
2054                                                         /* also print, so feedback is immediate */
2055                                                         printf("2.66 versioning fix: setting scene %s alpha mode to Premul\n", scene->id.name + 2);
2056
2057                                                         scene->r.alphamode = R_ALPHAPREMUL;
2058                                                 }
2059                                         }
2060                                 }
2061
2062                                 if (node->id != (ID *)re->scene) {
2063                                         if ((node->id->flag & LIB_DOIT) == 0) {
2064                                                 Scene *scene = (Scene *) node->id;
2065                                                 if (render_scene_has_layers_to_render(scene)) {
2066                                                         node->flag |= NODE_TEST;
2067                                                         node->id->flag |= LIB_DOIT;
2068 #ifdef DEPSGRAPH_WORKAROUND_HACK
2069                                                         tag_dependend_objects_for_render(scene, renderlay);
2070 #endif
2071                                                 }
2072                                         }
2073                                 }
2074                         }
2075                 }
2076         }
2077         
2078 }
2079
2080 static void ntree_render_scenes(Render *re)
2081 {
2082         bNode *node;
2083         int cfra = re->scene->r.cfra;
2084         Scene *restore_scene = re->scene;
2085         bool scene_changed = false;
2086         
2087         if (re->scene->nodetree == NULL) return;
2088         
2089         tag_scenes_for_render(re);
2090         
2091         /* now foreach render-result node tagged we do a full render */
2092         /* results are stored in a way compisitor will find it */
2093         for (node = re->scene->nodetree->nodes.first; node; node = node->next) {
2094                 if (node->type == CMP_NODE_R_LAYERS && (node->flag & NODE_MUTED) == 0) {
2095                         if (node->id && node->id != (ID *)re->scene) {
2096                                 if (node->flag & NODE_TEST) {
2097                                         Scene *scene = (Scene *)node->id;
2098
2099                                         scene_changed |= scene != restore_scene;
2100                                         render_scene(re, scene, cfra);
2101                                         node->flag &= ~NODE_TEST;
2102                                         
2103                                         nodeUpdate(restore_scene->nodetree, node);
2104                                 }
2105                         }
2106                 }
2107         }
2108
2109         /* restore scene if we rendered another last */
2110         if (scene_changed)
2111                 BKE_scene_set_background(re->main, re->scene);
2112 }
2113
2114 /* bad call... need to think over proper method still */
2115 static void render_composit_stats(void *UNUSED(arg), const char *str)
2116 {
2117         RenderStats i;
2118         memcpy(&i, &R.i, sizeof(i));
2119         i.infostr = str;
2120         R.stats_draw(R.sdh, &i);
2121 }
2122
2123 #ifdef WITH_FREESTYLE
2124 /* init Freestyle renderer */
2125 static void init_freestyle(Render *re)
2126 {
2127         re->freestyle_bmain = BKE_main_new();
2128
2129         /* We use the same window manager for freestyle bmain as
2130         * real bmain uses. This is needed because freestyle's
2131         * bmain could be used to tag scenes for update, which
2132         * implies call of ED_render_scene_update in some cases
2133         * and that function requires proper window manager
2134         * to present (sergey)
2135         */
2136         re->freestyle_bmain->wm = re->main->wm;
2137
2138         FRS_init_stroke_renderer(re);
2139 }
2140
2141 /* invokes Freestyle stroke rendering */
2142 static void add_freestyle(Render *re, int render)
2143 {
2144         SceneRenderLayer *srl, *actsrl;
2145         LinkData *link;
2146         Render *r;
2147         const bool do_link = (re->r.mode & R_MBLUR) == 0 || re->i.curblur == re->r.mblur_samples;
2148
2149         actsrl = BLI_findlink(&re->r.layers, re->r.actlay);
2150
2151         FRS_begin_stroke_rendering(re);
2152
2153         for (srl = (SceneRenderLayer *)re->r.layers.first; srl; srl = srl->next) {
2154                 if (do_link) {
2155                         link = (LinkData *)MEM_callocN(sizeof(LinkData), "LinkData to Freestyle render");
2156                         BLI_addtail(&re->freestyle_renders, link);
2157                 }
2158                 if ((re->r.scemode & R_SINGLE_LAYER) && srl != actsrl)
2159                         continue;
2160                 if (FRS_is_freestyle_enabled(srl)) {
2161                         r = FRS_do_stroke_rendering(re, srl, render);
2162                         if (do_link)
2163                                 link->data = (void *)r;
2164                 }
2165         }
2166
2167         FRS_end_stroke_rendering(re);
2168
2169         /* restore the global R value (invalidated by nested execution of the internal renderer) */
2170         R = *re;
2171 }
2172
2173 /* merges the results of Freestyle stroke rendering into a given render result */
2174 static void composite_freestyle_renders(Render *re, int sample)
2175 {
2176         Render *freestyle_render;
2177         RenderView *rv;
2178         SceneRenderLayer *srl, *actsrl;
2179         LinkData *link;
2180
2181         actsrl = BLI_findlink(&re->r.layers, re->r.actlay);
2182
2183         link = (LinkData *)re->freestyle_renders.first;
2184
2185         for (rv = re->result->views.first; rv; rv = rv->next) {
2186                 for (srl = (SceneRenderLayer *)re->r.layers.first; srl; srl = srl->next) {
2187                         if ((re->r.scemode & R_SINGLE_LAYER) && srl != actsrl)
2188                                 continue;
2189
2190                         if (FRS_is_freestyle_enabled(srl)) {
2191                                 freestyle_render = (Render *)link->data;
2192
2193                                 /* may be NULL in case of empty render layer */
2194                                 if (freestyle_render) {
2195                                         render_result_exr_file_read_sample(freestyle_render, sample);
2196                                         FRS_composite_result(re, srl, freestyle_render);
2197                                         RE_FreeRenderResult(freestyle_render->result);
2198                                         freestyle_render->result = NULL;
2199                                 }
2200                         }
2201                         link = link->next;
2202                 }
2203         }
2204 }
2205
2206 /* releases temporary scenes and renders for Freestyle stroke rendering */
2207 static void free_all_freestyle_renders(void)
2208 {
2209         Render *re1, *freestyle_render;
2210         Scene *freestyle_scene;
2211         LinkData *link;
2212
2213         for (re1= RenderGlobal.renderlist.first; re1; re1= re1->next) {
2214                 for (link = (LinkData *)re1->freestyle_renders.first; link; link = link->next) {
2215                         freestyle_render = (Render *)link->data;
2216
2217                         if (freestyle_render) {
2218                                 freestyle_scene = freestyle_render->scene;
2219                                 RE_FreeRender(freestyle_render);
2220                                 BKE_scene_unlink(re1->freestyle_bmain, freestyle_scene, NULL);
2221                         }
2222                 }
2223                 BLI_freelistN(&re1->freestyle_renders);
2224
2225                 if (re1->freestyle_bmain) {
2226                         /* detach the window manager from freestyle bmain (see comments
2227                          * in add_freestyle() for more detail)
2228                          */
2229                         BLI_listbase_clear(&re1->freestyle_bmain->wm);
2230
2231                         BKE_main_free(re1->freestyle_bmain);
2232                         re1->freestyle_bmain = NULL;
2233                 }
2234         }
2235 }
2236 #endif
2237
2238 /* reads all buffers, calls optional composite, merges in first result->views rectf */
2239 static void do_merge_fullsample(Render *re, bNodeTree *ntree)
2240 {
2241         ListBase *rectfs;
2242         RenderView *rv;
2243         float *rectf, filt[3][3];
2244         int x, y, sample;
2245         int nr, numviews;
2246         
2247         /* interaction callbacks */
2248         if (ntree) {
2249                 ntree->stats_draw = render_composit_stats;
2250                 ntree->test_break = re->test_break;
2251                 ntree->progress = re->progress;
2252                 ntree->sdh = re->sdh;
2253                 ntree->tbh = re->tbh;
2254                 ntree->prh = re->prh;
2255         }
2256         
2257         /* filtmask needs it */
2258         R = *re;
2259         
2260         /* temporary storage of the acccumulation buffers */
2261         rectfs = MEM_callocN(sizeof(ListBase), "fullsample accumulation buffers");
2262
2263         numviews = BLI_listbase_count(&re->result->views);
2264         for (nr = 0; nr < numviews; nr++) {
2265                 rv = MEM_callocN(sizeof(RenderView), "fullsample renderview");
2266
2267                 /* we accumulate in here */
2268                 rv->rectf = MEM_mapallocN(re->rectx * re->recty * sizeof(float) * 4, "fullsample rgba");
2269                 BLI_addtail(rectfs, rv);
2270         }
2271
2272         for (sample = 0; sample < re->r.osa; sample++) {
2273                 Scene *sce;
2274                 Render *re1;
2275                 RenderResult rres;
2276                 int mask;
2277                 
2278                 /* enable full sample print */
2279                 R.i.curfsa = sample + 1;
2280                 
2281                 /* set all involved renders on the samplebuffers (first was done by render itself, but needs tagged) */
2282                 /* also function below assumes this */
2283                         
2284                 tag_scenes_for_render(re);
2285                 for (sce = re->main->scene.first; sce; sce = sce->id.next) {
2286                         if (sce->id.flag & LIB_DOIT) {
2287                                 re1 = RE_GetRender(sce->id.name);
2288
2289                                 if (re1 && (re1->r.scemode & R_FULL_SAMPLE)) {
2290                                         if (sample) {
2291                                                 BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
2292                                                 render_result_exr_file_read_sample(re1, sample);
2293 #ifdef WITH_FREESTYLE
2294                                                 if (re1->r.mode & R_EDGE_FRS)
2295                                                         composite_freestyle_renders(re1, sample);
2296 #endif
2297                                                 BLI_rw_mutex_unlock(&re->resultmutex);
2298                                         }
2299                                         ntreeCompositTagRender(re1->scene); /* ensure node gets exec to put buffers on stack */
2300                                 }
2301                         }
2302                 }
2303                 
2304                 /* composite */
2305                 if (ntree) {
2306                         ntreeCompositTagRender(re->scene);
2307                         ntreeCompositTagAnimated(ntree);
2308
2309                         for (rv = re->result->views.first; rv; rv = rv->next) {
2310                                 ntreeCompositExecTree(re->scene, ntree, &re->r, true, G.background == 0, &re->scene->view_settings, &re->scene->display_settings, rv->name);
2311                         }
2312                 }
2313
2314                 for (nr = 0, rv = rectfs->first; rv; rv = rv->next, nr++) {
2315                         rectf = rv->rectf;
2316
2317                         /* ensure we get either composited result or the active layer */
2318                         RE_AcquireResultImage(re, &rres, nr);
2319
2320                         /* accumulate with filter, and clip */
2321                         mask = (1 << sample);
2322                         mask_array(mask, filt);
2323
2324                         for (y = 0; y < re->recty; y++) {
2325                                 float *rf = rectf + 4 * y * re->rectx;
2326                                 float *col = rres.rectf + 4 * y * re->rectx;
2327                                 
2328                                 for (x = 0; x < re->rectx; x++, rf += 4, col += 4) {
2329                                         /* clamping to 1.0 is needed for correct AA */
2330                                         CLAMP(col[0], 0.0f, 1.0f);
2331                                         CLAMP(col[1], 0.0f, 1.0f);
2332                                         CLAMP(col[2], 0.0f, 1.0f);
2333                                         
2334                                         add_filt_fmask_coord(filt, col, rf, re->rectx, re->recty, x, y);
2335                                 }
2336                         }
2337                 
2338                         RE_ReleaseResultImage(re);
2339
2340                         /* show stuff */
2341                         if (sample != re->osa - 1) {
2342                                 /* weak... the display callback wants an active renderlayer pointer... */
2343                                 re->result->renlay = render_get_active_layer(re, re->result);
2344                                 RE_SetActiveRenderView(re, rv->name);
2345                                 re->display_update(re->duh, re->result, NULL);
2346                         }
2347                 }
2348         }
2349
2350         for (nr = 0; nr < numviews; nr++) {
2351                 rectf = ((RenderView *)BLI_findlink(rectfs, nr))->rectf;
2352
2353                 /* clamp alpha and RGB to 0..1 and 0..inf, can go outside due to filter */
2354                 for (y = 0; y < re->recty; y++) {
2355                         float *rf = rectf + 4 * y * re->rectx;
2356                         
2357                         for (x = 0; x < re->rectx; x++, rf += 4) {
2358                                 rf[0] = MAX2(rf[0], 0.0f);
2359                                 rf[1] = MAX2(rf[1], 0.0f);
2360                                 rf[2] = MAX2(rf[2], 0.0f);
2361                                 CLAMP(rf[3], 0.0f, 1.0f);
2362                         }
2363                 }
2364
2365                 /* store the final result */
2366                 BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
2367                 rv = RE_RenderViewGetById(re->result, nr);
2368                 if (rv->rectf)
2369                         MEM_freeN(rv->rectf);
2370                 rv->rectf = rectf;
2371                 BLI_rw_mutex_unlock(&re->resultmutex);
2372         }
2373         
2374         /* clear interaction callbacks */
2375         if (ntree) {
2376                 ntree->stats_draw = NULL;
2377                 ntree->test_break = NULL;
2378                 ntree->progress = NULL;
2379                 ntree->tbh = ntree->sdh = ntree->prh = NULL;
2380         }
2381         
2382         /* disable full sample print */
2383         R.i.curfsa = 0;
2384
2385         /* garbage collection */
2386         while (rectfs->first) {
2387                 RenderView *rv = rectfs->first;
2388                 BLI_remlink(rectfs, rv);
2389                 MEM_freeN(rv);
2390         }
2391         MEM_freeN(rectfs);
2392 }
2393
2394 /* called externally, via compositor */
2395 void RE_MergeFullSample(Render *re, Main *bmain, Scene *sce, bNodeTree *ntree)
2396 {
2397         Scene *scene;
2398         bNode *node;
2399
2400         /* default start situation */
2401         G.is_break = false;
2402         
2403         re->main = bmain;
2404         re->scene = sce;
2405         re->scene_color_manage = BKE_scene_check_color_management_enabled(sce);
2406         
2407         /* first call RE_ReadRenderResult on every renderlayer scene. this creates Render structs */
2408         
2409         /* tag scenes unread */
2410         for (scene = re->main->scene.first; scene; scene = scene->id.next)
2411                 scene->id.flag |= LIB_DOIT;
2412         
2413 #ifdef WITH_FREESTYLE
2414         if (re->freestyle_bmain) {
2415                 for (scene = re->freestyle_bmain->scene.first; scene; scene = scene->id.next)
2416                         scene->id.flag &= ~LIB_DOIT;
2417         }
2418 #endif
2419
2420         for (node = ntree->nodes.first; node; node = node->next) {
2421                 if (node->type == CMP_NODE_R_LAYERS && (node->flag & NODE_MUTED) == 0) {
2422                         Scene *nodescene = (Scene *)node->id;
2423                         
2424                         if (nodescene == NULL) nodescene = sce;
2425                         if (nodescene->id.flag & LIB_DOIT) {
2426                                 nodescene->r.mode |= R_OSA; /* render struct needs tables */
2427                                 RE_ReadRenderResult(sce, nodescene);
2428                                 nodescene->id.flag &= ~LIB_DOIT;
2429                         }
2430                 }
2431         }
2432         
2433         /* own render result should be read/allocated */
2434         if (re->scene->id.flag & LIB_DOIT) {
2435                 RE_ReadRenderResult(re->scene, re->scene);
2436                 re->scene->id.flag &= ~LIB_DOIT;
2437         }
2438         
2439         /* and now we can draw (result is there) */
2440         re->display_init(re->dih, re->result);
2441         re->display_clear(re->dch, re->result);
2442         
2443 #ifdef WITH_FREESTYLE
2444         if (re->r.mode & R_EDGE_FRS) {
2445                 init_freestyle(re);
2446                 add_freestyle(re, 0);
2447         }
2448 #endif
2449
2450         do_merge_fullsample(re, ntree);
2451
2452 #ifdef WITH_FREESTYLE
2453         free_all_freestyle_renders();
2454 #endif
2455 }
2456
2457 /* returns fully composited render-result on given time step (in RenderData) */
2458 static void do_render_composite_fields_blur_3d(Render *re)
2459 {
2460         bNodeTree *ntree = re->scene->nodetree;
2461         int update_newframe = 0;
2462         
2463         /* INIT seeding, compositor can use random texture */
2464         BLI_srandom(re->r.cfra);
2465         
2466         if (composite_needs_render(re->scene, 1)) {
2467                 /* save memory... free all cached images */
2468                 ntreeFreeCache(ntree);
2469
2470                 /* render the frames
2471                  * it could be optimized to render only the needed view
2472                  * but what if a scene has a different number of views
2473                  * than the main scene? */
2474                 do_render_fields_blur_3d(re);
2475         }
2476         else {
2477                 re->i.cfra = re->r.cfra;
2478
2479                 /* ensure new result gets added, like for regular renders */
2480                 BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
2481                 
2482                 render_result_free(re->result);
2483                 if ((re->r.mode & R_CROP) == 0) {
2484                         render_result_disprect_to_full_resolution(re);
2485                 }
2486                 re->result = render_result_new(re, &re->disprect, 0, RR_USE_MEM, RR_ALL_LAYERS, RR_ALL_VIEWS);
2487
2488                 BLI_rw_mutex_unlock(&re->resultmutex);
2489                 
2490                 /* scene render process already updates animsys */
2491                 update_newframe = 1;
2492         }
2493         
2494         /* swap render result */
2495         if (re->r.scemode & R_SINGLE_LAYER) {
2496                 BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
2497                 render_result_single_layer_end(re);
2498                 BLI_rw_mutex_unlock(&re->resultmutex);
2499         }
2500         
2501         if (!re->test_break(re->tbh)) {
2502                 
2503                 if (ntree) {
2504                         ntreeCompositTagRender(re->scene);
2505                         ntreeCompositTagAnimated(ntree);
2506                 }
2507                 
2508                 if (ntree && re->scene->use_nodes && re->r.scemode & R_DOCOMP) {
2509                         /* checks if there are render-result nodes that need scene */
2510                         if ((re->r.scemode & R_SINGLE_LAYER) == 0)
2511                                 ntree_render_scenes(re);
2512                         
2513                         if (!re->test_break(re->tbh)) {
2514                                 ntree->stats_draw = render_composit_stats;
2515                                 ntree->test_break = re->test_break;
2516                                 ntree->progress = re->progress;
2517                                 ntree->sdh = re->sdh;
2518                                 ntree->tbh = re->tbh;
2519                                 ntree->prh = re->prh;
2520                                 
2521                                 /* in case it was never initialized */
2522                                 R.sdh = re->sdh;
2523                                 R.stats_draw = re->stats_draw;
2524                                 R.i.starttime = re->i.starttime;
2525                                 R.i.cfra = re->i.cfra;
2526                                 
2527                                 if (update_newframe)
2528                                         BKE_scene_update_for_newframe(re->eval_ctx, re->main, re->scene, re->lay);
2529                                 
2530                                 if (re->r.scemode & R_FULL_SAMPLE)
2531                                         do_merge_fullsample(re, ntree);
2532                                 else {
2533                                         RenderView *rv;
2534                                         for (rv = re->result->views.first; rv; rv = rv->next) {
2535                                                 ntreeCompositExecTree(re->scene, ntree, &re->r, true, G.background == 0, &re->scene->view_settings, &re->scene->display_settings, rv->name);
2536                                         }
2537                                 }
2538                                 
2539                                 ntree->stats_draw = NULL;
2540                                 ntree->test_break = NULL;
2541                                 ntree->progress = NULL;
2542                                 ntree->tbh = ntree->sdh = ntree->prh = NULL;
2543                         }
2544                 }
2545                 else if (re->r.scemode & R_FULL_SAMPLE)
2546                         do_merge_fullsample(re, NULL);
2547         }
2548
2549 #ifdef WITH_FREESTYLE
2550         free_all_freestyle_renders();
2551 #endif
2552
2553         /* weak... the display callback wants an active renderlayer pointer... */
2554         re->result->renlay = render_get_active_layer(re, re->result);
2555         re->display_update(re->duh, re->result, NULL);
2556 }
2557
2558 static void renderresult_stampinfo(Render *re)
2559 {
2560         RenderResult rres;
2561         RenderView *rv;
2562         int nr;
2563
2564         /* this is the basic trick to get the displayed float or char rect from render result */
2565         nr = 0;
2566         for (rv = re->result->views.first;rv;rv = rv->next, nr++) {
2567                 RE_SetActiveRenderView(re, rv->name);
2568                 RE_AcquireResultImage(re, &rres, nr);
2569                 BKE_image_stamp_buf(re->scene, RE_GetCamera(re), (unsigned char *)rres.rect32, rres.rectf, rres.rectx, rres.recty, 4);
2570                 RE_ReleaseResultImage(re);
2571         }
2572 }
2573
2574 int RE_seq_render_active(Scene *scene, RenderData *rd)
2575 {
2576         Editing *ed;
2577         Sequence *seq;
2578
2579         ed = scene->ed;
2580         
2581         if (!(rd->scemode & R_DOSEQ) || !ed || !ed->seqbase.first)
2582                 return 0;
2583         
2584         for (seq = ed->seqbase.first; seq; seq = seq->next) {
2585                 if (seq->type != SEQ_TYPE_SOUND_RAM)
2586                         return 1;
2587         }
2588         
2589         return 0;
2590 }
2591
2592 static void do_render_seq(Render *re)
2593 {
2594         static int recurs_depth = 0;
2595         struct ImBuf *out;
2596         RenderResult *rr; /* don't assign re->result here as it might change during give_ibuf_seq */
2597         int cfra = re->r.cfra;
2598         SeqRenderData context;
2599         int view_id, tot_views;
2600         struct ImBuf **ibuf_arr;
2601         int re_x, re_y;
2602
2603         re->i.cfra = cfra;
2604
2605         if (recurs_depth == 0) {
2606                 /* otherwise sequencer animation isn't updated */
2607                 BKE_animsys_evaluate_all_animation(re->main, re->scene, (float)cfra); // XXX, was BKE_scene_frame_get(re->scene)
2608         }
2609
2610         recurs_depth++;
2611
2612         if ((re->r.mode & R_BORDER) && (re->r.mode & R_CROP) == 0) {
2613                 /* if border rendering is used and cropping is disabled, final buffer should
2614                  * be as large as the whole frame */
2615                 re_x = re->winx;
2616                 re_y = re->winy;
2617         }
2618         else {
2619                 re_x = re->result->rectx;
2620                 re_y = re->result->recty;
2621         }
2622
2623         tot_views = BKE_scene_multiview_num_views_get(&re->r);
2624         ibuf_arr = MEM_mallocN(sizeof(ImBuf *) * tot_views, "Sequencer Views ImBufs");
2625
2626         BKE_sequencer_new_render_data(
2627                 re->eval_ctx, re->main, re->scene,
2628                 re_x, re_y, 100,
2629                 &context);
2630
2631         /* the renderresult gets destroyed during the rendering, so we first collect all ibufs
2632          * and then we populate the final renderesult */
2633
2634         for (view_id = 0; view_id < tot_views; view_id++) {
2635                 context.view_id = view_id;
2636                 out = BKE_sequencer_give_ibuf(&context, cfra, 0);
2637
2638                 if (out) {
2639                         ibuf_arr[view_id] = IMB_dupImBuf(out);
2640                         IMB_metadata_copy(ibuf_arr[view_id], out);
2641                         IMB_freeImBuf(out);
2642                         BKE_sequencer_imbuf_from_sequencer_space(re->scene, ibuf_arr[view_id]);
2643                 }
2644                 else {
2645                         ibuf_arr[view_id] = NULL;
2646                 }
2647         }
2648
2649         rr = re->result;
2650
2651         BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
2652         render_result_views_new(rr, &re->r);
2653         BLI_rw_mutex_unlock(&re->resultmutex);
2654
2655         for (view_id = 0; view_id < tot_views; view_id++) {
2656                 RenderView *rv = RE_RenderViewGetById(rr, view_id);
2657                 BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
2658
2659                 if (ibuf_arr[view_id]) {
2660                         /* copy ibuf into combined pixel rect */
2661                         RE_render_result_rect_from_ibuf(rr, &re->r, ibuf_arr[view_id], view_id);
2662
2663                         if (ibuf_arr[view_id]->metadata && (re->r.stamp & R_STAMP_STRIPMETA)) {
2664                                 /* ensure render stamp info first */
2665                                 BKE_render_result_stamp_info(NULL, NULL, rr, true);
2666                                 BKE_stamp_info_from_imbuf(rr, ibuf_arr[view_id]);
2667                         }
2668
2669                         if (recurs_depth == 0) { /* with nested scenes, only free on toplevel... */
2670                                 Editing *ed = re->scene->ed;
2671                                 if (ed)
2672                                         BKE_sequencer_free_imbuf(re->scene, &ed->seqbase, true);
2673                         }
2674                         IMB_freeImBuf(ibuf_arr[view_id]);
2675                 }
2676                 else {
2677                         /* render result is delivered empty in most cases, nevertheless we handle all cases */
2678                         render_result_rect_fill_zero(rr, view_id);
2679                 }
2680
2681                 BLI_rw_mutex_unlock(&re->resultmutex);
2682
2683                 /* would mark display buffers as invalid */
2684                 RE_SetActiveRenderView(re, rv->name);
2685                 re->display_update(re->duh, re->result, NULL);
2686         }
2687
2688         MEM_freeN(ibuf_arr);
2689
2690         recurs_depth--;
2691
2692         /* just in case this flag went missing at some point */
2693         re->r.scemode |= R_DOSEQ;
2694
2695         /* set overall progress of sequence rendering */
2696         if (re->r.efra != re->r.sfra)
2697                 re->progress(re->prh, (float)(cfra - re->r.sfra) / (re->r.efra - re->r.sfra));
2698         else
2699                 re->progress(re->prh, 1.0f);
2700 }
2701
2702 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
2703
2704 /* main loop: doing sequence + fields + blur + 3d render + compositing */
2705 static void do_render_all_options(Render *re)
2706 {
2707         Object *camera;
2708         bool render_seq = false;
2709
2710         re->current_scene_update(re->suh, re->scene);
2711
2712         BKE_scene_camera_switch_update(re->scene);
2713
2714         re->i.starttime = PIL_check_seconds_timer();
2715
2716         /* ensure no images are in memory from previous animated sequences */
2717         BKE_image_all_free_anim_ibufs(re->r.cfra);
2718
2719         if (RE_engine_render(re, 1)) {
2720                 /* in this case external render overrides all */
2721         }
2722         else if (RE_seq_render_active(re->scene, &re->r)) {
2723                 /* note: do_render_seq() frees rect32 when sequencer returns float images */
2724                 if (!re->test_break(re->tbh)) {
2725                         do_render_seq(re);
2726                         render_seq = true;
2727                 }
2728                 
2729                 re->stats_draw(re->sdh, &re->i);
2730                 re->display_update(re->duh, re->result, NULL);
2731         }
2732         else {
2733                 re->pool = BKE_image_pool_new();
2734
2735                 do_render_composite_fields_blur_3d(re);
2736
2737                 BKE_image_pool_free(re->pool);
2738                 re->pool = NULL;
2739         }
2740         
2741         re->i.lastframetime = PIL_check_seconds_timer() - re->i.starttime;
2742         
2743         re->stats_draw(re->sdh, &re->i);
2744         
2745         /* save render result stamp if needed */
2746         camera = RE_GetCamera(re);
2747         /* sequence rendering should have taken care of that already */
2748         if (!(render_seq && (re->r.stamp & R_STAMP_STRIPMETA)))
2749                 BKE_render_result_stamp_info(re->scene, camera, re->result, false);
2750
2751         /* stamp image info here */
2752         if ((re->r.stamp & R_STAMP_ALL) && (re->r.stamp & R_STAMP_DRAW)) {
2753                 renderresult_stampinfo(re);
2754                 re->display_update(re->duh, re->result, NULL);
2755         }
2756 }
2757
2758 bool RE_force_single_renderlayer(Scene *scene)
2759 {
2760         int scemode = check_mode_full_sample(&scene->r);
2761         if (scemode & R_SINGLE_LAYER) {
2762                 SceneRenderLayer *srl = BLI_findlink(&scene->r.layers, scene->r.actlay);
2763                 /* force layer to be enabled */
2764                 if (srl->layflag & SCE_LAY_DISABLE) {
2765                         srl->layflag &= ~SCE_LAY_DISABLE;
2766                         return true;
2767                 }
2768         }
2769         return false;
2770 }
2771
2772 static bool check_valid_compositing_camera(Scene *scene, Object *camera_override)
2773 {
2774         if (scene->r.scemode & R_DOCOMP && scene->use_nodes) {
2775                 bNode *node = scene->nodetree->nodes.first;
2776
2777                 while (node) {
2778                         if (node->type == CMP_NODE_R_LAYERS && (node->flag & NODE_MUTED) == 0) {
2779                                 Scene *sce = node->id ? (Scene *)node->id : scene;
2780
2781                                 if (!sce->camera && !BKE_scene_camera_find(sce)) {
2782                                         /* all render layers nodes need camera */
2783                                         return false;
2784                                 }
2785                         }
2786
2787                         node = node->next;
2788                 }
2789
2790                 return true;
2791         }
2792         else {
2793                 return (camera_override != NULL || scene->camera != NULL);
2794         }
2795 }
2796
2797 static bool check_valid_camera_multiview(Scene *scene, Object *camera, ReportList *reports)
2798 {
2799         SceneRenderView *srv;
2800         bool active_view = false;
2801
2802         if ((scene->r.scemode & R_MULTIVIEW) == 0)
2803                 return true;
2804
2805         for (srv = scene->r.views.first; srv; srv = srv->next) {
2806                 if (BKE_scene_multiview_is_render_view_active(&scene->r, srv)) {
2807                         active_view = true;
2808
2809                         if (scene->r.views_format == SCE_VIEWS_FORMAT_MULTIVIEW) {
2810                                 Object *view_camera;
2811                                 view_camera = BKE_camera_multiview_render(scene, camera, srv->name);
2812
2813                                 if (view_camera == camera) {
2814                                         /* if the suffix is not in the camera, means we are using the fallback camera */
2815                                         if (!BLI_str_endswith(view_camera->id.name + 2, srv->suffix)) {
2816                                                 BKE_reportf(reports, RPT_ERROR, "Camera \"%s\" is not a multi-view camera",
2817                                                             camera->id.name + 2);
2818                                                 return false;
2819                                         }
2820                                 }
2821                         }
2822                 }
2823         }
2824
2825         if (!active_view) {
2826                 BKE_reportf(reports, RPT_ERROR, "No active view found in scene \"%s\"", scene->id.name + 2);
2827                 return false;
2828         }
2829
2830         return true;
2831 }
2832
2833 static int check_valid_camera(Scene *scene, Object *camera_override, ReportList *reports)
2834 {
2835         if (camera_override == NULL && scene->camera == NULL)
2836                 scene->camera = BKE_scene_camera_find(scene);
2837
2838         if (!check_valid_camera_multiview(scene, scene->camera, reports))
2839                 return false;
2840
2841         if (RE_seq_render_active(scene, &scene->r)) {
2842                 if (scene->ed) {
2843                         Sequence *seq = scene->ed->seqbase.first;
2844
2845                         while (seq) {
2846                                 if (seq->type == SEQ_TYPE_SCENE && seq->scene) {
2847                                         if (!seq->scene_camera) {
2848                                                 if (!seq->scene->camera && !BKE_scene_camera_find(seq->scene)) {
2849                                                         /* camera could be unneeded due to composite nodes */
2850                                                         Object *override = (seq->scene == scene) ? camera_override : NULL;
2851
2852                                                         if (!check_valid_compositing_camera(seq->scene, override)) {
2853                                                                 BKE_reportf(reports, RPT_ERROR, "No camera found in scene \"%s\"", seq->scene->id.name+2);
2854                                                                 return false;
2855                                                         }
2856                                                 }
2857                                         }
2858                                         else if (!check_valid_camera_multiview(seq->scene, seq->scene_camera, reports))
2859                                                 return false;
2860                                 }
2861
2862                                 seq = seq->next;
2863                         }
2864                 }
2865         }
2866         else if (!check_valid_compositing_camera(scene, camera_override)) {
2867                 BKE_report(reports, RPT_ERROR, "No camera found in scene");
2868                 return false;
2869         }
2870
2871         return true;
2872 }
2873
2874 static bool node_tree_has_composite_output(bNodeTree *ntree)
2875 {
2876         bNode *node;
2877
2878         for (node = ntree->nodes.first; node; node = node->next) {
2879                 if (ELEM(node->type, CMP_NODE_COMPOSITE, CMP_NODE_OUTPUT_FILE)) {
2880                         return true;
2881                 }
2882                 else if (node->type == NODE_GROUP) {
2883                         if (node->id) {
2884                                 if (node_tree_has_composite_output((bNodeTree *)node->id)) {
2885                                         return true;
2886                                 }
2887                         }
2888                 }
2889         }
2890
2891         return false;
2892 }
2893
2894 static int check_composite_output(Scene *scene)
2895 {
2896         return node_tree_has_composite_output(scene->nodetree);
2897 }
2898
2899 bool RE_is_rendering_allowed(Scene *scene, Object *camera_override, ReportList *reports)
2900 {
2901         int scemode = check_mode_full_sample(&scene->r);
2902         
2903         if (scene->r.mode & R_BORDER) {
2904                 if (scene->r.border.xmax <= scene->r.border.xmin ||
2905                     scene->r.border.ymax <= scene->r.border.ymin)
2906                 {
2907                         BKE_report(reports, RPT_ERROR, "No border area selected");
2908                         return 0;
2909                 }
2910         }
2911         
2912         if (scemode & (R_EXR_TILE_FILE | R_FULL_SAMPLE)) {
2913                 char str[FILE_MAX];
2914                 
2915                 render_result_exr_file_path(scene, "", 0, str);
2916                 
2917                 if (!BLI_file_is_writable(str)) {
2918                         BKE_report(reports, RPT_ERROR, "Cannot save render buffers, check the temp default path");
2919                         return 0;
2920                 }
2921                 
2922                 /* no fullsample and edge */
2923                 if ((scemode & R_FULL_SAMPLE) && (scene->r.mode & R_EDGE)) {
2924                         BKE_report(reports, RPT_ERROR, "Full sample does not support edge enhance");
2925                         return 0;
2926                 }
2927                 
2928         }
2929         
2930         if (scemode & R_DOCOMP) {
2931                 if (scene->use_nodes) {
2932                         if (!scene->nodetree) {
2933                                 BKE_report(reports, RPT_ERROR, "No node tree in scene");
2934                                 return 0;
2935                         }
2936                         
2937                         if (!check_composite_output(scene)) {
2938                                 BKE_report(reports, RPT_ERROR, "No render output node in scene");
2939                                 return 0;
2940                         }
2941                         
2942                         if (scemode & R_FULL_SAMPLE) {
2943                                 if (composite_needs_render(scene, 0) == 0) {
2944                                         BKE_report(reports, RPT_ERROR, "Full sample AA not supported without 3D rendering");
2945                                         return 0;
2946                                 }
2947                         }
2948                 }
2949         }
2950         
2951         /* check valid camera, without camera render is OK (compo, seq) */
2952         if (!check_valid_camera(scene, camera_override, reports)) {
2953                 return 0;
2954         }
2955         
2956         /* get panorama & ortho, only after camera is set */
2957         BKE_camera_object_mode(&scene->r, camera_override ? camera_override : scene->camera);
2958
2959         /* forbidden combinations */
2960         if (scene->r.mode & R_PANORAMA) {
2961                 if (scene->r.mode & R_ORTHO) {
2962                         BKE_report(reports, RPT_ERROR, "No ortho render possible for panorama");
2963                         return 0;
2964                 }
2965
2966 #ifdef WITH_FREESTYLE
2967                 if (scene->r.mode & R_EDGE_FRS) {
2968                         BKE_report(reports, RPT_ERROR, "Panoramic camera not supported in Freestyle");
2969                         return 0;
2970                 }
2971 #endif
2972         }
2973
2974 #ifdef WITH_FREESTYLE
2975         if (scene->r.mode & R_EDGE_FRS) {
2976                 if (scene->r.mode & R_FIELDS) {
2977                         BKE_report(reports, RPT_ERROR, "Fields not supported in Freestyle");
2978                         return false;
2979                 }
2980         }
2981 #endif
2982
2983         /* layer flag tests */
2984         if (!render_scene_has_layers_to_render(scene)) {
2985                 BKE_report(reports, RPT_ERROR, "All render layers are disabled");
2986                 return 0;
2987         }
2988
2989         return 1;
2990 }
2991
2992 static void validate_render_settings(Render *re)
2993 {
2994         if (re->r.scemode & (R_EXR_TILE_FILE | R_FULL_SAMPLE)) {
2995                 /* no osa + fullsample won't work... */
2996                 if (re->r.osa == 0)
2997                         re->r.scemode &= ~R_FULL_SAMPLE;
2998         }
2999
3000         if (RE_engine_is_external(re)) {
3001                 /* not supported yet */
3002                 re->r.scemode &= ~(R_FULL_SAMPLE);
3003                 re->r.mode &= ~(R_FIELDS | R_MBLUR);
3004         }
3005 }
3006
3007 static void update_physics_cache(Render *re, Scene *scene, int UNUSED(anim_init))
3008 {
3009         PTCacheBaker baker;
3010
3011         baker.main = re->main;
3012         baker.scene = scene;
3013         baker.pid = NULL;
3014         baker.bake = 0;
3015         baker.render = 1;
3016         baker.anim_init = 1;
3017         baker.quick_step = 1;
3018         baker.break_test = re->test_break;
3019         baker.break_data = re->tbh;
3020         baker.progressbar = NULL;
3021
3022         BKE_ptcache_bake(&baker);
3023 }
3024
3025 void RE_SetActiveRenderView(Render *re, const char *viewname)
3026 {
3027         BLI_strncpy(re->viewname, viewname, sizeof(re->viewname));
3028 }
3029
3030 const char *RE_GetActiveRenderView(Render *re)
3031 {
3032         return re->viewname;
3033 }
3034
3035 /* evaluating scene options for general Blender render */
3036 static int render_initialize_from_main(Render *re, RenderData *rd, Main *bmain, Scene *scene, SceneRenderLayer *srl,
3037                                        Object *camera_override, unsigned int lay_override, int anim, int anim_init)
3038 {
3039         int winx, winy;
3040         rcti disprect;
3041         
3042         /* r.xsch and r.ysch has the actual view window size
3043          * r.border is the clipping rect */
3044         
3045         /* calculate actual render result and display size */
3046         winx = (rd->size * rd->xsch) / 100;
3047         winy = (rd->size * rd->ysch) / 100;
3048         
3049         /* we always render smaller part, inserting it in larger image is compositor bizz, it uses disprect for it */
3050         if (scene->r.mode & R_BORDER) {
3051                 disprect.xmin = rd->border.xmin * winx;
3052                 disprect.xmax = rd->border.xmax * winx;
3053                 
3054                 disprect.ymin = rd->border.ymin * winy;
3055                 disprect.ymax = rd->border.ymax * winy;
3056         }
3057         else {
3058                 disprect.xmin = disprect.ymin = 0;
3059                 disprect.xmax = winx;
3060                 disprect.ymax = winy;
3061         }
3062         
3063         re->main = bmain;
3064         re->scene = scene;
3065         re->scene_color_manage = BKE_scene_check_color_management_enabled(scene);
3066         re->camera_override = camera_override;
3067         re->lay = lay_override ? lay_override : scene->lay;
3068         re->layer_override = lay_override;
3069         re->i.localview = (re->lay & 0xFF000000) != 0;
3070         re->viewname[0] = '\0';
3071
3072         /* not too nice, but it survives anim-border render */
3073         if (anim) {
3074                 render_update_anim_renderdata(re, &scene->r);
3075                 re->disprect = disprect;
3076                 return 1;
3077         }
3078         
3079         /* check all scenes involved */
3080         tag_scenes_for_render(re);
3081
3082         /*
3083          * Disabled completely for now,
3084          * can be later set as render profile option
3085          * and default for background render.
3086          */
3087         if (0) {
3088                 /* make sure dynamics are up to date */
3089                 update_physics_cache(re, scene, anim_init);
3090         }
3091         
3092         if (srl || scene->r.scemode & R_SINGLE_LAYER) {
3093                 BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
3094                 render_result_single_layer_begin(re);
3095                 BLI_rw_mutex_unlock(&re->resultmutex);
3096         }
3097         
3098         RE_InitState(re, NULL, &scene->r, srl, winx, winy, &disprect);
3099         if (!re->ok)  /* if an error was printed, abort */
3100                 return 0;
3101         
3102         /* initstate makes new result, have to send changed tags around */
3103         ntreeCompositTagRender(re->scene);
3104
3105         validate_render_settings(re);
3106
3107         re->display_init(re->dih, re->result);
3108         re->display_clear(re->dch, re->result);
3109         
3110         return 1;
3111 }
3112
3113 void RE_SetReports(Render *re, ReportList *reports)
3114 {
3115         re->reports = reports;
3116 }
3117
3118 /* general Blender frame render call */
3119 void RE_BlenderFrame(Render *re, Main *bmain, Scene *scene, SceneRenderLayer *srl, Object *camera_override,
3120                      unsigned int lay_override, int frame, const bool write_still)
3121 {
3122         BLI_callback_exec(re->main, (ID *)scene, BLI_CB_EVT_RENDER_INIT);
3123
3124         /* ugly global still... is to prevent preview events and signal subsurfs etc to make full resol */
3125         G.is_rendering = true;
3126         
3127         scene->r.cfra = frame;
3128         
3129         if (render_initialize_from_main(re, &scene->r, bmain, scene, srl, camera_override, lay_override, 0, 0)) {
3130                 MEM_reset_peak_memory();
3131
3132                 BLI_callback_exec(re->main, (ID *)scene, BLI_CB_EVT_RENDER_PRE);
3133
3134                 do_render_all_options(re);
3135
3136                 if (write_still && !G.is_break) {
3137                         if (BKE_imtype_is_movie(scene->r.im_format.imtype)) {
3138                                 /* operator checks this but in case its called from elsewhere */
3139                                 printf("Error: cant write single images with a movie format!\n");
3140                         }
3141                         else {
3142                                 char name[FILE_MAX];
3143                                 BKE_image_path_from_imformat(
3144                                         name, scene->r.pic, bmain->name, scene->r.cfra,
3145                                         &scene->r.im_format, (scene->r.scemode & R_EXTENSION) != 0, false, NULL);
3146
3147                                 /* reports only used for Movie */
3148                                 do_write_image_or_movie(re, bmain, scene, NULL, 0, name);
3149                         }
3150                 }
3151
3152                 BLI_callback_exec(re-