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