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