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