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