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