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