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