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