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