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, Group *group)
1240 {
1241         if (group->id.tag & LIB_TAG_DOIT) {
1242                 return;
1243         }
1244         group->id.tag |= LIB_TAG_DOIT;
1245
1246         for (GroupObject *go = group->gobject.first; go != NULL; go = go->next) {
1247                 Object *object = go->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                                                                 for (GroupObject *go = part->dup_group->gobject.first;
1305                                                                      go != NULL;
1306                                                                      go = go->next)
1307                                                                 {
1308                                                                         DEG_id_tag_update(&go->ob->id, OB_RECALC_DATA);
1309                                                                 }
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_groups_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_groups_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                 BKE_animsys_evaluate_all_animation(re->main, re->scene, (float)cfra); // XXX, was BKE_scene_frame_get(re->scene)
1682         }
1683
1684         recurs_depth++;
1685
1686         if ((re->r.mode & R_BORDER) && (re->r.mode & R_CROP) == 0) {
1687                 /* if border rendering is used and cropping is disabled, final buffer should
1688                  * be as large as the whole frame */
1689                 re_x = re->winx;
1690                 re_y = re->winy;
1691         }
1692         else {
1693                 re_x = re->result->rectx;
1694                 re_y = re->result->recty;
1695         }
1696
1697         tot_views = BKE_scene_multiview_num_views_get(&re->r);
1698         ibuf_arr = MEM_mallocN(sizeof(ImBuf *) * tot_views, "Sequencer Views ImBufs");
1699
1700         BKE_sequencer_new_render_data(
1701                 re->main, re->scene,
1702                 re_x, re_y, 100, true,
1703                 &context);
1704
1705         /* the renderresult gets destroyed during the rendering, so we first collect all ibufs
1706          * and then we populate the final renderesult */
1707
1708         for (view_id = 0; view_id < tot_views; view_id++) {
1709                 context.view_id = view_id;
1710                 out = BKE_sequencer_give_ibuf(&context, cfra, 0);
1711
1712                 if (out) {
1713                         ibuf_arr[view_id] = IMB_dupImBuf(out);
1714                         IMB_metadata_copy(ibuf_arr[view_id], out);
1715                         IMB_freeImBuf(out);
1716                         BKE_sequencer_imbuf_from_sequencer_space(re->scene, ibuf_arr[view_id]);
1717                 }
1718                 else {
1719                         ibuf_arr[view_id] = NULL;
1720                 }
1721         }
1722
1723         rr = re->result;
1724
1725         BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
1726         render_result_views_new(rr, &re->r);
1727         BLI_rw_mutex_unlock(&re->resultmutex);
1728
1729         for (view_id = 0; view_id < tot_views; view_id++) {
1730                 RenderView *rv = RE_RenderViewGetById(rr, view_id);
1731                 BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
1732
1733                 if (ibuf_arr[view_id]) {
1734                         /* copy ibuf into combined pixel rect */
1735                         RE_render_result_rect_from_ibuf(rr, &re->r, ibuf_arr[view_id], view_id);
1736
1737                         if (ibuf_arr[view_id]->metadata && (re->r.stamp & R_STAMP_STRIPMETA)) {
1738                                 /* ensure render stamp info first */
1739                                 BKE_render_result_stamp_info(NULL, NULL, rr, true);
1740                                 BKE_stamp_info_from_imbuf(rr, ibuf_arr[view_id]);
1741                         }
1742
1743                         if (recurs_depth == 0) { /* with nested scenes, only free on toplevel... */
1744                                 Editing *ed = re->scene->ed;
1745                                 if (ed)
1746                                         BKE_sequencer_free_imbuf(re->scene, &ed->seqbase, true);
1747                         }
1748                         IMB_freeImBuf(ibuf_arr[view_id]);
1749                 }
1750                 else {
1751                         /* render result is delivered empty in most cases, nevertheless we handle all cases */
1752                         render_result_rect_fill_zero(rr, view_id);
1753                 }
1754
1755                 BLI_rw_mutex_unlock(&re->resultmutex);
1756
1757                 /* would mark display buffers as invalid */
1758                 RE_SetActiveRenderView(re, rv->name);
1759                 re->display_update(re->duh, re->result, NULL);
1760         }
1761
1762         MEM_freeN(ibuf_arr);
1763
1764         recurs_depth--;
1765
1766         /* just in case this flag went missing at some point */
1767         re->r.scemode |= R_DOSEQ;
1768
1769         /* set overall progress of sequence rendering */
1770         if (re->r.efra != re->r.sfra)
1771                 re->progress(re->prh, (float)(cfra - re->r.sfra) / (re->r.efra - re->r.sfra));
1772         else
1773                 re->progress(re->prh, 1.0f);
1774 }
1775
1776 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
1777
1778 /* main loop: doing sequence + 3d render + compositing */
1779 static void do_render_all_options(Render *re)
1780 {
1781         Object *camera;
1782         bool render_seq = false;
1783
1784         re->current_scene_update(re->suh, re->scene);
1785
1786         BKE_scene_camera_switch_update(re->scene);
1787
1788         re->i.starttime = PIL_check_seconds_timer();
1789
1790         /* ensure no images are in memory from previous animated sequences */
1791         BKE_image_all_free_anim_ibufs(re->r.cfra);
1792         BKE_sequencer_all_free_anim_ibufs(re->r.cfra);
1793
1794         if (RE_engine_render(re, 1)) {
1795                 /* in this case external render overrides all */
1796         }
1797         else if (RE_seq_render_active(re->scene, &re->r)) {
1798                 /* note: do_render_seq() frees rect32 when sequencer returns float images */
1799                 if (!re->test_break(re->tbh)) {
1800                         do_render_seq(re);
1801                         render_seq = true;
1802                 }
1803                 
1804                 re->stats_draw(re->sdh, &re->i);
1805                 re->display_update(re->duh, re->result, NULL);
1806         }
1807         else {
1808                 do_render_composite(re);
1809         }
1810         
1811         re->i.lastframetime = PIL_check_seconds_timer() - re->i.starttime;
1812         
1813         re->stats_draw(re->sdh, &re->i);
1814         
1815         /* save render result stamp if needed */
1816         if (re->result != NULL) {
1817                 camera = RE_GetCamera(re);
1818                 /* sequence rendering should have taken care of that already */
1819                 if (!(render_seq && (re->r.stamp & R_STAMP_STRIPMETA)))
1820                         BKE_render_result_stamp_info(re->scene, camera, re->result, false);
1821
1822                 /* stamp image info here */
1823                 if ((re->r.stamp & R_STAMP_ALL) && (re->r.stamp & R_STAMP_DRAW)) {
1824                         renderresult_stampinfo(re);
1825                         re->display_update(re->duh, re->result, NULL);
1826                 }
1827         }
1828 }
1829
1830 static bool check_valid_compositing_camera(Scene *scene, Object *camera_override)
1831 {
1832         if (scene->r.scemode & R_DOCOMP && scene->use_nodes) {
1833                 bNode *node = scene->nodetree->nodes.first;
1834
1835                 while (node) {
1836                         if (node->type == CMP_NODE_R_LAYERS && (node->flag & NODE_MUTED) == 0) {
1837                                 Scene *sce = node->id ? (Scene *)node->id : scene;
1838                                 if (sce->camera == NULL) {
1839                                         sce->camera = BKE_view_layer_camera_find(BKE_view_layer_default_render(sce));
1840                                 }
1841                                 if (sce->camera == NULL) {
1842                                         /* all render layers nodes need camera */
1843                                         return false;
1844                                 }
1845                         }
1846                         node = node->next;
1847                 }
1848
1849                 return true;
1850         }
1851         else {
1852                 return (camera_override != NULL || scene->camera != NULL);
1853         }
1854 }
1855
1856 static bool check_valid_camera_multiview(Scene *scene, Object *camera, ReportList *reports)
1857 {
1858         SceneRenderView *srv;
1859         bool active_view = false;
1860
1861         if (camera == NULL || (scene->r.scemode & R_MULTIVIEW) == 0)
1862                 return true;
1863
1864         for (srv = scene->r.views.first; srv; srv = srv->next) {
1865                 if (BKE_scene_multiview_is_render_view_active(&scene->r, srv)) {
1866                         active_view = true;
1867
1868                         if (scene->r.views_format == SCE_VIEWS_FORMAT_MULTIVIEW) {
1869                                 Object *view_camera;
1870                                 view_camera = BKE_camera_multiview_render(scene, camera, srv->name);
1871
1872                                 if (view_camera == camera) {
1873                                         /* if the suffix is not in the camera, means we are using the fallback camera */
1874                                         if (!BLI_str_endswith(view_camera->id.name + 2, srv->suffix)) {
1875                                                 BKE_reportf(reports, RPT_ERROR, "Camera \"%s\" is not a multi-view camera",
1876                                                             camera->id.name + 2);
1877                                                 return false;
1878                                         }
1879                                 }
1880                         }
1881                 }
1882         }
1883
1884         if (!active_view) {
1885                 BKE_reportf(reports, RPT_ERROR, "No active view found in scene \"%s\"", scene->id.name + 2);
1886                 return false;
1887         }
1888
1889         return true;
1890 }
1891
1892 static int check_valid_camera(Scene *scene, Object *camera_override, ReportList *reports)
1893 {
1894         const char *err_msg = "No camera found in scene \"%s\"";
1895
1896         if (camera_override == NULL && scene->camera == NULL)
1897                 scene->camera = BKE_view_layer_camera_find(BKE_view_layer_default_render(scene));
1898
1899         if (!check_valid_camera_multiview(scene, scene->camera, reports))
1900                 return false;
1901
1902         if (RE_seq_render_active(scene, &scene->r)) {
1903                 if (scene->ed) {
1904                         Sequence *seq = scene->ed->seqbase.first;
1905
1906                         while (seq) {
1907                                 if ((seq->type == SEQ_TYPE_SCENE) &&
1908                                     ((seq->flag & SEQ_SCENE_STRIPS) == 0) &&
1909                                     (seq->scene != NULL))
1910                                 {
1911                                         if (!seq->scene_camera) {
1912                                                 if (!seq->scene->camera &&
1913                                                     !BKE_view_layer_camera_find(BKE_view_layer_default_render(seq->scene)))
1914                                                 {
1915                                                         /* camera could be unneeded due to composite nodes */
1916                                                         Object *override = (seq->scene == scene) ? camera_override : NULL;
1917
1918                                                         if (!check_valid_compositing_camera(seq->scene, override)) {
1919                                                                 BKE_reportf(reports, RPT_ERROR, err_msg, seq->scene->id.name + 2);
1920                                                                 return false;
1921                                                         }
1922                                                 }
1923                                         }
1924                                         else if (!check_valid_camera_multiview(seq->scene, seq->scene_camera, reports))
1925                                                 return false;
1926                                 }
1927
1928                                 seq = seq->next;
1929                         }
1930                 }
1931         }
1932         else if (!check_valid_compositing_camera(scene, camera_override)) {
1933                 BKE_reportf(reports, RPT_ERROR, err_msg, scene->id.name + 2);
1934                 return false;
1935         }
1936
1937         return true;
1938 }
1939
1940 static bool node_tree_has_composite_output(bNodeTree *ntree)
1941 {
1942         bNode *node;
1943
1944         for (node = ntree->nodes.first; node; node = node->next) {
1945                 if (ELEM(node->type, CMP_NODE_COMPOSITE, CMP_NODE_OUTPUT_FILE)) {
1946                         return true;
1947                 }
1948                 else if (node->type == NODE_GROUP) {
1949                         if (node->id) {
1950                                 if (node_tree_has_composite_output((bNodeTree *)node->id)) {
1951                                         return true;
1952                                 }
1953                         }
1954                 }
1955         }
1956
1957         return false;
1958 }
1959
1960 static int check_composite_output(Scene *scene)
1961 {
1962         return node_tree_has_composite_output(scene->nodetree);
1963 }
1964
1965 bool RE_is_rendering_allowed(Scene *scene, ViewLayer *single_layer, Object *camera_override, ReportList *reports)
1966 {
1967         int scemode = check_mode_full_sample(&scene->r);
1968         
1969         if (scene->r.mode & R_BORDER) {
1970                 if (scene->r.border.xmax <= scene->r.border.xmin ||
1971                     scene->r.border.ymax <= scene->r.border.ymin)
1972                 {
1973                         BKE_report(reports, RPT_ERROR, "No border area selected");
1974                         return 0;
1975                 }
1976         }
1977         
1978         if (scemode & (R_EXR_TILE_FILE | R_FULL_SAMPLE)) {
1979                 char str[FILE_MAX];
1980                 
1981                 render_result_exr_file_path(scene, "", 0, str);
1982                 
1983                 if (!BLI_file_is_writable(str)) {
1984                         BKE_report(reports, RPT_ERROR, "Cannot save render buffers, check the temp default path");
1985                         return 0;
1986                 }
1987         }
1988         
1989         if (scemode & R_DOCOMP) {
1990                 if (scene->use_nodes) {
1991                         if (!scene->nodetree) {
1992                                 BKE_report(reports, RPT_ERROR, "No node tree in scene");
1993                                 return 0;
1994                         }
1995                         
1996                         if (!check_composite_output(scene)) {
1997                                 BKE_report(reports, RPT_ERROR, "No render output node in scene");
1998                                 return 0;
1999                         }
2000                         
2001                         if (scemode & R_FULL_SAMPLE) {
2002                                 if (composite_needs_render(scene, 0) == 0) {
2003                                         BKE_report(reports, RPT_ERROR, "Full sample AA not supported without 3D rendering");
2004                                         return 0;
2005                                 }
2006                         }
2007                 }
2008         }
2009         
2010         /* check valid camera, without camera render is OK (compo, seq) */
2011         if (!check_valid_camera(scene, camera_override, reports)) {
2012                 return 0;
2013         }
2014         
2015         /* get panorama & ortho, only after camera is set */
2016         BKE_camera_object_mode(&scene->r, camera_override ? camera_override : scene->camera);
2017
2018         /* forbidden combinations */
2019         if (scene->r.mode & R_PANORAMA) {
2020                 if (scene->r.mode & R_ORTHO) {
2021                         BKE_report(reports, RPT_ERROR, "No ortho render possible for panorama");
2022                         return 0;
2023                 }
2024
2025 #ifdef WITH_FREESTYLE
2026                 if (scene->r.mode & R_EDGE_FRS) {
2027                         BKE_report(reports, RPT_ERROR, "Panoramic camera not supported in Freestyle");
2028                         return 0;
2029                 }
2030 #endif
2031         }
2032
2033         if (RE_seq_render_active(scene, &scene->r)) {
2034                 if (scene->r.mode & R_BORDER) {
2035                         BKE_report(reports, RPT_ERROR, "Border rendering is not supported by sequencer");
2036                         return false;
2037                 }
2038         }
2039
2040         /* layer flag tests */
2041         if (!render_scene_has_layers_to_render(scene, single_layer)) {
2042                 BKE_report(reports, RPT_ERROR, "All render layers are disabled");
2043                 return 0;
2044         }
2045
2046         return 1;
2047 }
2048
2049 static void validate_render_settings(Render *re)
2050 {
2051         if (RE_engine_is_external(re)) {
2052                 /* not supported yet */
2053                 re->r.scemode &= ~(R_FULL_SAMPLE);
2054         }
2055 }
2056
2057 static void update_physics_cache(Render *re, Scene *scene, ViewLayer *view_layer, int UNUSED(anim_init))
2058 {
2059         PTCacheBaker baker;
2060
2061         memset(&baker, 0, sizeof(baker));
2062         baker.main = re->main;
2063         baker.scene = scene;
2064         baker.view_layer = view_layer;
2065         baker.depsgraph = BKE_scene_get_depsgraph(scene, view_layer, true);
2066         baker.bake = 0;
2067         baker.render = 1;
2068         baker.anim_init = 1;
2069         baker.quick_step = 1;
2070
2071         BKE_ptcache_bake(&baker);
2072 }
2073
2074 void RE_SetActiveRenderView(Render *re, const char *viewname)
2075 {
2076         BLI_strncpy(re->viewname, viewname, sizeof(re->viewname));
2077 }
2078
2079 const char *RE_GetActiveRenderView(Render *re)
2080 {
2081         return re->viewname;
2082 }
2083
2084 /* evaluating scene options for general Blender render */
2085 static int render_initialize_from_main(Render *re, RenderData *rd, Main *bmain, Scene *scene,
2086                                        ViewLayer *single_layer, Object *camera_override, unsigned int lay_override,
2087                                        int anim, int anim_init)
2088 {
2089         int winx, winy;
2090         rcti disprect;
2091         
2092         /* r.xsch and r.ysch has the actual view window size
2093          * r.border is the clipping rect */
2094         
2095         /* calculate actual render result and display size */
2096         winx = (rd->size * rd->xsch) / 100;
2097         winy = (rd->size * rd->ysch) / 100;
2098         
2099         /* we always render smaller part, inserting it in larger image is compositor bizz, it uses disprect for it */
2100         if (scene->r.mode & R_BORDER) {
2101                 disprect.xmin = rd->border.xmin * winx;
2102                 disprect.xmax = rd->border.xmax * winx;
2103                 
2104                 disprect.ymin = rd->border.ymin * winy;
2105                 disprect.ymax = rd->border.ymax * winy;
2106         }
2107         else {
2108                 disprect.xmin = disprect.ymin = 0;
2109                 disprect.xmax = winx;
2110                 disprect.ymax = winy;
2111         }
2112         
2113         re->main = bmain;
2114         re->scene = scene;
2115         re->camera_override = camera_override;
2116         re->lay = lay_override ? lay_override : scene->lay;
2117         re->layer_override = lay_override;
2118         re->i.localview = (re->lay & 0xFF000000) != 0;
2119         re->viewname[0] = '\0';
2120
2121         /* not too nice, but it survives anim-border render */
2122         if (anim) {
2123                 render_update_anim_renderdata(re, &scene->r, &scene->view_layers);
2124                 re->disprect = disprect;
2125                 return 1;
2126         }
2127         
2128         /* check all scenes involved */
2129         tag_scenes_for_render(re);
2130
2131 #ifdef DEPSGRAPH_WORKAROUND_GROUP_HACK
2132         /* Update group collections visibility. */
2133         tag_groups_for_render(re);
2134 #endif
2135
2136         /*
2137          * Disabled completely for now,
2138          * can be later set as render profile option
2139          * and default for background render.
2140          */
2141         if (0) {
2142                 /* make sure dynamics are up to date */
2143                 ViewLayer *view_layer = BKE_view_layer_context_active_PLACEHOLDER(scene);
2144                 update_physics_cache(re, scene, view_layer, anim_init);
2145         }
2146         
2147         if (single_layer || scene->r.scemode & R_SINGLE_LAYER) {
2148                 BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
2149                 render_result_single_layer_begin(re);
2150                 BLI_rw_mutex_unlock(&re->resultmutex);
2151         }
2152         
2153         RE_InitState(re, NULL, &scene->r, &scene->view_layers, single_layer, winx, winy, &disprect);
2154         if (!re->ok)  /* if an error was printed, abort */
2155                 return 0;
2156         
2157         /* initstate makes new result, have to send changed tags around */
2158         ntreeCompositTagRender(re->scene);
2159
2160         validate_render_settings(re);
2161
2162         re->display_init(re->dih, re->result);
2163         re->display_clear(re->dch, re->result);
2164         
2165         return 1;
2166 }
2167
2168 void RE_SetReports(Render *re, ReportList *reports)
2169 {
2170         re->reports = reports;
2171 }
2172
2173 /* general Blender frame render call */
2174 void RE_BlenderFrame(Render *re, Main *bmain, Scene *scene, ViewLayer *single_layer, Object *camera_override,
2175                      unsigned int lay_override, int frame, const bool write_still)
2176 {
2177         BLI_callback_exec(re->main, (ID *)scene, BLI_CB_EVT_RENDER_INIT);
2178
2179         /* ugly global still... is to prevent preview events and signal subsurfs etc to make full resol */
2180         G.is_rendering = true;
2181         
2182         scene->r.cfra = frame;
2183         
2184         if (render_initialize_from_main(re, &scene->r, bmain, scene, single_layer,
2185                                         camera_override, lay_override, 0, 0))
2186         {
2187                 MEM_reset_peak_memory();
2188
2189                 BLI_callback_exec(re->main, (ID *)scene, BLI_CB_EVT_RENDER_PRE);
2190
2191                 do_render_all_options(re);
2192
2193                 if (write_still && !G.is_break) {
2194                         if (BKE_imtype_is_movie(scene->r.im_format.imtype)) {
2195                                 /* operator checks this but in case its called from elsewhere */
2196                                 printf("Error: cant write single images with a movie format!\n");
2197                         }
2198                         else {
2199                                 char name[FILE_MAX];
2200                                 BKE_image_path_from_imformat(
2201                                         name, scene->r.pic, bmain->name, scene->r.cfra,
2202                                         &scene->r.im_format, (scene->r.scemode & R_EXTENSION) != 0, false, NULL);
2203
2204                                 /* reports only used for Movie */
2205                                 do_write_image_or_movie(re, bmain, scene, NULL, 0, name);
2206                         }
2207                 }
2208
2209                 BLI_callback_exec(re->main, (ID *)scene, BLI_CB_EVT_RENDER_POST); /* keep after file save */
2210                 if (write_still) {
2211                         BLI_callback_exec(re->main, (ID *)scene, BLI_CB_EVT_RENDER_WRITE);
2212                 }
2213         }
2214
2215         BLI_callback_exec(re->main, (ID *)scene, G.is_break ? BLI_CB_EVT_RENDER_CANCEL : BLI_CB_EVT_RENDER_COMPLETE);
2216
2217         /* UGLY WARNING */
2218         G.is_rendering = false;
2219 }
2220
2221 #ifdef WITH_FREESTYLE
2222 void RE_RenderFreestyleStrokes(Render *re, Main *bmain, Scene *scene, int render)
2223 {
2224         re->result_ok= 0;
2225         if (render_initialize_from_main(re, &scene->r, bmain, scene, NULL, NULL, scene->lay, 0, 0)) {
2226                 if (render)
2227                         do_render_3d(re);
2228         }
2229         re->result_ok = 1;
2230 }
2231
2232 void RE_RenderFreestyleExternal(Render *re)
2233 {
2234         if (!re->test_break(re->tbh)) {
2235                 RenderView *rv;
2236
2237                 init_freestyle(re);
2238
2239                 for (rv = re->result->views.first; rv; rv = rv->next) {
2240                         RE_SetActiveRenderView(re, rv->name);
2241
2242                         /* scene needs to be set to get camera */
2243                         Object *camera = RE_GetCamera(re);
2244
2245                         /* if no camera, viewmat should have been set! */
2246                         if (camera) {
2247                                 /* called before but need to call again in case of lens animation from the
2248                                  * above call to BKE_scene_graph_update_for_newframe, fixes bug. [#22702].
2249                                  * following calls don't depend on 'RE_SetCamera' */
2250                                 float mat[4][4];
2251
2252                                 RE_SetCamera(re, camera);
2253                                 RE_GetCameraModelMatrix(re, camera, mat);
2254                                 invert_m4(mat);
2255                                 RE_SetView(re, mat);
2256
2257                                 /* force correct matrix for scaled cameras */
2258                                 DEG_id_tag_update_ex(re->main, &camera->id, OB_RECALC_OB);
2259                         }
2260
2261                         printf("add freestyle\n");
2262
2263                         add_freestyle(re, 1);
2264                 }
2265         }
2266 }
2267 #endif
2268
2269 bool RE_WriteRenderViewsImage(ReportList *reports, RenderResult *rr, Scene *scene, const bool stamp, char *name)
2270 {
2271         bool ok = true;
2272         RenderData *rd = &scene->r;
2273
2274         if (!rr)
2275                 return false;
2276
2277         bool is_mono = BLI_listbase_count_at_most(&rr->views, 2) < 2;
2278         bool is_exr_rr = ELEM(rd->im_format.imtype, R_IMF_IMTYPE_OPENEXR, R_IMF_IMTYPE_MULTILAYER) &&
2279                          RE_HasFloatPixels(rr);
2280
2281         if (rd->im_format.views_format == R_IMF_VIEWS_MULTIVIEW && is_exr_rr)
2282         {
2283                 ok = RE_WriteRenderResult(reports, rr, name, &rd->im_format, NULL, -1);
2284                 render_print_save_message(reports, name, ok, errno);
2285         }
2286
2287         /* mono, legacy code */
2288         else if (is_mono || (rd->im_format.views_format == R_IMF_VIEWS_INDIVIDUAL))
2289         {
2290                 RenderView *rv;
2291                 int view_id;
2292                 char filepath[FILE_MAX];
2293
2294                 BLI_strncpy(filepath, name, sizeof(filepath));
2295
2296                 for (view_id = 0, rv = rr->views.first; rv; rv = rv->next, view_id++) {
2297                         if (!is_mono) {
2298                                 BKE_scene_multiview_view_filepath_get(&scene->r, filepath, rv->name, name);
2299                         }
2300
2301                         if (is_exr_rr) {
2302                                 ok = RE_WriteRenderResult(reports, rr, name, &rd->im_format, rv->name, -1);
2303                                 render_print_save_message(reports, name, ok, errno);
2304
2305                                 /* optional preview images for exr */
2306                                 if (ok && (rd->im_format.flag & R_IMF_FLAG_PREVIEW_JPG)) {
2307                                         ImageFormatData imf = rd->im_format;
2308                                         imf.imtype = R_IMF_IMTYPE_JPEG90;
2309
2310                                         if (BLI_testextensie(name, ".exr"))
2311                                                 name[strlen(name) - 4] = 0;
2312                                         BKE_image_path_ensure_ext_from_imformat(name, &imf);
2313
2314                                         ImBuf *ibuf = render_result_rect_to_ibuf(rr, rd, view_id);
2315                                         ibuf->planes = 24;
2316
2317                                         ok = render_imbuf_write_stamp_test(reports, scene, rr, ibuf, name, &imf, stamp);
2318
2319                                         IMB_freeImBuf(ibuf);
2320                                 }
2321                         }
2322                         else {
2323                                 ImBuf *ibuf = render_result_rect_to_ibuf(rr, rd, view_id);
2324
2325                                 IMB_colormanagement_imbuf_for_write(ibuf, true, false, &scene->view_settings,
2326                                                                     &scene->display_settings, &rd->im_format);
2327
2328                                 ok = render_imbuf_write_stamp_test(reports, scene, rr, ibuf, name, &rd->im_format, stamp);
2329
2330                                 /* imbuf knows which rects are not part of ibuf */
2331                                 IMB_freeImBuf(ibuf);
2332                         }
2333                 }
2334         }
2335         else { /* R_IMF_VIEWS_STEREO_3D */
2336                 BLI_assert(scene->r.im_format.views_format == R_IMF_VIEWS_STEREO_3D);
2337
2338                 if (rd->im_format.imtype == R_IMF_IMTYPE_MULTILAYER) {
2339                         printf("Stereo 3D not supported for MultiLayer image: %s\n", name);
2340                 }
2341                 else {
2342                         ImBuf *ibuf_arr[3] = {NULL};
2343                         const char *names[2] = {STEREO_LEFT_NAME, STEREO_RIGHT_NAME};
2344                         int i;
2345
2346                         for (i = 0; i < 2; i++) {
2347                                 int view_id = BLI_findstringindex(&rr->views, names[i], offsetof(RenderView, name));
2348                                 ibuf_arr[i] = render_result_rect_to_ibuf(rr, rd, view_id);
2349                                 IMB_colormanagement_imbuf_for_write(ibuf_arr[i], true, false, &scene->view_settings,
2350                                                                     &scene->display_settings, &scene->r.im_format);
2351                                 IMB_prepare_write_ImBuf(IMB_isfloat(ibuf_arr[i]), ibuf_arr[i]);
2352                         }
2353
2354                         ibuf_arr[2] = IMB_stereo3d_ImBuf(&scene->r.im_format, ibuf_arr[0], ibuf_arr[1]);
2355
2356                         ok = render_imbuf_write_stamp_test(reports, scene, rr, ibuf_arr[2], name, &rd->im_format, stamp);
2357
2358                         /* optional preview images for exr */
2359                         if (ok && is_exr_rr &&
2360                             (rd->im_format.flag & R_IMF_FLAG_PREVIEW_JPG))
2361                         {
2362                                 ImageFormatData imf = rd->im_format;
2363                                 imf.imtype = R_IMF_IMTYPE_JPEG90;
2364
2365                                 if (BLI_testextensie(name, ".exr"))
2366                                         name[strlen(name) - 4] = 0;
2367
2368                                 BKE_image_path_ensure_ext_from_imformat(name, &imf);
2369                                 ibuf_arr[2]->planes = 24;
2370
2371                                 ok = render_imbuf_write_stamp_test(reports, scene, rr, ibuf_arr[2], name, &rd->im_format, stamp);
2372                         }
2373
2374                         /* imbuf knows which rects are not part of ibuf */
2375                         for (i = 0; i < 3; i++) {
2376                                 IMB_freeImBuf(ibuf_arr[i]);
2377                         }
2378                 }
2379         }
2380
2381         return ok;
2382 }
2383
2384 bool RE_WriteRenderViewsMovie(
2385         ReportList *reports, RenderResult *rr, Scene *scene, RenderData *rd, bMovieHandle *mh,
2386         void **movie_ctx_arr, const int totvideos, bool preview)
2387 {
2388         bool is_mono;
2389         bool ok = true;
2390
2391         if (!rr)
2392                 return false;
2393
2394         is_mono = BLI_listbase_count_at_most(&rr->views, 2) < 2;
2395
2396         if (is_mono || (scene->r.im_format.views_format == R_IMF_VIEWS_INDIVIDUAL)) {
2397                 int view_id;
2398                 for (view_id = 0; view_id < totvideos; view_id++) {
2399                         const char *suffix = BKE_scene_multiview_view_id_suffix_get(&scene->r, view_id);
2400                         ImBuf *ibuf = render_result_rect_to_ibuf(rr, &scene->r, view_id);
2401
2402                         IMB_colormanagement_imbuf_for_write(ibuf, true, false, &scene->view_settings,
2403                                                             &scene->display_settings, &scene->r.im_format);
2404
2405                         ok &= mh->append_movie(movie_ctx_arr[view_id], rd, preview ? scene->r.psfra : scene->r.sfra, scene->r.cfra,
2406                                                (int *) ibuf->rect, ibuf->x, ibuf->y, suffix, reports);
2407
2408                         /* imbuf knows which rects are not part of ibuf */
2409                         IMB_freeImBuf(ibuf);
2410                 }
2411                 printf("Append frame %d\n", scene->r.cfra);
2412         }
2413         else { /* R_IMF_VIEWS_STEREO_3D */
2414                 const char *names[2] = {STEREO_LEFT_NAME, STEREO_RIGHT_NAME};
2415                 ImBuf *ibuf_arr[3] = {NULL};
2416                 int i;
2417
2418                 BLI_assert((totvideos == 1) && (scene->r.im_format.views_format == R_IMF_VIEWS_STEREO_3D));
2419
2420                 for (i = 0; i < 2; i++) {
2421                         int view_id = BLI_findstringindex(&rr->views, names[i], offsetof(RenderView, name));
2422                         ibuf_arr[i] = render_result_rect_to_ibuf(rr, &scene->r, view_id);
2423
2424                         IMB_colormanagement_imbuf_for_write(ibuf_arr[i], true, false, &scene->view_settings,
2425                                                             &scene->display_settings, &scene->r.im_format);
2426                 }
2427
2428                 ibuf_arr[2] = IMB_stereo3d_ImBuf(&scene->r.im_format, ibuf_arr[0], ibuf_arr[1]);
2429
2430                 ok = mh->append_movie(movie_ctx_arr[0], rd, preview ? scene->r.psfra : scene->r.sfra, scene->r.cfra, (int *) ibuf_arr[2]->rect,
2431                                       ibuf_arr[2]->x, ibuf_arr[2]->y, "", reports);
2432
2433                 for (i = 0; i < 3; i++) {
2434                         /* imbuf knows which rects are not part of ibuf */
2435                         IMB_freeImBuf(ibuf_arr[i]);
2436                 }
2437         }
2438
2439         return ok;
2440 }
2441
2442 static int do_write_image_or_movie(Render *re, Main *bmain, Scene *scene, bMovieHandle *mh, const int totvideos, const char *name_override)
2443 {
2444         char name[FILE_MAX];
2445         RenderResult rres;
2446         double render_time;
2447         bool ok = true;
2448
2449         RE_AcquireResultImageViews(re, &rres);
2450
2451         /* write movie or image */
2452         if (BKE_imtype_is_movie(scene->r.im_format.imtype)) {
2453                 RE_WriteRenderViewsMovie(re->reports, &rres, scene, &re->r, mh, re->movie_ctx_arr, totvideos, false);
2454         }
2455         else {
2456                 if (name_override)
2457                         BLI_strncpy(name, name_override, sizeof(name));
2458                 else
2459                         BKE_image_path_from_imformat(
2460                                 name, scene->r.pic, bmain->name, scene->r.cfra,
2461                                 &scene->r.im_format, (scene->r.scemode & R_EXTENSION) != 0, true, NULL);
2462
2463                 /* write images as individual images or stereo */
2464                 ok = RE_WriteRenderViewsImage(re->reports, &rres, scene, true, name);
2465         }
2466         
2467         RE_ReleaseResultImageViews(re, &rres);
2468
2469         render_time = re->i.lastframetime;
2470         re->i.lastframetime = PIL_check_seconds_timer() - re->i.starttime;
2471         
2472         BLI_timecode_string_from_time_simple(name, sizeof(name), re->i.lastframetime);
2473         printf(" Time: %s", name);
2474
2475         /* Flush stdout to be sure python callbacks are printing stuff after blender. */
2476         fflush(stdout);
2477
2478         BLI_callback_exec(G.main, NULL, BLI_CB_EVT_RENDER_STATS);
2479
2480         BLI_timecode_string_from_time_simple(name, sizeof(name), re->i.lastframetime - render_time);
2481         printf(" (Saving: %s)\n", name);
2482         
2483         fputc('\n', stdout);
2484         fflush(stdout); /* needed for renderd !! (not anymore... (ton)) */
2485
2486         return ok;
2487 }
2488
2489 static void get_videos_dimensions(
2490         Render *re, RenderData *rd,
2491         size_t *r_width, size_t *r_height)
2492 {
2493         size_t width, height;
2494         if (re->r.mode & R_BORDER) {
2495                 if ((re->r.mode & R_CROP) == 0) {
2496                         width = re->winx;
2497                         height = re->winy;
2498                 }
2499                 else {
2500                         width = re->rectx;
2501                         height = re->recty;
2502                 }
2503         }
2504         else {
2505                 width = re->rectx;
2506                 height = re->recty;
2507         }
2508
2509         BKE_scene_multiview_videos_dimensions_get(rd, width, height, r_width, r_height);
2510 }
2511
2512 static void re_movie_free_all(Render *re, bMovieHandle *mh, int totvideos)
2513 {
2514         int i;
2515
2516         for (i = 0; i < totvideos; i++) {
2517                 mh->end_movie(re->movie_ctx_arr[i]);
2518                 mh->context_free(re->movie_ctx_arr[i]);
2519         }
2520
2521         MEM_SAFE_FREE(re->movie_ctx_arr);
2522 }
2523
2524 /* saves images to disk */
2525 void RE_BlenderAnim(Render *re, Main *bmain, Scene *scene, Object *camera_override,
2526                     unsigned int lay_override, int sfra, int efra, int tfra)
2527 {
2528         RenderData rd = scene->r;
2529         bMovieHandle *mh = NULL;
2530         int cfrao = scene->r.cfra;
2531         int nfra, totrendered = 0, totskipped = 0;
2532         const int totvideos = BKE_scene_multiview_num_videos_get(&rd);
2533         const bool is_movie = BKE_imtype_is_movie(scene->r.im_format.imtype);
2534         const bool is_multiview_name = ((scene->r.scemode & R_MULTIVIEW) != 0 &&
2535                                         (scene->r.im_format.views_format == R_IMF_VIEWS_INDIVIDUAL));
2536
2537         BLI_callback_exec(re->main, (ID *)scene, BLI_CB_EVT_RENDER_INIT);
2538
2539         /* do not fully call for each frame, it initializes & pops output window */
2540         if (!render_initialize_from_main(re, &rd, bmain, scene, NULL, camera_override, lay_override, 0, 1))
2541                 return;
2542
2543         /* MULTIVIEW_TODO:
2544          * in case a new video format is added that implements get_next_frame multiview has to be addressed
2545          * or the error throwing for R_IMF_IMTYPE_FRAMESERVER has to be extended for those cases as well
2546          */
2547         if ((rd.im_format.imtype == R_IMF_IMTYPE_FRAMESERVER) && (totvideos > 1)) {
2548                 BKE_report(re->reports, RPT_ERROR, "Frame Server only support stereo output for multiview rendering");
2549                 return;
2550         }
2551
2552         if (is_movie) {
2553                 size_t width, height;
2554                 int i;
2555                 bool is_error = false;
2556
2557                 get_videos_dimensions(re, &rd, &width, &height);
2558
2559                 mh = BKE_movie_handle_get(scene->r.im_format.imtype);
2560                 if (mh == NULL) {
2561                         BKE_report(re->reports, RPT_ERROR, "Movie format unsupported");
2562                         return;
2563                 }
2564
2565                 re->movie_ctx_arr = MEM_mallocN(sizeof(void *) * totvideos, "Movies' Context");
2566
2567                 for (i = 0; i < totvideos; i++) {
2568                         const char *suffix = BKE_scene_multiview_view_id_suffix_get(&re->r, i);
2569
2570                         re->movie_ctx_arr[i] = mh->context_create();
2571
2572                         if (!mh->start_movie(re->movie_ctx_arr[i], scene, &re->r, width, height, re->reports, false, suffix)) {
2573                                 is_error = true;
2574                                 break;
2575                         }
2576                 }
2577
2578                 if (is_error) {
2579                         /* report is handled above */
2580                         re_movie_free_all(re, mh, i + 1);
2581                         return;
2582                 }
2583         }
2584
2585         /* ugly global still... is to prevent renderwin events and signal subsurfs etc to make full resol */
2586         /* is also set by caller renderwin.c */
2587         G.is_rendering = true;
2588
2589         re->flag |= R_ANIMATION;
2590
2591         {
2592                 for (nfra = sfra, scene->r.cfra = sfra; scene->r.cfra <= efra; scene->r.cfra++) {
2593                         char name[FILE_MAX];
2594
2595                         /* Special case for 'mh->get_next_frame'
2596                          * this overrides regular frame stepping logic */
2597                         if (mh && mh->get_next_frame) {
2598                                 while (G.is_break == false) {
2599                                         int nfra_test = mh->get_next_frame(re->movie_ctx_arr[0], &re->r, re->reports);
2600                                         if (nfra_test >= 0 && nfra_test >= sfra && nfra_test <= efra) {
2601                                                 nfra = nfra_test;
2602                                                 break;
2603                                         }
2604                                         else {
2605                                                 if (re->test_break(re->tbh)) {
2606                                                         G.is_break = true;
2607                                                 }
2608                                         }
2609                                 }
2610                         }
2611
2612                         /* Here is a feedback loop exists -- render initialization requires updated
2613                          * render layers settings which could be animated, but scene evaluation for
2614                          * the frame happens later because it depends on what layers are visible to
2615                          * render engine.
2616                          *
2617                          * The idea here is to only evaluate animation data associated with the scene,
2618                          * which will make sure render layer settings are up-to-date, initialize the
2619                          * render database itself and then perform full scene update with only needed
2620                          * layers.
2621                          *                                                              -sergey-
2622                          */
2623                         {
2624                                 float ctime = BKE_scene_frame_get(scene);
2625                                 AnimData *adt = BKE_animdata_from_id(&scene->id);
2626                                 BKE_animsys_evaluate_animdata(scene, &scene->id, adt, ctime, ADT_RECALC_ALL);
2627                         }
2628
2629                         /* only border now, todo: camera lens. (ton) */
2630                         render_initialize_from_main(re, &rd, bmain, scene,
2631                                                     NULL, camera_override, lay_override, 1, 0);
2632
2633                         if (nfra != scene->r.cfra) {
2634                                 /* Skip this frame, but could update for physics and particles system. */
2635                                 continue;
2636                         }
2637                         else
2638                                 nfra += tfra;
2639
2640                         /* Touch/NoOverwrite options are only valid for image's */
2641                         if (is_movie == false) {
2642                                 if (scene->r.mode & (R_NO_OVERWRITE | R_TOUCH))
2643                                         BKE_image_path_from_imformat(
2644                                                 name, scene->r.pic, bmain->name, scene->r.cfra,
2645                                                 &scene->r.im_format, (scene->r.scemode & R_EXTENSION) != 0, true, NULL);
2646
2647                                 if (scene->r.mode & R_NO_OVERWRITE) {
2648                                         if (!is_multiview_name) {
2649                                                 if (BLI_exists(name)) {
2650                                                         printf("skipping existing frame \"%s\"\n", name);
2651                                                         totskipped++;
2652                                                         continue;
2653                                                 }
2654                                         }
2655                                         else {
2656                                                 SceneRenderView *srv;
2657                                                 bool is_skip = false;
2658                                                 char filepath[FILE_MAX];
2659
2660                                                 for (srv = scene->r.views.first; srv; srv = srv->next) {
2661                                                         if (!BKE_scene_multiview_is_render_view_active(&scene->r, srv))
2662                                                                 continue;
2663
2664                                                         BKE_scene_multiview_filepath_get(srv, name, filepath);
2665
2666                                                         if (BLI_exists(filepath)) {
2667                                                                 is_skip = true;
2668                                                                 printf("skipping existing frame \"%s\" for view \"%s\"\n", filepath, srv->name);
2669                                                         }
2670                                                 }
2671
2672                                                 if (is_skip) {
2673                                                         totskipped++;
2674                                                         continue;
2675                                                 }
2676                                         }
2677                                 }
2678
2679                                 if (scene->r.mode & R_TOUCH) {
2680                                         if (!is_multiview_name) {
2681                                                 if (!BLI_exists(name)) {
2682                                                         BLI_make_existing_file(name); /* makes the dir if its not there */
2683                                                         BLI_file_touch(name);
2684                                                 }
2685                                         }
2686                                         else {
2687                                                 SceneRenderView *srv;
2688                                                 char filepath[FILE_MAX];
2689
2690                                                 for (srv = scene->r.views.first; srv; srv = srv->next) {
2691                                                         if (!BKE_scene_multiview_is_render_view_active(&scene->r, srv))
2692                                                                 continue;
2693
2694                                                         BKE_scene_multiview_filepath_get(srv, name, filepath);
2695
2696                                                         if (!BLI_exists(filepath)) {
2697                                                                 BLI_make_existing_file(filepath); /* makes the dir if its not there */
2698                                                                 BLI_file_touch(filepath);
2699                                                         }
2700                                                 }
2701                                         }
2702                                 }
2703                         }
2704
2705                         re->r.cfra = scene->r.cfra;     /* weak.... */
2706
2707                         /* run callbacs before rendering, before the scene is updated */
2708                         BLI_callback_exec(re->main, (ID *)scene, BLI_CB_EVT_RENDER_PRE);
2709
2710                         
2711                         do_render_all_options(re);
2712                         totrendered++;
2713                         
2714                         if (re->test_break(re->tbh) == 0) {
2715                                 if (!G.is_break)
2716                                         if (!do_write_image_or_movie(re, bmain, scene, mh, totvideos, NULL))
2717                                                 G.is_break = true;
2718                         }
2719                         else
2720                                 G.is_break = true;
2721                 
2722                         if (G.is_break == true) {
2723                                 /* remove touched file */
2724                                 if (is_movie == false) {
2725                                         if ((scene->r.mode & R_TOUCH)) {
2726                                                 if (!is_multiview_name) {
2727                                                         if ((BLI_file_size(name) == 0)) {
2728                                                                 /* BLI_exists(name) is implicit */
2729                                                                 BLI_delete(name, false, false);
2730                                                         }
2731                                                 }
2732                                                 else {
2733                                                         SceneRenderView *srv;
2734                                                         char filepath[FILE_MAX];
2735
2736                                                         for (srv = scene->r.views.first; srv; srv = srv->next) {
2737                                                                 if (!BKE_scene_multiview_is_render_view_active(&scene->r, srv))
2738                                                                         continue;
2739
2740                                                                 BKE_scene_multiview_filepath_get(srv, name, filepath);
2741
2742                                                                 if ((BLI_file_size(filepath) == 0)) {
2743                                                                         /* BLI_exists(filepath) is implicit */
2744                                                                         BLI_delete(filepath, false, false);
2745                                                                 }
2746                                                         }
2747                                                 }
2748                                         }
2749                                 }
2750                                 
2751                                 break;
2752                         }
2753
2754                         if (G.is_break == false) {
2755                                 BLI_callback_exec(re->main, (ID *)scene, BLI_CB_EVT_RENDER_POST); /* keep after file save */
2756                                 BLI_callback_exec(re->main, (ID *)scene, BLI_CB_EVT_RENDER_WRITE);
2757                         }
2758                 }
2759         }
2760         
2761         /* end movie */
2762         if (is_movie) {
2763                 re_movie_free_all(re, mh, totvideos);
2764         }
2765         
2766         if (totskipped && totrendered == 0)
2767                 BKE_report(re->reports, RPT_INFO, "No frames rendered, skipped to not overwrite");
2768
2769         scene->r.cfra = cfrao;
2770
2771         re->flag &= ~R_ANIMATION;
2772
2773         BLI_callback_exec(re->main, (ID *)scene, G.is_break ? BLI_CB_EVT_RENDER_CANCEL : BLI_CB_EVT_RENDER_COMPLETE);
2774         BKE_sound_reset_scene_specs(scene);
2775
2776         /* UGLY WARNING */
2777         G.is_rendering = false;
2778 }
2779
2780 void RE_PreviewRender(Render *re, Main *bmain, Scene *sce)
2781 {
2782         Object *camera;
2783         int winx, winy;
2784
2785         winx = (sce->r.size * sce->r.xsch) / 100;
2786         winy = (sce->r.size * sce->r.ysch) / 100;
2787
2788         RE_InitState(re, NULL, &sce->r, &sce->view_layers, NULL, winx, winy, NULL);
2789
2790         re->main = bmain;
2791         re->scene = sce;
2792         re->lay = sce->lay;
2793
2794         camera = RE_GetCamera(re);
2795         RE_SetCamera(re, camera);
2796
2797         do_render_3d(re);
2798 }
2799
2800 /* note; repeated win/disprect calc... solve that nicer, also in compo */
2801
2802 /* only the temp file! */
2803 bool RE_ReadRenderResult(Scene *scene, Scene *scenode)
2804 {
2805         Render *re;
2806         int winx, winy;
2807         bool success;
2808         rcti disprect;
2809         
2810         /* calculate actual render result and display size */
2811         winx = (scene->r.size * scene->r.xsch) / 100;
2812         winy = (scene->r.size * scene->r.ysch) / 100;
2813         
2814         /* only in movie case we render smaller part */
2815         if (scene->r.mode & R_BORDER) {
2816                 disprect.xmin = scene->r.border.xmin * winx;
2817                 disprect.xmax = scene->r.border.xmax * winx;
2818                 
2819                 disprect.ymin = scene->r.border.ymin * winy;
2820                 disprect.ymax = scene->r.border.ymax * winy;
2821         }
2822         else {
2823                 disprect.xmin = disprect.ymin = 0;
2824                 disprect.xmax = winx;
2825                 disprect.ymax = winy;
2826         }
2827         
2828         if (scenode)
2829                 scene = scenode;
2830         
2831         /* get render: it can be called from UI with draw callbacks */
2832         re = RE_GetSceneRender(scene);
2833         if (re == NULL)
2834                 re = RE_NewSceneRender(scene);
2835         RE_InitState(re, NULL, &scene->r, &scene->view_layers, NULL, winx, winy, &disprect);
2836         re->scene = scene;
2837         
2838         BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
2839         success = render_result_exr_file_cache_read(re);
2840         BLI_rw_mutex_unlock(&re->resultmutex);
2841
2842         render_result_uncrop(re);
2843
2844         return success;
2845 }
2846
2847 void RE_init_threadcount(Render *re) 
2848 {
2849         re->r.threads = BKE_render_num_threads(&re->r);
2850 }
2851
2852 /* loads in image into a result, size must match
2853  * x/y offsets are only used on a partial copy when dimensions don't match */
2854 void RE_layer_load_from_file(RenderLayer *layer, ReportList *reports, const char *filename, int x, int y)
2855 {
2856         /* OCIO_TODO: assume layer was saved in defaule color space */
2857         ImBuf *ibuf = IMB_loadiffname(filename, IB_rect, NULL);
2858         RenderPass *rpass = NULL;
2859
2860         /* multiview: since the API takes no 'view', we use the first combined pass found */
2861         for (rpass = layer->passes.first; rpass; rpass = rpass->next)
2862                 if (STREQ(rpass->name, RE_PASSNAME_COMBINED))
2863                         break;
2864
2865         if (rpass == NULL)
2866                 BKE_reportf(reports, RPT_ERROR, "%s: no Combined pass found in the render layer '%s'", __func__, filename);
2867
2868         if (ibuf && (ibuf->rect || ibuf->rect_float)) {
2869                 if (ibuf->x == layer->rectx && ibuf->y == layer->recty) {
2870                         if (ibuf->rect_float == NULL)
2871                                 IMB_float_from_rect(ibuf);
2872
2873                         memcpy(rpass->rect, ibuf->rect_float, sizeof(float) * 4 * layer->rectx * layer->recty);
2874                 }
2875                 else {
2876                         if ((ibuf->x - x >= layer->rectx) && (ibuf->y - y >= layer->recty)) {
2877                                 ImBuf *ibuf_clip;
2878
2879                                 if (ibuf->rect_float == NULL)
2880                                         IMB_float_from_rect(ibuf);
2881
2882                                 ibuf_clip = IMB_allocImBuf(layer->rectx, layer->recty, 32, IB_rectfloat);
2883                                 if (ibuf_clip) {
2884                                         IMB_rectcpy(ibuf_clip, ibuf, 0, 0, x, y, layer->rectx, layer->recty);
2885
2886                                         memcpy(rpass->rect, ibuf_clip->rect_float, sizeof(float) * 4 * layer->rectx * layer->recty);
2887                                         IMB_freeImBuf(ibuf_clip);
2888                                 }
2889                                 else {
2890                                         BKE_reportf(reports, RPT_ERROR, "%s: failed to allocate clip buffer '%s'", __func__, filename);
2891                                 }
2892                         }
2893                         else {
2894                                 BKE_reportf(reports, RPT_ERROR, "%s: incorrect dimensions for partial copy '%s'", __func__, filename);
2895                         }
2896                 }
2897
2898                 IMB_freeImBuf(ibuf);
2899         }
2900         else {
2901                 BKE_reportf(reports, RPT_ERROR, "%s: failed to load '%s'", __func__, filename);
2902         }
2903 }
2904
2905 void RE_result_load_from_file(RenderResult *result, ReportList *reports, const char *filename)
2906 {
2907         if (!render_result_exr_file_read_path(result, NULL, filename)) {
2908                 BKE_reportf(reports, RPT_ERROR, "%s: failed to load '%s'", __func__, filename);
2909                 return;
2910         }
2911 }
2912
2913 /* Used in the interface to decide whether to show layers or passes. */
2914 bool RE_layers_have_name(struct RenderResult *rr)
2915 {
2916         switch (BLI_listbase_count_at_most(&rr->layers, 2)) {
2917                 case 0:
2918                         return false;
2919                 case 1:
2920                         return (((RenderLayer *)rr->layers.first)->name[0] != '\0');
2921                 default:
2922                         return true;
2923         }
2924         return false;
2925 }
2926
2927 bool RE_passes_have_name(struct RenderLayer *rl)
2928 {
2929         for (RenderPass *rp = rl->passes.first; rp; rp = rp->next) {
2930                 if (!STREQ(rp->name, "Combined")) {
2931                         return true;
2932                 }
2933         }
2934
2935         return false;
2936 }
2937
2938 RenderPass *RE_pass_find_by_name(volatile RenderLayer *rl, const char *name, const char *viewname)
2939 {
2940         RenderPass *rp = NULL;
2941
2942         for (rp = rl->passes.last; rp; rp = rp->prev) {
2943                 if (STREQ(rp->name, name)) {
2944                         if (viewname == NULL || viewname[0] == '\0')
2945                                 break;
2946                         else if (STREQ(rp->view, viewname))
2947                                 break;
2948                 }
2949         }
2950         return rp;
2951 }
2952
2953 /* Only provided for API compatibility, don't use this in new code! */
2954 RenderPass *RE_pass_find_by_type(volatile RenderLayer *rl, int passtype, const char *viewname)
2955 {
2956 #define CHECK_PASS(NAME) \
2957         if (passtype == SCE_PASS_ ## NAME) \
2958                 return RE_pass_find_by_name(rl, RE_PASSNAME_ ## NAME, viewname);
2959
2960         CHECK_PASS(COMBINED);
2961         CHECK_PASS(Z);
2962         CHECK_PASS(VECTOR);
2963         CHECK_PASS(NORMAL);
2964         CHECK_PASS(UV);
2965         CHECK_PASS(RGBA);
2966         CHECK_PASS(EMIT);
2967         CHECK_PASS(DIFFUSE);
2968         CHECK_PASS(SPEC);
2969         CHECK_PASS(SHADOW);
2970         CHECK_PASS(AO);
2971         CHECK_PASS(ENVIRONMENT);
2972         CHECK_PASS(INDIRECT);
2973         CHECK_PASS(REFLECT);
2974         CHECK_PASS(REFRACT);
2975         CHECK_PASS(INDEXOB);
2976         CHECK_PASS(INDEXMA);
2977         CHECK_PASS(MIST);
2978         CHECK_PASS(RAYHITS);
2979         CHECK_PASS(DIFFUSE_DIRECT);
2980         CHECK_PASS(DIFFUSE_INDIRECT);
2981         CHECK_PASS(DIFFUSE_COLOR);
2982         CHECK_PASS(GLOSSY_DIRECT);
2983         CHECK_PASS(GLOSSY_INDIRECT);
2984         CHECK_PASS(GLOSSY_COLOR);
2985         CHECK_PASS(TRANSM_DIRECT);
2986         CHECK_PASS(TRANSM_INDIRECT);
2987         CHECK_PASS(TRANSM_COLOR);
2988         CHECK_PASS(SUBSURFACE_DIRECT);
2989         CHECK_PASS(SUBSURFACE_INDIRECT);
2990         CHECK_PASS(SUBSURFACE_COLOR);
2991
2992 #undef CHECK_PASS
2993
2994         return NULL;
2995 }
2996
2997 /* create a renderlayer and renderpass for grease pencil layer */
2998 RenderPass *RE_create_gp_pass(RenderResult *rr, const char *layername, const char *viewname)
2999 {
3000         RenderLayer *rl = BLI_findstring(&rr->layers, layername, offsetof(RenderLayer, name));
3001         /* only create render layer if not exist */
3002         if (!rl) {
3003                 rl = MEM_callocN(sizeof(RenderLayer), layername);
3004                 BLI_addtail(&rr->layers, rl);
3005                 BLI_strncpy(rl->name, layername, sizeof(rl->name));
3006                 rl->layflag = SCE_LAY_SOLID;
3007                 rl->passflag = SCE_PASS_COMBINED;
3008                 rl->rectx = rr->rectx;
3009                 rl->recty = rr->recty;
3010         }
3011         
3012         /* clear previous pass if exist or the new image will be over previous one*/
3013         RenderPass *rp = RE_pass_find_by_name(rl, RE_PASSNAME_COMBINED, viewname);
3014         if (rp) {
3015                 if (rp->rect) {
3016                         MEM_freeN(rp->rect);
3017                 }
3018                 BLI_freelinkN(&rl->passes, rp);
3019         }
3020         /* create a totally new pass */
3021         return gp_add_pass(rr, rl, 4, RE_PASSNAME_COMBINED, viewname);
3022 }