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