Merging r48538 through r48544 from trunk into soc-2011-tomato
[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 "BKE_animsys.h"  /* <------ should this be here?, needed for sequencer update */
49 #include "BKE_camera.h"
50 #include "BKE_global.h"
51 #include "BKE_image.h"
52 #include "BKE_main.h"
53 #include "BKE_node.h"
54 #include "BKE_pointcache.h"
55 #include "BKE_report.h"
56 #include "BKE_scene.h"
57 #include "BKE_sequencer.h"
58 #include "BKE_utildefines.h"
59 #include "BKE_writeavi.h"  /* <------ should be replaced once with generic movie module */
60
61 #include "BLI_math.h"
62 #include "BLI_listbase.h"
63 #include "BLI_string.h"
64 #include "BLI_path_util.h"
65 #include "BLI_fileops.h"
66 #include "BLI_rand.h"
67 #include "BLI_callbacks.h"
68
69 #include "PIL_time.h"
70 #include "IMB_colormanagement.h"
71 #include "IMB_imbuf.h"
72 #include "IMB_imbuf_types.h"
73
74 #include "RE_engine.h"
75 #include "RE_pipeline.h"
76
77 /* internal */
78 #include "render_result.h"
79 #include "render_types.h"
80 #include "renderpipeline.h"
81 #include "renderdatabase.h"
82 #include "rendercore.h"
83 #include "initrender.h"
84 #include "shadbuf.h"
85 #include "pixelblending.h"
86 #include "zbuf.h"
87
88 /* render flow
89  *
90  * 1) Initialize state
91  * - state data, tables
92  * - movie/image file init
93  * - everything that doesn't change during animation
94  *
95  * 2) Initialize data
96  * - camera, world, matrices
97  * - make render verts, faces, halos, strands
98  * - everything can change per frame/field
99  *
100  * 3) Render Processor
101  * - multiple layers
102  * - tiles, rect, baking
103  * - layers/tiles optionally to disk or directly in Render Result
104  *
105  * 4) Composite Render Result
106  * - also read external files etc
107  *
108  * 5) Image Files
109  * - save file or append in movie
110  *
111  */
112
113
114 /* ********* globals ******** */
115
116 /* here we store all renders */
117 static struct {
118         ListBase renderlist;
119
120         /* commandline thread override */
121         int threads;
122 } RenderGlobal = {{NULL, NULL}, -1}; 
123
124 /* hardcopy of current render, used while rendering for speed */
125 Render R;
126
127 /* ********* alloc and free ******** */
128
129 static int do_write_image_or_movie(Render *re, Main *bmain, Scene *scene, bMovieHandle *mh, const char *name_override);
130
131 static volatile int g_break = 0;
132 static int thread_break(void *UNUSED(arg))
133 {
134         return g_break;
135 }
136
137 /* default callbacks, set in each new render */
138 static void result_nothing(void *UNUSED(arg), RenderResult *UNUSED(rr)) {}
139 static void result_rcti_nothing(void *UNUSED(arg), RenderResult *UNUSED(rr), volatile struct rcti *UNUSED(rect)) {}
140 static void stats_nothing(void *UNUSED(arg), RenderStats *UNUSED(rs)) {}
141 static void float_nothing(void *UNUSED(arg), float UNUSED(val)) {}
142 static int default_break(void *UNUSED(arg)) {return G.afbreek == 1;}
143
144 static void stats_background(void *UNUSED(arg), RenderStats *rs)
145 {
146         uintptr_t mem_in_use, mmap_in_use, peak_memory;
147         float megs_used_memory, mmap_used_memory, megs_peak_memory;
148
149         mem_in_use = MEM_get_memory_in_use();
150         mmap_in_use = MEM_get_mapped_memory_in_use();
151         peak_memory = MEM_get_peak_memory();
152
153         megs_used_memory = (mem_in_use - mmap_in_use) / (1024.0 * 1024.0);
154         mmap_used_memory = (mmap_in_use) / (1024.0 * 1024.0);
155         megs_peak_memory = (peak_memory) / (1024.0 * 1024.0);
156
157         fprintf(stdout, "Fra:%d Mem:%.2fM (%.2fM, peak %.2fM) ", rs->cfra,
158                 megs_used_memory, mmap_used_memory, megs_peak_memory);
159
160         if (rs->curfield)
161                 fprintf(stdout, "Field %d ", rs->curfield);
162         if (rs->curblur)
163                 fprintf(stdout, "Blur %d ", rs->curblur);
164
165         if (rs->infostr) {
166                 fprintf(stdout, "| %s", rs->infostr);
167         }
168         else {
169                 if (rs->tothalo)
170                         fprintf(stdout, "Sce: %s Ve:%d Fa:%d Ha:%d La:%d", rs->scenename, rs->totvert, rs->totface, rs->tothalo, rs->totlamp);
171                 else
172                         fprintf(stdout, "Sce: %s Ve:%d Fa:%d La:%d", rs->scenename, rs->totvert, rs->totface, rs->totlamp);
173         }
174
175         BLI_callback_exec(G.main, NULL, BLI_CB_EVT_RENDER_STATS);
176
177         fputc('\n', stdout);
178         fflush(stdout);
179 }
180
181 void RE_FreeRenderResult(RenderResult *res)
182 {
183         render_result_free(res);
184 }
185
186 float *RE_RenderLayerGetPass(RenderLayer *rl, int passtype)
187 {
188         RenderPass *rpass;
189         
190         for (rpass = rl->passes.first; rpass; rpass = rpass->next)
191                 if (rpass->passtype == passtype)
192                         return rpass->rect;
193         return NULL;
194 }
195
196 RenderLayer *RE_GetRenderLayer(RenderResult *rr, const char *name)
197 {
198         RenderLayer *rl;
199         
200         if (rr == NULL) return NULL;
201         
202         for (rl = rr->layers.first; rl; rl = rl->next)
203                 if (strncmp(rl->name, name, RE_MAXNAME) == 0)
204                         return rl;
205         return NULL;
206 }
207
208 RenderResult *RE_MultilayerConvert(void *exrhandle, int rectx, int recty)
209 {
210         return render_result_new_from_exr(exrhandle, rectx, recty);
211 }
212
213 RenderLayer *render_get_active_layer(Render *re, RenderResult *rr)
214 {
215         RenderLayer *rl = BLI_findlink(&rr->layers, re->r.actlay);
216         
217         if (rl)
218                 return rl;
219         else 
220                 return rr->layers.first;
221 }
222
223 static int render_scene_needs_vector(Render *re)
224 {
225         SceneRenderLayer *srl;
226         
227         for (srl = re->scene->r.layers.first; srl; srl = srl->next)
228                 if (!(srl->layflag & SCE_LAY_DISABLE))
229                         if (srl->passflag & SCE_PASS_VECTOR)
230                                 return 1;
231
232         return 0;
233 }
234
235 /* *************************************************** */
236
237 Render *RE_GetRender(const char *name)
238 {
239         Render *re;
240
241         /* search for existing renders */
242         for (re = RenderGlobal.renderlist.first; re; re = re->next)
243                 if (strncmp(re->name, name, RE_MAXNAME) == 0)
244                         break;
245
246         return re;
247 }
248
249 Render *RE_GetRender_FromData(const RenderData *rd)
250 {
251         Render *re;
252
253         /* search for existing renders */
254         for (re = RenderGlobal.renderlist.first; re; re = re->next)
255                 if (&re->r == rd)
256                         break;
257
258         return re;
259 }
260
261 /* if you want to know exactly what has been done */
262 RenderResult *RE_AcquireResultRead(Render *re)
263 {
264         if (re) {
265                 BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_READ);
266                 return re->result;
267         }
268
269         return NULL;
270 }
271
272 RenderResult *RE_AcquireResultWrite(Render *re)
273 {
274         if (re) {
275                 BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
276                 return re->result;
277         }
278
279         return NULL;
280 }
281
282 void RE_SwapResult(Render *re, RenderResult **rr)
283 {
284         /* for keeping render buffers */
285         if (re) {
286                 SWAP(RenderResult *, re->result, *rr);
287         }
288 }
289
290
291 void RE_ReleaseResult(Render *re)
292 {
293         if (re)
294                 BLI_rw_mutex_unlock(&re->resultmutex);
295 }
296
297 /* displist.c util.... */
298 Scene *RE_GetScene(Render *re)
299 {
300         if (re)
301                 return re->scene;
302         return NULL;
303 }
304
305 /* fill provided result struct with what's currently active or done */
306 void RE_AcquireResultImage(Render *re, RenderResult *rr)
307 {
308         memset(rr, 0, sizeof(RenderResult));
309
310         if (re) {
311                 BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_READ);
312
313                 if (re->result) {
314                         RenderLayer *rl;
315                         
316                         rr->rectx = re->result->rectx;
317                         rr->recty = re->result->recty;
318                         
319                         rr->rectf = re->result->rectf;
320                         rr->rectz = re->result->rectz;
321                         rr->rect32 = re->result->rect32;
322                         
323                         /* active layer */
324                         rl = render_get_active_layer(re, re->result);
325
326                         if (rl) {
327                                 if (rr->rectf == NULL)
328                                         rr->rectf = rl->rectf;
329                                 if (rr->rectz == NULL)
330                                         rr->rectz = RE_RenderLayerGetPass(rl, SCE_PASS_Z);
331                         }
332
333                         rr->have_combined = (re->result->rectf != NULL);
334                         rr->layers = re->result->layers;
335                 }
336         }
337 }
338
339 void RE_ReleaseResultImage(Render *re)
340 {
341         if (re)
342                 BLI_rw_mutex_unlock(&re->resultmutex);
343 }
344
345 /* caller is responsible for allocating rect in correct size! */
346 void RE_ResultGet32(Render *re, unsigned int *rect)
347 {
348         RenderResult rres;
349         
350         RE_AcquireResultImage(re, &rres);
351         render_result_rect_get_pixels(&rres, &re->r, rect, re->rectx, re->recty);
352         RE_ReleaseResultImage(re);
353 }
354
355 RenderStats *RE_GetStats(Render *re)
356 {
357         return &re->i;
358 }
359
360 Render *RE_NewRender(const char *name)
361 {
362         Render *re;
363
364         /* only one render per name exists */
365         re = RE_GetRender(name);
366         if (re == NULL) {
367                 
368                 /* new render data struct */
369                 re = MEM_callocN(sizeof(Render), "new render");
370                 BLI_addtail(&RenderGlobal.renderlist, re);
371                 BLI_strncpy(re->name, name, RE_MAXNAME);
372                 BLI_rw_mutex_init(&re->resultmutex);
373         }
374         
375         RE_InitRenderCB(re);
376
377         /* init some variables */
378         re->ycor = 1.0f;
379         
380         return re;
381 }
382
383 /* called for new renders and when finishing rendering so
384  * we always have valid callbacks on a render */
385 void RE_InitRenderCB(Render *re)
386 {
387         /* set default empty callbacks */
388         re->display_init = result_nothing;
389         re->display_clear = result_nothing;
390         re->display_draw = result_rcti_nothing;
391         re->progress = float_nothing;
392         re->test_break = default_break;
393         if (G.background)
394                 re->stats_draw = stats_background;
395         else
396                 re->stats_draw = stats_nothing;
397         /* clear callback handles */
398         re->dih = re->dch = re->ddh = re->sdh = re->prh = re->tbh = NULL;
399 }
400
401 /* only call this while you know it will remove the link too */
402 void RE_FreeRender(Render *re)
403 {
404         BLI_rw_mutex_end(&re->resultmutex);
405         
406         free_renderdata_tables(re);
407         free_sample_tables(re);
408         
409         render_result_free(re->result);
410         render_result_free(re->pushedresult);
411         
412         BLI_remlink(&RenderGlobal.renderlist, re);
413         MEM_freeN(re);
414 }
415
416 /* exit blender */
417 void RE_FreeAllRender(void)
418 {
419         while (RenderGlobal.renderlist.first) {
420                 RE_FreeRender(RenderGlobal.renderlist.first);
421         }
422 }
423
424 /* on file load, free all re */
425 void RE_FreeAllRenderResults(void)
426 {
427         Render *re;
428
429         for (re = RenderGlobal.renderlist.first; re; re = re->next) {
430                 render_result_free(re->result);
431                 render_result_free(re->pushedresult);
432
433                 re->result = NULL;
434                 re->pushedresult = NULL;
435         }
436 }
437
438 /* ********* initialize state ******** */
439
440
441 /* what doesn't change during entire render sequence */
442 /* disprect is optional, if NULL it assumes full window render */
443 void RE_InitState(Render *re, Render *source, RenderData *rd, SceneRenderLayer *srl, int winx, int winy, rcti *disprect)
444 {
445         re->ok = TRUE;   /* maybe flag */
446         
447         re->i.starttime = PIL_check_seconds_timer();
448         re->r = *rd;     /* hardcopy */
449         
450         re->winx = winx;
451         re->winy = winy;
452         if (disprect) {
453                 re->disprect = *disprect;
454                 re->rectx = disprect->xmax - disprect->xmin;
455                 re->recty = disprect->ymax - disprect->ymin;
456         }
457         else {
458                 re->disprect.xmin = re->disprect.ymin = 0;
459                 re->disprect.xmax = winx;
460                 re->disprect.ymax = winy;
461                 re->rectx = winx;
462                 re->recty = winy;
463         }
464         
465         if (re->rectx < 2 || re->recty < 2 || (BKE_imtype_is_movie(rd->im_format.imtype) &&
466                                                (re->rectx < 16 || re->recty < 16) ))
467         {
468                 BKE_report(re->reports, RPT_ERROR, "Image too small");
469                 re->ok = 0;
470                 return;
471         }
472
473         if ((re->r.mode & (R_OSA)) == 0)
474                 re->r.scemode &= ~R_FULL_SAMPLE;
475
476 #ifdef WITH_OPENEXR
477         if (re->r.scemode & R_FULL_SAMPLE)
478                 re->r.scemode |= R_EXR_TILE_FILE;   /* enable automatic */
479
480         /* Until use_border is made compatible with save_buffers/full_sample, render without the later instead of not rendering at all.*/
481         if (re->r.mode & R_BORDER) {
482                 re->r.scemode &= ~(R_EXR_TILE_FILE | R_FULL_SAMPLE);
483         }
484
485 #else
486         /* can't do this without openexr support */
487         re->r.scemode &= ~(R_EXR_TILE_FILE | R_FULL_SAMPLE);
488 #endif
489         
490         /* fullsample wants uniform osa levels */
491         if (source && (re->r.scemode & R_FULL_SAMPLE)) {
492                 /* but, if source has no full sample we disable it */
493                 if ((source->r.scemode & R_FULL_SAMPLE) == 0)
494                         re->r.scemode &= ~R_FULL_SAMPLE;
495                 else
496                         re->r.osa = re->osa = source->osa;
497         }
498         else {
499                 /* check state variables, osa? */
500                 if (re->r.mode & (R_OSA)) {
501                         re->osa = re->r.osa;
502                         if (re->osa > 16) re->osa = 16;
503                 }
504                 else re->osa = 0;
505         }
506         
507         if (srl) {
508                 int index = BLI_findindex(&re->r.layers, srl);
509                 if (index != -1) {
510                         re->r.actlay = index;
511                         re->r.scemode |= R_SINGLE_LAYER;
512                 }
513         }
514                 
515         /* always call, checks for gamma, gamma tables and jitter too */
516         make_sample_tables(re); 
517         
518         /* if preview render, we try to keep old result */
519         BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
520
521         if (re->r.scemode & R_PREVIEWBUTS) {
522                 if (re->result && re->result->rectx == re->rectx && re->result->recty == re->recty) ;
523                 else {
524                         render_result_free(re->result);
525                         re->result = NULL;
526                 }
527         }
528         else {
529                 
530                 /* make empty render result, so display callbacks can initialize */
531                 render_result_free(re->result);
532                 re->result = MEM_callocN(sizeof(RenderResult), "new render result");
533                 re->result->rectx = re->rectx;
534                 re->result->recty = re->recty;
535         }
536
537         BLI_rw_mutex_unlock(&re->resultmutex);
538         
539         /* we clip faces with a minimum of 2 pixel boundary outside of image border. see zbuf.c */
540         re->clipcrop = 1.0f + 2.0f / (float)(re->winx > re->winy ? re->winy : re->winx);
541         
542         re->mblur_offs = re->field_offs = 0.f;
543         
544         RE_init_threadcount(re);
545 }
546
547 void RE_SetWindow(Render *re, rctf *viewplane, float clipsta, float clipend)
548 {
549         /* re->ok flag? */
550         
551         re->viewplane = *viewplane;
552         re->clipsta = clipsta;
553         re->clipend = clipend;
554         re->r.mode &= ~R_ORTHO;
555
556         perspective_m4(re->winmat,
557                        re->viewplane.xmin, re->viewplane.xmax,
558                        re->viewplane.ymin, re->viewplane.ymax, re->clipsta, re->clipend);
559         
560 }
561
562 void RE_SetOrtho(Render *re, rctf *viewplane, float clipsta, float clipend)
563 {
564         /* re->ok flag? */
565         
566         re->viewplane = *viewplane;
567         re->clipsta = clipsta;
568         re->clipend = clipend;
569         re->r.mode |= R_ORTHO;
570
571         orthographic_m4(re->winmat,
572                         re->viewplane.xmin, re->viewplane.xmax,
573                         re->viewplane.ymin, re->viewplane.ymax, re->clipsta, re->clipend);
574 }
575
576 void RE_SetView(Render *re, float mat[][4])
577 {
578         /* re->ok flag? */
579         copy_m4_m4(re->viewmat, mat);
580         invert_m4_m4(re->viewinv, re->viewmat);
581 }
582
583 /* image and movie output has to move to either imbuf or kernel */
584 void RE_display_init_cb(Render *re, void *handle, void (*f)(void *handle, RenderResult *rr))
585 {
586         re->display_init = f;
587         re->dih = handle;
588 }
589 void RE_display_clear_cb(Render *re, void *handle, void (*f)(void *handle, RenderResult *rr))
590 {
591         re->display_clear = f;
592         re->dch = handle;
593 }
594 void RE_display_draw_cb(Render *re, void *handle, void (*f)(void *handle, RenderResult *rr, volatile rcti *rect))
595 {
596         re->display_draw = f;
597         re->ddh = handle;
598 }
599 void RE_stats_draw_cb(Render *re, void *handle, void (*f)(void *handle, RenderStats *rs))
600 {
601         re->stats_draw = f;
602         re->sdh = handle;
603 }
604 void RE_progress_cb(Render *re, void *handle, void (*f)(void *handle, float))
605 {
606         re->progress = f;
607         re->prh = handle;
608 }
609
610 void RE_draw_lock_cb(Render *re, void *handle, void (*f)(void *handle, int i))
611 {
612         re->draw_lock = f;
613         re->tbh = handle;
614 }
615
616 void RE_test_break_cb(Render *re, void *handle, int (*f)(void *handle))
617 {
618         re->test_break = f;
619         re->tbh = handle;
620 }
621
622
623 /* ********* add object data (later) ******** */
624
625 /* object is considered fully prepared on correct time etc */
626 /* includes lights */
627 #if 0
628 void RE_AddObject(Render *UNUSED(re), Object *UNUSED(ob))
629 {
630         
631 }
632 #endif
633
634 /* *************************************** */
635
636 static int render_display_draw_enabled(Render *re)
637 {
638         /* don't show preprocess for previewrender sss */
639         if (re->sss_points)
640                 return !(re->r.scemode & R_PREVIEWBUTS);
641         else
642                 return 1;
643 }
644
645 /* the main thread call, renders an entire part */
646 static void *do_part_thread(void *pa_v)
647 {
648         RenderPart *pa = pa_v;
649         
650         /* need to return nicely all parts on esc */
651         if (R.test_break(R.tbh) == 0) {
652                 
653                 if (!R.sss_points && (R.r.scemode & R_FULL_SAMPLE))
654                         pa->result = render_result_new_full_sample(&R, &pa->fullresult, &pa->disprect, pa->crop, RR_USE_MEM);
655                 else
656                         pa->result = render_result_new(&R, &pa->disprect, pa->crop, RR_USE_MEM, RR_ALL_LAYERS);
657
658                 if (R.sss_points)
659                         zbufshade_sss_tile(pa);
660                 else if (R.osa)
661                         zbufshadeDA_tile(pa);
662                 else
663                         zbufshade_tile(pa);
664                 
665                 /* merge too on break! */
666                 if (R.result->do_exr_tile) {
667                         render_result_exr_file_merge(R.result, pa->result);
668                 }
669                 else if (render_display_draw_enabled(&R)) {
670                         /* on break, don't merge in result for preview renders, looks nicer */
671                         if (R.test_break(R.tbh) && (R.r.scemode & R_PREVIEWBUTS)) ;
672                         else render_result_merge(R.result, pa->result);
673                 }
674         }
675         
676         pa->ready = 1;
677         
678         return NULL;
679 }
680
681 /* calculus for how much 1 pixel rendered should rotate the 3d geometry */
682 /* is not that simple, needs to be corrected for errors of larger viewplane sizes */
683 /* called in initrender.c, initparts() and convertblender.c, for speedvectors */
684 float panorama_pixel_rot(Render *re)
685 {
686         float psize, phi, xfac;
687         float borderfac = (float)(re->disprect.xmax - re->disprect.xmin) / (float)re->winx;
688         
689         /* size of 1 pixel mapped to viewplane coords */
690         psize = (re->viewplane.xmax - re->viewplane.xmin) / (float)(re->winx);
691         /* angle of a pixel */
692         phi = atan(psize / re->clipsta);
693         
694         /* correction factor for viewplane shifting, first calculate how much the viewplane angle is */
695         xfac = borderfac * ((re->viewplane.xmax - re->viewplane.xmin)) / (float)re->xparts;
696         xfac = atan(0.5f * xfac / re->clipsta);
697         /* and how much the same viewplane angle is wrapped */
698         psize = 0.5f * phi * ((float)re->partx);
699         
700         /* the ratio applied to final per-pixel angle */
701         phi *= xfac / psize;
702         
703         return phi;
704 }
705
706 /* call when all parts stopped rendering, to find the next Y slice */
707 /* if slice found, it rotates the dbase */
708 static RenderPart *find_next_pano_slice(Render *re, int *minx, rctf *viewplane)
709 {
710         RenderPart *pa, *best = NULL;
711         
712         *minx = re->winx;
713         
714         /* most left part of the non-rendering parts */
715         for (pa = re->parts.first; pa; pa = pa->next) {
716                 if (pa->ready == 0 && pa->nr == 0) {
717                         if (pa->disprect.xmin < *minx) {
718                                 best = pa;
719                                 *minx = pa->disprect.xmin;
720                         }
721                 }
722         }
723                         
724         if (best) {
725                 float phi = panorama_pixel_rot(re);
726
727                 R.panodxp = (re->winx - (best->disprect.xmin + best->disprect.xmax) ) / 2;
728                 R.panodxv = ((viewplane->xmax - viewplane->xmin) * R.panodxp) / (float)(re->winx);
729
730                 /* shift viewplane */
731                 R.viewplane.xmin = viewplane->xmin + R.panodxv;
732                 R.viewplane.xmax = viewplane->xmax + R.panodxv;
733                 RE_SetWindow(re, &R.viewplane, R.clipsta, R.clipend);
734                 copy_m4_m4(R.winmat, re->winmat);
735                 
736                 /* rotate database according to part coordinates */
737                 project_renderdata(re, projectverto, 1, -R.panodxp * phi, 1);
738                 R.panosi = sin(R.panodxp * phi);
739                 R.panoco = cos(R.panodxp * phi);
740         }
741         return best;
742 }
743
744 static RenderPart *find_next_part(Render *re, int minx)
745 {
746         RenderPart *pa, *best = NULL;
747
748         /* long long int's needed because of overflow [#24414] */
749         long long int centx = re->winx / 2, centy = re->winy / 2, tot = 1;
750         long long int mindist = (long long int)re->winx * (long long int)re->winy;
751         
752         /* find center of rendered parts, image center counts for 1 too */
753         for (pa = re->parts.first; pa; pa = pa->next) {
754                 if (pa->ready) {
755                         centx += (pa->disprect.xmin + pa->disprect.xmax) / 2;
756                         centy += (pa->disprect.ymin + pa->disprect.ymax) / 2;
757                         tot++;
758                 }
759         }
760         centx /= tot;
761         centy /= tot;
762         
763         /* closest of the non-rendering parts */
764         for (pa = re->parts.first; pa; pa = pa->next) {
765                 if (pa->ready == 0 && pa->nr == 0) {
766                         long long int distx = centx - (pa->disprect.xmin + pa->disprect.xmax) / 2;
767                         long long int disty = centy - (pa->disprect.ymin + pa->disprect.ymax) / 2;
768                         distx = (long long int)sqrt(distx * distx + disty * disty);
769                         if (distx < mindist) {
770                                 if (re->r.mode & R_PANORAMA) {
771                                         if (pa->disprect.xmin == minx) {
772                                                 best = pa;
773                                                 mindist = distx;
774                                         }
775                                 }
776                                 else {
777                                         best = pa;
778                                         mindist = distx;
779                                 }
780                         }
781                 }
782         }
783         return best;
784 }
785
786 static void print_part_stats(Render *re, RenderPart *pa)
787 {
788         char str[64];
789         
790         BLI_snprintf(str, sizeof(str), "%s, Part %d-%d", re->scene->id.name + 2, pa->nr, re->i.totpart);
791         re->i.infostr = str;
792         re->stats_draw(re->sdh, &re->i);
793         re->i.infostr = NULL;
794 }
795
796 static void threaded_tile_processor(Render *re)
797 {
798         ListBase threads;
799         RenderPart *pa, *nextpa;
800         rctf viewplane = re->viewplane;
801         int rendering = 1, counter = 1, drawtimer = 0, hasdrawn, minx = 0;
802         
803         BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
804
805         /* first step; free the entire render result, make new, and/or prepare exr buffer saving */
806         if (re->result == NULL || !(re->r.scemode & R_PREVIEWBUTS)) {
807                 render_result_free(re->result);
808         
809                 if (re->sss_points && render_display_draw_enabled(re))
810                         re->result = render_result_new(re, &re->disprect, 0, RR_USE_MEM, RR_ALL_LAYERS);
811                 else if (re->r.scemode & R_FULL_SAMPLE)
812                         re->result = render_result_new_full_sample(re, &re->fullresult, &re->disprect, 0, RR_USE_EXR);
813                 else
814                         re->result = render_result_new(re, &re->disprect, 0,
815                                                        (re->r.scemode & R_EXR_TILE_FILE) ? RR_USE_EXR : RR_USE_MEM, RR_ALL_LAYERS);
816         }
817
818         BLI_rw_mutex_unlock(&re->resultmutex);
819         
820         if (re->result == NULL)
821                 return;
822         
823         /* warning; no return here without closing exr file */
824         
825         initparts(re, TRUE);
826
827         if (re->result->do_exr_tile)
828                 render_result_exr_file_begin(re);
829         
830         BLI_init_threads(&threads, do_part_thread, re->r.threads);
831         
832         /* assuming no new data gets added to dbase... */
833         R = *re;
834         
835         /* set threadsafe break */
836         R.test_break = thread_break;
837         
838         /* timer loop demands to sleep when no parts are left, so we enter loop with a part */
839         if (re->r.mode & R_PANORAMA)
840                 nextpa = find_next_pano_slice(re, &minx, &viewplane);
841         else
842                 nextpa = find_next_part(re, 0);
843         
844         while (rendering) {
845                 
846                 if (re->test_break(re->tbh))
847                         PIL_sleep_ms(50);
848                 else if (nextpa && BLI_available_threads(&threads)) {
849                         drawtimer = 0;
850                         nextpa->nr = counter++;  /* for nicest part, and for stats */
851                         nextpa->thread = BLI_available_thread_index(&threads);   /* sample index */
852                         BLI_insert_thread(&threads, nextpa);
853
854                         nextpa = find_next_part(re, minx);
855                 }
856                 else if (re->r.mode & R_PANORAMA) {
857                         if (nextpa == NULL && BLI_available_threads(&threads) == re->r.threads)
858                                 nextpa = find_next_pano_slice(re, &minx, &viewplane);
859                         else {
860                                 PIL_sleep_ms(50);
861                                 drawtimer++;
862                         }
863                 }
864                 else {
865                         PIL_sleep_ms(50);
866                         drawtimer++;
867                 }
868                 
869                 /* check for ready ones to display, and if we need to continue */
870                 rendering = 0;
871                 hasdrawn = 0;
872                 for (pa = re->parts.first; pa; pa = pa->next) {
873                         if (pa->ready) {
874                                 
875                                 BLI_remove_thread(&threads, pa);
876                                 
877                                 if (pa->result) {
878                                         if (render_display_draw_enabled(re))
879                                                 re->display_draw(re->ddh, pa->result, NULL);
880                                         print_part_stats(re, pa);
881                                         
882                                         render_result_free_list(&pa->fullresult, pa->result);
883                                         pa->result = NULL;
884                                         re->i.partsdone++;
885                                         re->progress(re->prh, re->i.partsdone / (float)re->i.totpart);
886                                         hasdrawn = 1;
887                                 }
888                         }
889                         else {
890                                 rendering = 1;
891                                 if (pa->nr && pa->result && drawtimer > 20) {
892                                         if (render_display_draw_enabled(re))
893                                                 re->display_draw(re->ddh, pa->result, &pa->result->renrect);
894                                         hasdrawn = 1;
895                                 }
896                         }
897                 }
898                 if (hasdrawn)
899                         drawtimer = 0;
900
901                 /* on break, wait for all slots to get freed */
902                 if ( (g_break = re->test_break(re->tbh)) && BLI_available_threads(&threads) == re->r.threads)
903                         rendering = 0;
904                 
905         }
906         
907         if (re->result->do_exr_tile) {
908                 BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
909                 render_result_exr_file_end(re);
910                 BLI_rw_mutex_unlock(&re->resultmutex);
911         }
912         
913         /* unset threadsafety */
914         g_break = 0;
915         
916         BLI_end_threads(&threads);
917         freeparts(re);
918         re->viewplane = viewplane; /* restore viewplane, modified by pano render */
919 }
920
921 /* currently only called by preview renders and envmap */
922 void RE_TileProcessor(Render *re)
923 {
924         threaded_tile_processor(re);
925 }
926
927 /* ************  This part uses API, for rendering Blender scenes ********** */
928
929 static void do_render_3d(Render *re)
930 {
931         /* try external */
932         if (RE_engine_render(re, 0))
933                 return;
934
935         /* internal */
936         
937 //      re->cfra= cfra; /* <- unused! */
938         re->scene->r.subframe = re->mblur_offs + re->field_offs;
939         
940         /* lock drawing in UI during data phase */
941         if (re->draw_lock)
942                 re->draw_lock(re->dlh, 1);
943         
944         /* make render verts/faces/halos/lamps */
945         if (render_scene_needs_vector(re))
946                 RE_Database_FromScene_Vectors(re, re->main, re->scene, re->lay);
947         else
948                 RE_Database_FromScene(re, re->main, re->scene, re->lay, 1);
949         
950         /* clear UI drawing locks */
951         if (re->draw_lock)
952                 re->draw_lock(re->dlh, 0);
953         
954         threaded_tile_processor(re);
955         
956         /* do left-over 3d post effects (flares) */
957         if (re->flag & R_HALO)
958                 if (!re->test_break(re->tbh))
959                         add_halo_flare(re);
960         
961         /* free all render verts etc */
962         RE_Database_Free(re);
963         
964         re->scene->r.subframe = 0.f;
965 }
966
967 /* called by blur loop, accumulate RGBA key alpha */
968 static void addblur_rect_key(RenderResult *rr, float *rectf, float *rectf1, float blurfac)
969 {
970         float mfac = 1.0f - blurfac;
971         int a, b, stride = 4 * rr->rectx;
972         int len = stride * sizeof(float);
973         
974         for (a = 0; a < rr->recty; a++) {
975                 if (blurfac == 1.0f) {
976                         memcpy(rectf, rectf1, len);
977                 }
978                 else {
979                         float *rf = rectf, *rf1 = rectf1;
980                         
981                         for (b = rr->rectx; b > 0; b--, rf += 4, rf1 += 4) {
982                                 if (rf1[3] < 0.01f)
983                                         rf[3] = mfac * rf[3];
984                                 else if (rf[3] < 0.01f) {
985                                         rf[0] = rf1[0];
986                                         rf[1] = rf1[1];
987                                         rf[2] = rf1[2];
988                                         rf[3] = blurfac * rf1[3];
989                                 }
990                                 else {
991                                         rf[0] = mfac * rf[0] + blurfac * rf1[0];
992                                         rf[1] = mfac * rf[1] + blurfac * rf1[1];
993                                         rf[2] = mfac * rf[2] + blurfac * rf1[2];
994                                         rf[3] = mfac * rf[3] + blurfac * rf1[3];
995                                 }                               
996                         }
997                 }
998                 rectf += stride;
999                 rectf1 += stride;
1000         }
1001 }
1002
1003 /* called by blur loop, accumulate renderlayers */
1004 static void addblur_rect(RenderResult *rr, float *rectf, float *rectf1, float blurfac, int channels)
1005 {
1006         float mfac = 1.0f - blurfac;
1007         int a, b, stride = channels * rr->rectx;
1008         int len = stride * sizeof(float);
1009         
1010         for (a = 0; a < rr->recty; a++) {
1011                 if (blurfac == 1.0f) {
1012                         memcpy(rectf, rectf1, len);
1013                 }
1014                 else {
1015                         float *rf = rectf, *rf1 = rectf1;
1016                         
1017                         for (b = rr->rectx * channels; b > 0; b--, rf++, rf1++) {
1018                                 rf[0] = mfac * rf[0] + blurfac * rf1[0];
1019                         }
1020                 }
1021                 rectf += stride;
1022                 rectf1 += stride;
1023         }
1024 }
1025
1026
1027 /* called by blur loop, accumulate renderlayers */
1028 static void merge_renderresult_blur(RenderResult *rr, RenderResult *brr, float blurfac, int key_alpha)
1029 {
1030         RenderLayer *rl, *rl1;
1031         RenderPass *rpass, *rpass1;
1032         
1033         rl1 = brr->layers.first;
1034         for (rl = rr->layers.first; rl && rl1; rl = rl->next, rl1 = rl1->next) {
1035                 
1036                 /* combined */
1037                 if (rl->rectf && rl1->rectf) {
1038                         if (key_alpha)
1039                                 addblur_rect_key(rr, rl->rectf, rl1->rectf, blurfac);
1040                         else
1041                                 addblur_rect(rr, rl->rectf, rl1->rectf, blurfac, 4);
1042                 }
1043                 
1044                 /* passes are allocated in sync */
1045                 rpass1 = rl1->passes.first;
1046                 for (rpass = rl->passes.first; rpass && rpass1; rpass = rpass->next, rpass1 = rpass1->next) {
1047                         addblur_rect(rr, rpass->rect, rpass1->rect, blurfac, rpass->channels);
1048                 }
1049         }
1050 }
1051
1052 /* main blur loop, can be called by fields too */
1053 static void do_render_blur_3d(Render *re)
1054 {
1055         RenderResult *rres;
1056         float blurfac;
1057         int blur = re->r.mblur_samples;
1058         
1059         /* create accumulation render result */
1060         rres = render_result_new(re, &re->disprect, 0, RR_USE_MEM, RR_ALL_LAYERS);
1061         
1062         /* do the blur steps */
1063         while (blur--) {
1064                 re->mblur_offs = re->r.blurfac * ((float)(re->r.mblur_samples - blur)) / (float)re->r.mblur_samples;
1065                 
1066                 re->i.curblur = re->r.mblur_samples - blur;    /* stats */
1067                 
1068                 do_render_3d(re);
1069                 
1070                 blurfac = 1.0f / (float)(re->r.mblur_samples - blur);
1071                 
1072                 merge_renderresult_blur(rres, re->result, blurfac, re->r.alphamode & R_ALPHAKEY);
1073                 if (re->test_break(re->tbh)) break;
1074         }
1075         
1076         /* swap results */
1077         BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
1078         render_result_free(re->result);
1079         re->result = rres;
1080         BLI_rw_mutex_unlock(&re->resultmutex);
1081         
1082         re->mblur_offs = 0.0f;
1083         re->i.curblur = 0;   /* stats */
1084         
1085         /* weak... the display callback wants an active renderlayer pointer... */
1086         re->result->renlay = render_get_active_layer(re, re->result);
1087         re->display_draw(re->ddh, re->result, NULL);    
1088 }
1089
1090
1091 /* function assumes rectf1 and rectf2 to be half size of rectf */
1092 static void interleave_rect(RenderResult *rr, float *rectf, float *rectf1, float *rectf2, int channels)
1093 {
1094         int a, stride = channels * rr->rectx;
1095         int len = stride * sizeof(float);
1096         
1097         for (a = 0; a < rr->recty; a += 2) {
1098                 memcpy(rectf, rectf1, len);
1099                 rectf += stride;
1100                 rectf1 += stride;
1101                 memcpy(rectf, rectf2, len);
1102                 rectf += stride;
1103                 rectf2 += stride;
1104         }
1105 }
1106
1107 /* merge render results of 2 fields */
1108 static void merge_renderresult_fields(RenderResult *rr, RenderResult *rr1, RenderResult *rr2)
1109 {
1110         RenderLayer *rl, *rl1, *rl2;
1111         RenderPass *rpass, *rpass1, *rpass2;
1112         
1113         rl1 = rr1->layers.first;
1114         rl2 = rr2->layers.first;
1115         for (rl = rr->layers.first; rl && rl1 && rl2; rl = rl->next, rl1 = rl1->next, rl2 = rl2->next) {
1116                 
1117                 /* combined */
1118                 if (rl->rectf && rl1->rectf && rl2->rectf)
1119                         interleave_rect(rr, rl->rectf, rl1->rectf, rl2->rectf, 4);
1120                 
1121                 /* passes are allocated in sync */
1122                 rpass1 = rl1->passes.first;
1123                 rpass2 = rl2->passes.first;
1124                 for (rpass = rl->passes.first; rpass && rpass1 && rpass2; rpass = rpass->next, rpass1 = rpass1->next, rpass2 = rpass2->next) {
1125                         interleave_rect(rr, rpass->rect, rpass1->rect, rpass2->rect, rpass->channels);
1126                 }
1127         }
1128 }
1129
1130
1131 /* interleaves 2 frames */
1132 static void do_render_fields_3d(Render *re)
1133 {
1134         Object *camera = RE_GetCamera(re);
1135         RenderResult *rr1, *rr2 = NULL;
1136         
1137         /* no render result was created, we can safely halve render y */
1138         re->winy /= 2;
1139         re->recty /= 2;
1140         re->disprect.ymin /= 2;
1141         re->disprect.ymax /= 2;
1142         
1143         re->i.curfield = 1;  /* stats */
1144         
1145         /* first field, we have to call camera routine for correct aspect and subpixel offset */
1146         RE_SetCamera(re, camera);
1147         if (re->r.mode & R_MBLUR && (re->r.scemode & R_FULL_SAMPLE) == 0)
1148                 do_render_blur_3d(re);
1149         else
1150                 do_render_3d(re);
1151
1152         BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
1153         rr1 = re->result;
1154         re->result = NULL;
1155         BLI_rw_mutex_unlock(&re->resultmutex);
1156         
1157         /* second field */
1158         if (!re->test_break(re->tbh)) {
1159                 
1160                 re->i.curfield = 2;  /* stats */
1161                 
1162                 re->flag |= R_SEC_FIELD;
1163                 if ((re->r.mode & R_FIELDSTILL) == 0) {
1164                         re->field_offs = 0.5f;
1165                 }
1166                 RE_SetCamera(re, camera);
1167                 if (re->r.mode & R_MBLUR && (re->r.scemode & R_FULL_SAMPLE) == 0)
1168                         do_render_blur_3d(re);
1169                 else
1170                         do_render_3d(re);
1171                 re->flag &= ~R_SEC_FIELD;
1172                 
1173                 re->field_offs = 0.0f;
1174                 
1175                 rr2 = re->result;
1176         }
1177         
1178         /* allocate original height new buffers */
1179         re->winy *= 2;
1180         re->recty *= 2;
1181         re->disprect.ymin *= 2;
1182         re->disprect.ymax *= 2;
1183
1184         BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
1185         re->result = render_result_new(re, &re->disprect, 0, RR_USE_MEM, RR_ALL_LAYERS);
1186
1187         if (rr2) {
1188                 if (re->r.mode & R_ODDFIELD)
1189                         merge_renderresult_fields(re->result, rr2, rr1);
1190                 else
1191                         merge_renderresult_fields(re->result, rr1, rr2);
1192                 
1193                 render_result_free(rr2);
1194         }
1195
1196         render_result_free(rr1);
1197         
1198         re->i.curfield = 0;  /* stats */
1199         
1200         /* weak... the display callback wants an active renderlayer pointer... */
1201         re->result->renlay = render_get_active_layer(re, re->result);
1202
1203         BLI_rw_mutex_unlock(&re->resultmutex);
1204
1205         re->display_draw(re->ddh, re->result, NULL);
1206 }
1207
1208 /* main render routine, no compositing */
1209 static void do_render_fields_blur_3d(Render *re)
1210 {
1211         Object *camera = RE_GetCamera(re);
1212         /* also check for camera here */
1213         if (camera == NULL) {
1214                 printf("ERROR: Cannot render, no camera\n");
1215                 G.afbreek = 1;
1216                 return;
1217         }
1218
1219         /* now use renderdata and camera to set viewplane */
1220         RE_SetCamera(re, camera);
1221         
1222         if (re->r.mode & R_FIELDS)
1223                 do_render_fields_3d(re);
1224         else if (re->r.mode & R_MBLUR && (re->r.scemode & R_FULL_SAMPLE) == 0)
1225                 do_render_blur_3d(re);
1226         else
1227                 do_render_3d(re);
1228         
1229         /* when border render, check if we have to insert it in black */
1230         if (re->result) {
1231                 if (re->r.mode & R_BORDER) {
1232                         if ((re->r.mode & R_CROP) == 0) {
1233                                 RenderResult *rres;
1234                                 
1235                                 BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
1236
1237                                 /* sub-rect for merge call later on */
1238                                 re->result->tilerect = re->disprect;
1239                                 
1240                                 /* this copying sequence could become function? */
1241                                 /* weak is: it chances disprect from border */
1242                                 re->disprect.xmin = re->disprect.ymin = 0;
1243                                 re->disprect.xmax = re->winx;
1244                                 re->disprect.ymax = re->winy;
1245                                 re->rectx = re->winx;
1246                                 re->recty = re->winy;
1247                                 
1248                                 rres = render_result_new(re, &re->disprect, 0, RR_USE_MEM, RR_ALL_LAYERS);
1249                                 
1250                                 render_result_merge(rres, re->result);
1251                                 render_result_free(re->result);
1252                                 re->result = rres;
1253                                 
1254                                 /* weak... the display callback wants an active renderlayer pointer... */
1255                                 re->result->renlay = render_get_active_layer(re, re->result);
1256                                 
1257                                 BLI_rw_mutex_unlock(&re->resultmutex);
1258                 
1259                                 re->display_init(re->dih, re->result);
1260                                 re->display_draw(re->ddh, re->result, NULL);
1261                         }
1262                         else {
1263                                 /* set offset (again) for use in compositor, disprect was manipulated. */
1264                                 re->result->xof = 0;
1265                                 re->result->yof = 0;
1266                         }
1267                 }
1268         }
1269 }
1270
1271
1272 /* within context of current Render *re, render another scene.
1273  * it uses current render image size and disprect, but doesn't execute composite
1274  */
1275 static void render_scene(Render *re, Scene *sce, int cfra)
1276 {
1277         Render *resc = RE_NewRender(sce->id.name);
1278         int winx = re->winx, winy = re->winy;
1279         
1280         sce->r.cfra = cfra;
1281
1282         BKE_scene_camera_switch_update(sce);
1283
1284         /* exception: scene uses own size (unfinished code) */
1285         if (0) {
1286                 winx = (sce->r.size * sce->r.xsch) / 100;
1287                 winy = (sce->r.size * sce->r.ysch) / 100;
1288         }
1289         
1290         /* initial setup */
1291         RE_InitState(resc, re, &sce->r, NULL, winx, winy, &re->disprect);
1292         
1293         /* still unsure entity this... */
1294         resc->main = re->main;
1295         resc->scene = sce;
1296         resc->lay = sce->lay;
1297         
1298         /* ensure scene has depsgraph, base flags etc OK */
1299         BKE_scene_set_background(re->main, sce);
1300
1301         /* copy callbacks */
1302         resc->display_draw = re->display_draw;
1303         resc->ddh = re->ddh;
1304         resc->test_break = re->test_break;
1305         resc->tbh = re->tbh;
1306         resc->stats_draw = re->stats_draw;
1307         resc->sdh = re->sdh;
1308         
1309         do_render_fields_blur_3d(resc);
1310 }
1311
1312 /* helper call to detect if this scene needs a render, or if there's a any render layer to render */
1313 static int composite_needs_render(Scene *sce, int this_scene)
1314 {
1315         bNodeTree *ntree = sce->nodetree;
1316         bNode *node;
1317         
1318         if (ntree == NULL) return 1;
1319         if (sce->use_nodes == FALSE) return 1;
1320         if ((sce->r.scemode & R_DOCOMP) == 0) return 1;
1321         
1322         for (node = ntree->nodes.first; node; node = node->next) {
1323                 if (node->type == CMP_NODE_R_LAYERS)
1324                         if (this_scene == 0 || node->id == NULL || node->id == &sce->id)
1325                                 return 1;
1326         }
1327         return 0;
1328 }
1329
1330 static void tag_scenes_for_render(Render *re)
1331 {
1332         bNode *node;
1333         Scene *sce;
1334         
1335         for (sce = re->main->scene.first; sce; sce = sce->id.next)
1336                 sce->id.flag &= ~LIB_DOIT;
1337         
1338         if (RE_GetCamera(re) && composite_needs_render(re->scene, 1))
1339                 re->scene->id.flag |= LIB_DOIT;
1340         
1341         if (re->scene->nodetree == NULL) return;
1342         
1343         /* check for render-layers nodes using other scenes, we tag them LIB_DOIT */
1344         for (node = re->scene->nodetree->nodes.first; node; node = node->next) {
1345                 if (node->type == CMP_NODE_R_LAYERS) {
1346                         if (node->id) {
1347                                 if (node->id != (ID *)re->scene)
1348                                         node->id->flag |= LIB_DOIT;
1349                         }
1350                 }
1351         }
1352         
1353 }
1354
1355 static void ntree_render_scenes(Render *re)
1356 {
1357         bNode *node;
1358         int cfra = re->scene->r.cfra;
1359         int restore_scene = 0;
1360         
1361         if (re->scene->nodetree == NULL) return;
1362         
1363         tag_scenes_for_render(re);
1364         
1365         /* now foreach render-result node tagged we do a full render */
1366         /* results are stored in a way compisitor will find it */
1367         for (node = re->scene->nodetree->nodes.first; node; node = node->next) {
1368                 if (node->type == CMP_NODE_R_LAYERS) {
1369                         if (node->id && node->id != (ID *)re->scene) {
1370                                 if (node->id->flag & LIB_DOIT) {
1371                                         Scene *scene = (Scene *)node->id;
1372
1373                                         render_scene(re, scene, cfra);
1374                                         restore_scene = (scene != re->scene);
1375                                         node->id->flag &= ~LIB_DOIT;
1376                                         
1377                                         nodeUpdate(re->scene->nodetree, node);
1378                                 }
1379                         }
1380                 }
1381         }
1382
1383         /* restore scene if we rendered another last */
1384         if (restore_scene)
1385                 BKE_scene_set_background(re->main, re->scene);
1386 }
1387
1388 /* bad call... need to think over proper method still */
1389 static void render_composit_stats(void *UNUSED(arg), char *str)
1390 {
1391         R.i.infostr = str;
1392         R.stats_draw(R.sdh, &R.i);
1393         R.i.infostr = NULL;
1394 }
1395
1396
1397 /* reads all buffers, calls optional composite, merges in first result->rectf */
1398 static void do_merge_fullsample(Render *re, bNodeTree *ntree)
1399 {
1400         float *rectf, filt[3][3];
1401         int sample;
1402         
1403         /* interaction callbacks */
1404         if (ntree) {
1405                 ntree->stats_draw = render_composit_stats;
1406                 ntree->test_break = re->test_break;
1407                 ntree->progress = re->progress;
1408                 ntree->sdh = re->sdh;
1409                 ntree->tbh = re->tbh;
1410                 ntree->prh = re->prh;
1411         }
1412         
1413         /* filtmask needs it */
1414         R = *re;
1415         
1416         /* we accumulate in here */
1417         rectf = MEM_mapallocN(re->rectx * re->recty * sizeof(float) * 4, "fullsample rgba");
1418         
1419         for (sample = 0; sample < re->r.osa; sample++) {
1420                 Render *re1;
1421                 RenderResult rres;
1422                 int x, y, mask;
1423                 
1424                 /* enable full sample print */
1425                 R.i.curfsa = sample + 1;
1426                 
1427                 /* set all involved renders on the samplebuffers (first was done by render itself, but needs tagged) */
1428                 /* also function below assumes this */
1429                         
1430                 tag_scenes_for_render(re);
1431                 for (re1 = RenderGlobal.renderlist.first; re1; re1 = re1->next) {
1432                         if (re1->scene->id.flag & LIB_DOIT) {
1433                                 if (re1->r.scemode & R_FULL_SAMPLE) {
1434                                         if (sample) {
1435                                                 BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
1436                                                 render_result_exr_file_read(re1, sample);
1437                                                 BLI_rw_mutex_unlock(&re->resultmutex);
1438                                         }
1439                                         ntreeCompositTagRender(re1->scene); /* ensure node gets exec to put buffers on stack */
1440                                 }
1441                         }
1442                 }
1443                 
1444                 /* composite */
1445                 if (ntree) {
1446                         ntreeCompositTagRender(re->scene);
1447                         ntreeCompositTagAnimated(ntree);
1448                         
1449                         ntreeCompositExecTree(ntree, &re->r, 1, G.background == 0);
1450                 }
1451                 
1452                 /* ensure we get either composited result or the active layer */
1453                 RE_AcquireResultImage(re, &rres);
1454                 
1455                 /* accumulate with filter, and clip */
1456                 mask = (1 << sample);
1457                 mask_array(mask, filt);
1458
1459                 for (y = 0; y < re->recty; y++) {
1460                         float *rf = rectf + 4 * y * re->rectx;
1461                         float *col = rres.rectf + 4 * y * re->rectx;
1462                                 
1463                         for (x = 0; x < re->rectx; x++, rf += 4, col += 4) {
1464                                 /* clamping to 1.0 is needed for correct AA */
1465                                 if (col[0] < 0.0f) col[0] = 0.0f; else if (col[0] > 1.0f) col[0] = 1.0f;
1466                                 if (col[1] < 0.0f) col[1] = 0.0f; else if (col[1] > 1.0f) col[1] = 1.0f;
1467                                 if (col[2] < 0.0f) col[2] = 0.0f; else if (col[2] > 1.0f) col[2] = 1.0f;
1468                                 
1469                                 add_filt_fmask_coord(filt, col, rf, re->rectx, re->recty, x, y);
1470                         }
1471                 }
1472                 
1473                 RE_ReleaseResultImage(re);
1474
1475                 /* show stuff */
1476                 if (sample != re->osa - 1) {
1477                         /* weak... the display callback wants an active renderlayer pointer... */
1478                         re->result->renlay = render_get_active_layer(re, re->result);
1479                         re->display_draw(re->ddh, re->result, NULL);
1480                 }
1481                 
1482                 if (re->test_break(re->tbh))
1483                         break;
1484         }
1485         
1486         /* clear interaction callbacks */
1487         if (ntree) {
1488                 ntree->stats_draw = NULL;
1489                 ntree->test_break = NULL;
1490                 ntree->progress = NULL;
1491                 ntree->tbh = ntree->sdh = ntree->prh = NULL;
1492         }
1493         
1494         /* disable full sample print */
1495         R.i.curfsa = 0;
1496         
1497         BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
1498         if (re->result->rectf)
1499                 MEM_freeN(re->result->rectf);
1500         re->result->rectf = rectf;
1501         BLI_rw_mutex_unlock(&re->resultmutex);
1502 }
1503
1504 /* called externally, via compositor */
1505 void RE_MergeFullSample(Render *re, Main *bmain, Scene *sce, bNodeTree *ntree)
1506 {
1507         Scene *scene;
1508         bNode *node;
1509
1510         /* default start situation */
1511         G.afbreek = 0;
1512         
1513         re->main = bmain;
1514         re->scene = sce;
1515         
1516         /* first call RE_ReadRenderResult on every renderlayer scene. this creates Render structs */
1517         
1518         /* tag scenes unread */
1519         for (scene = re->main->scene.first; scene; scene = scene->id.next)
1520                 scene->id.flag |= LIB_DOIT;
1521         
1522         for (node = ntree->nodes.first; node; node = node->next) {
1523                 if (node->type == CMP_NODE_R_LAYERS) {
1524                         Scene *nodescene = (Scene *)node->id;
1525                         
1526                         if (nodescene == NULL) nodescene = sce;
1527                         if (nodescene->id.flag & LIB_DOIT) {
1528                                 nodescene->r.mode |= R_OSA; /* render struct needs tables */
1529                                 RE_ReadRenderResult(sce, nodescene);
1530                                 nodescene->id.flag &= ~LIB_DOIT;
1531                         }
1532                 }
1533         }
1534         
1535         /* own render result should be read/allocated */
1536         if (re->scene->id.flag & LIB_DOIT) {
1537                 RE_ReadRenderResult(re->scene, re->scene);
1538                 re->scene->id.flag &= ~LIB_DOIT;
1539         }
1540         
1541         /* and now we can draw (result is there) */
1542         re->display_init(re->dih, re->result);
1543         re->display_clear(re->dch, re->result);
1544         
1545         do_merge_fullsample(re, ntree);
1546 }
1547
1548 /* returns fully composited render-result on given time step (in RenderData) */
1549 static void do_render_composite_fields_blur_3d(Render *re)
1550 {
1551         bNodeTree *ntree = re->scene->nodetree;
1552         int update_newframe = 0;
1553         
1554         /* INIT seeding, compositor can use random texture */
1555         BLI_srandom(re->r.cfra);
1556         
1557         if (composite_needs_render(re->scene, 1)) {
1558                 /* save memory... free all cached images */
1559                 ntreeFreeCache(ntree);
1560                 
1561                 do_render_fields_blur_3d(re);
1562         } 
1563         else {
1564                 /* ensure new result gets added, like for regular renders */
1565                 BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
1566                 
1567                 render_result_free(re->result);
1568                 re->result = render_result_new(re, &re->disprect, 0, RR_USE_MEM, RR_ALL_LAYERS);
1569
1570                 BLI_rw_mutex_unlock(&re->resultmutex);
1571                 
1572                 /* scene render process already updates animsys */
1573                 update_newframe = 1;
1574         }
1575         
1576         /* swap render result */
1577         if (re->r.scemode & R_SINGLE_LAYER) {
1578                 BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
1579                 render_result_single_layer_end(re);
1580                 BLI_rw_mutex_unlock(&re->resultmutex);
1581         }
1582         
1583         if (!re->test_break(re->tbh)) {
1584                 
1585                 if (ntree) {
1586                         ntreeCompositTagRender(re->scene);
1587                         ntreeCompositTagAnimated(ntree);
1588                 }
1589                 
1590                 if (ntree && re->scene->use_nodes && re->r.scemode & R_DOCOMP) {
1591                         /* checks if there are render-result nodes that need scene */
1592                         if ((re->r.scemode & R_SINGLE_LAYER) == 0)
1593                                 ntree_render_scenes(re);
1594                         
1595                         if (!re->test_break(re->tbh)) {
1596                                 ntree->stats_draw = render_composit_stats;
1597                                 ntree->test_break = re->test_break;
1598                                 ntree->progress = re->progress;
1599                                 ntree->sdh = re->sdh;
1600                                 ntree->tbh = re->tbh;
1601                                 ntree->prh = re->prh;
1602                                 
1603                                 /* in case it was never initialized */
1604                                 R.sdh = re->sdh;
1605                                 R.stats_draw = re->stats_draw;
1606                                 
1607                                 if (update_newframe)
1608                                         BKE_scene_update_for_newframe(re->main, re->scene, re->lay);
1609                                 
1610                                 if (re->r.scemode & R_FULL_SAMPLE)
1611                                         do_merge_fullsample(re, ntree);
1612                                 else {
1613                                         ntreeCompositExecTree(ntree, &re->r, 1, G.background == 0);
1614                                 }
1615                                 
1616                                 ntree->stats_draw = NULL;
1617                                 ntree->test_break = NULL;
1618                                 ntree->progress = NULL;
1619                                 ntree->tbh = ntree->sdh = ntree->prh = NULL;
1620                         }
1621                 }
1622                 else if (re->r.scemode & R_FULL_SAMPLE)
1623                         do_merge_fullsample(re, NULL);
1624         }
1625
1626         /* weak... the display callback wants an active renderlayer pointer... */
1627         re->result->renlay = render_get_active_layer(re, re->result);
1628         re->display_draw(re->ddh, re->result, NULL);
1629 }
1630
1631 static void renderresult_stampinfo(Render *re)
1632 {
1633         RenderResult rres;
1634
1635         /* this is the basic trick to get the displayed float or char rect from render result */
1636         RE_AcquireResultImage(re, &rres);
1637         BKE_stamp_buf(re->scene, RE_GetCamera(re), (unsigned char *)rres.rect32, rres.rectf, rres.rectx, rres.recty, 4);
1638         RE_ReleaseResultImage(re);
1639 }
1640
1641 int RE_seq_render_active(Scene *scene, RenderData *rd)
1642 {
1643         Editing *ed;
1644         Sequence *seq;
1645
1646         ed = scene->ed;
1647         
1648         if (!(rd->scemode & R_DOSEQ) || !ed || !ed->seqbase.first)
1649                 return 0;
1650         
1651         for (seq = ed->seqbase.first; seq; seq = seq->next) {
1652                 if (seq->type != SEQ_TYPE_SOUND_RAM)
1653                         return 1;
1654         }
1655         
1656         return 0;
1657 }
1658
1659 static void do_render_seq(Render *re)
1660 {
1661         static int recurs_depth = 0;
1662         struct ImBuf *ibuf;
1663         RenderResult *rr; /* don't assign re->result here as it might change during give_ibuf_seq */
1664         int cfra = re->r.cfra;
1665         SeqRenderData context;
1666
1667         re->i.cfra = cfra;
1668
1669         if (recurs_depth == 0) {
1670                 /* otherwise sequencer animation isn't updated */
1671                 BKE_animsys_evaluate_all_animation(re->main, re->scene, (float)cfra); // XXX, was BKE_scene_frame_get(re->scene)
1672         }
1673
1674         recurs_depth++;
1675
1676         if ((re->r.mode & R_BORDER) && (re->r.mode & R_CROP) == 0) {
1677                 /* if border rendering is used and cropping is disabled, final buffer should
1678                  * be as large as the whole frame */
1679                 context = seq_new_render_data(re->main, re->scene,
1680                                               re->winx, re->winy,
1681                                               100);
1682         }
1683         else {
1684                 context = seq_new_render_data(re->main, re->scene,
1685                                               re->result->rectx, re->result->recty,
1686                                               100);
1687         }
1688
1689         ibuf = give_ibuf_seq(context, cfra, 0);
1690
1691         recurs_depth--;
1692
1693         rr = re->result;
1694         
1695         BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
1696
1697         if (ibuf) {
1698                 /* copy ibuf into combined pixel rect */
1699                 render_result_rect_from_ibuf(rr, &re->r, ibuf);
1700                 
1701                 if (recurs_depth == 0) { /* with nested scenes, only free on toplevel... */
1702                         Editing *ed = re->scene->ed;
1703                         if (ed)
1704                                 free_imbuf_seq(re->scene, &ed->seqbase, TRUE, TRUE);
1705                 }
1706                 IMB_freeImBuf(ibuf);
1707         }
1708         else {
1709                 /* render result is delivered empty in most cases, nevertheless we handle all cases */
1710                 render_result_rect_fill_zero(rr);
1711         }
1712
1713         BLI_rw_mutex_unlock(&re->resultmutex);
1714
1715         /* just in case this flag went missing at some point */
1716         re->r.scemode |= R_DOSEQ;
1717
1718         /* set overall progress of sequence rendering */
1719         if (re->r.efra != re->r.sfra)
1720                 re->progress(re->prh, (float)(cfra - re->r.sfra) / (re->r.efra - re->r.sfra));
1721         else
1722                 re->progress(re->prh, 1.0f);
1723 }
1724
1725 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
1726
1727 /* main loop: doing sequence + fields + blur + 3d render + compositing */
1728 static void do_render_all_options(Render *re)
1729 {
1730         BKE_scene_camera_switch_update(re->scene);
1731
1732         re->i.starttime = PIL_check_seconds_timer();
1733
1734         /* ensure no images are in memory from previous animated sequences */
1735         BKE_image_all_free_anim_ibufs(re->r.cfra);
1736
1737         if (RE_engine_render(re, 1)) {
1738                 /* in this case external render overrides all */
1739         }
1740         else if (RE_seq_render_active(re->scene, &re->r)) {
1741                 /* note: do_render_seq() frees rect32 when sequencer returns float images */
1742                 if (!re->test_break(re->tbh))
1743                         do_render_seq(re);
1744                 
1745                 re->stats_draw(re->sdh, &re->i);
1746                 re->display_draw(re->ddh, re->result, NULL);
1747         }
1748         else {
1749                 do_render_composite_fields_blur_3d(re);
1750         }
1751         
1752         re->i.lastframetime = PIL_check_seconds_timer() - re->i.starttime;
1753         
1754         re->stats_draw(re->sdh, &re->i);
1755         
1756         /* stamp image info here */
1757         if ((re->r.stamp & R_STAMP_ALL) && (re->r.stamp & R_STAMP_DRAW)) {
1758                 renderresult_stampinfo(re);
1759                 re->display_draw(re->ddh, re->result, NULL);
1760         }
1761 }
1762
1763 static int check_valid_camera(Scene *scene, Object *camera_override)
1764 {
1765         int check_comp = 1;
1766
1767         if (camera_override == NULL && scene->camera == NULL)
1768                 scene->camera = BKE_scene_camera_find(scene);
1769
1770         if (scene->r.scemode & R_DOSEQ) {
1771                 if (scene->ed) {
1772                         Sequence *seq = scene->ed->seqbase.first;
1773
1774                         check_comp = 0;
1775
1776                         while (seq) {
1777                                 if (seq->type == SEQ_TYPE_SCENE && seq->scene) {
1778                                         if (!seq->scene_camera) {
1779                                                 if (!seq->scene->camera && !BKE_scene_camera_find(seq->scene)) {
1780                                                         if (seq->scene == scene) {
1781                                                                 /* for current scene camera could be unneeded due to compisite nodes */
1782                                                                 check_comp = 1;
1783                                                         }
1784                                                         else {
1785                                                                 /* for other scenes camera is necessary */
1786                                                                 return 0;
1787                                                         }
1788                                                 }
1789                                         }
1790                                 }
1791
1792                                 seq = seq->next;
1793                         }
1794                 }
1795         }
1796
1797         if (check_comp) { /* no sequencer or sequencer depends on compositor */
1798                 if (scene->r.scemode & R_DOCOMP && scene->use_nodes) {
1799                         bNode *node = scene->nodetree->nodes.first;
1800
1801                         while (node) {
1802                                 if (node->type == CMP_NODE_R_LAYERS) {
1803                                         Scene *sce = node->id ? (Scene *)node->id : scene;
1804
1805                                         if (!sce->camera && !BKE_scene_camera_find(sce)) {
1806                                                 /* all render layers nodes need camera */
1807                                                 return 0;
1808                                         }
1809                                 }
1810
1811                                 node = node->next;
1812                         }
1813                 }
1814                 else {
1815                         return (camera_override != NULL || scene->camera != NULL);
1816                 }
1817         }
1818
1819         return 1;
1820 }
1821
1822 static int node_tree_has_composite_output(bNodeTree *ntree)
1823 {
1824         bNode *node;
1825
1826         for (node = ntree->nodes.first; node; node = node->next) {
1827                 if (node->type == CMP_NODE_COMPOSITE) {
1828                         return TRUE;
1829                 }
1830                 else if (node->type == NODE_GROUP) {
1831                         if (node_tree_has_composite_output((bNodeTree *)node->id))
1832                                 return TRUE;
1833                 }
1834         }
1835
1836         return FALSE;
1837 }
1838
1839 static int check_composite_output(Scene *scene)
1840 {
1841         return node_tree_has_composite_output(scene->nodetree);
1842 }
1843
1844 int RE_is_rendering_allowed(Scene *scene, Object *camera_override, ReportList *reports)
1845 {
1846         SceneRenderLayer *srl;
1847         
1848         if (scene->r.mode & R_BORDER) {
1849                 if (scene->r.border.xmax <= scene->r.border.xmin ||
1850                     scene->r.border.ymax <= scene->r.border.ymin)
1851                 {
1852                         BKE_report(reports, RPT_ERROR, "No border area selected.");
1853                         return 0;
1854                 }
1855         }
1856         
1857         if (scene->r.scemode & (R_EXR_TILE_FILE | R_FULL_SAMPLE)) {
1858                 char str[FILE_MAX];
1859                 
1860                 render_result_exr_file_path(scene, "", 0, str);
1861                 
1862                 if (BLI_file_is_writable(str) == 0) {
1863                         BKE_report(reports, RPT_ERROR, "Can not save render buffers, check the temp default path");
1864                         return 0;
1865                 }
1866                 
1867                 /* no fullsample and edge */
1868                 if ((scene->r.scemode & R_FULL_SAMPLE) && (scene->r.mode & R_EDGE)) {
1869                         BKE_report(reports, RPT_ERROR, "Full Sample doesn't support Edge Enhance");
1870                         return 0;
1871                 }
1872                 
1873         }
1874         else
1875                 scene->r.scemode &= ~R_FULL_SAMPLE;  /* clear to be sure */
1876         
1877         if (scene->r.scemode & R_DOCOMP) {
1878                 if (scene->use_nodes) {
1879                         if (!scene->nodetree) {
1880                                 BKE_report(reports, RPT_ERROR, "No Nodetree in Scene");
1881                                 return 0;
1882                         }
1883                         
1884                         if (!check_composite_output(scene)) {
1885                                 BKE_report(reports, RPT_ERROR, "No Render Output Node in Scene");
1886                                 return 0;
1887                         }
1888                         
1889                         if (scene->r.scemode & R_FULL_SAMPLE) {
1890                                 if (composite_needs_render(scene, 0) == 0) {
1891                                         BKE_report(reports, RPT_ERROR, "Full Sample AA not supported without 3d rendering");
1892                                         return 0;
1893                                 }
1894                         }
1895                 }
1896         }
1897         
1898         /* check valid camera, without camera render is OK (compo, seq) */
1899         if (!check_valid_camera(scene, camera_override)) {
1900                 BKE_report(reports, RPT_ERROR, "No camera");
1901                 return 0;
1902         }
1903         
1904         /* get panorama & ortho, only after camera is set */
1905         BKE_camera_object_mode(&scene->r, camera_override ? camera_override : scene->camera);
1906
1907         /* forbidden combinations */
1908         if (scene->r.mode & R_PANORAMA) {
1909                 if (scene->r.mode & R_ORTHO) {
1910                         BKE_report(reports, RPT_ERROR, "No Ortho render possible for Panorama");
1911                         return 0;
1912                 }
1913         }
1914
1915         /* layer flag tests */
1916         if (scene->r.scemode & R_SINGLE_LAYER) {
1917                 srl = BLI_findlink(&scene->r.layers, scene->r.actlay);
1918                 /* force layer to be enabled */
1919                 srl->layflag &= ~SCE_LAY_DISABLE;
1920         }
1921         
1922         for (srl = scene->r.layers.first; srl; srl = srl->next)
1923                 if (!(srl->layflag & SCE_LAY_DISABLE))
1924                         break;
1925         if (srl == NULL) {
1926                 BKE_report(reports, RPT_ERROR, "All RenderLayers are disabled");
1927                 return 0;
1928         }
1929
1930         return 1;
1931 }
1932
1933 static void validate_render_settings(Render *re)
1934 {
1935         if (re->r.scemode & (R_EXR_TILE_FILE | R_FULL_SAMPLE)) {
1936                 /* no osa + fullsample won't work... */
1937                 if (re->r.osa == 0)
1938                         re->r.scemode &= ~R_FULL_SAMPLE;
1939         }
1940         else re->r.scemode &= ~R_FULL_SAMPLE;   /* clear to be sure */
1941
1942         if (RE_engine_is_external(re)) {
1943                 /* not supported yet */
1944                 re->r.scemode &= ~(R_FULL_SAMPLE);
1945                 re->r.mode &= ~(R_FIELDS | R_MBLUR);
1946         }
1947 }
1948
1949 static void update_physics_cache(Render *re, Scene *scene, int UNUSED(anim_init))
1950 {
1951         PTCacheBaker baker;
1952
1953         baker.main = re->main;
1954         baker.scene = scene;
1955         baker.pid = NULL;
1956         baker.bake = 0;
1957         baker.render = 1;
1958         baker.anim_init = 1;
1959         baker.quick_step = 1;
1960         baker.break_test = re->test_break;
1961         baker.break_data = re->tbh;
1962         baker.progressbar = NULL;
1963
1964         BKE_ptcache_bake(&baker);
1965 }
1966 /* evaluating scene options for general Blender render */
1967 static int render_initialize_from_main(Render *re, Main *bmain, Scene *scene, SceneRenderLayer *srl, Object *camera_override, unsigned int lay, int anim, int anim_init)
1968 {
1969         int winx, winy;
1970         rcti disprect;
1971         
1972         /* r.xsch and r.ysch has the actual view window size
1973          * r.border is the clipping rect */
1974         
1975         /* calculate actual render result and display size */
1976         winx = (scene->r.size * scene->r.xsch) / 100;
1977         winy = (scene->r.size * scene->r.ysch) / 100;
1978         
1979         /* we always render smaller part, inserting it in larger image is compositor bizz, it uses disprect for it */
1980         if (scene->r.mode & R_BORDER) {
1981                 disprect.xmin = scene->r.border.xmin * winx;
1982                 disprect.xmax = scene->r.border.xmax * winx;
1983                 
1984                 disprect.ymin = scene->r.border.ymin * winy;
1985                 disprect.ymax = scene->r.border.ymax * winy;
1986         }
1987         else {
1988                 disprect.xmin = disprect.ymin = 0;
1989                 disprect.xmax = winx;
1990                 disprect.ymax = winy;
1991         }
1992         
1993         re->main = bmain;
1994         re->scene = scene;
1995         re->camera_override = camera_override;
1996         re->lay = lay;
1997         
1998         /* not too nice, but it survives anim-border render */
1999         if (anim) {
2000                 re->disprect = disprect;
2001                 return 1;
2002         }
2003         
2004         /* check all scenes involved */
2005         tag_scenes_for_render(re);
2006
2007         /*
2008          * Disabled completely for now,
2009          * can be later set as render profile option
2010          * and default for background render.
2011          */
2012         if (0) {
2013                 /* make sure dynamics are up to date */
2014                 update_physics_cache(re, scene, anim_init);
2015         }
2016         
2017         if (srl || scene->r.scemode & R_SINGLE_LAYER) {
2018                 BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
2019                 render_result_single_layer_begin(re);
2020                 BLI_rw_mutex_unlock(&re->resultmutex);
2021         }
2022         
2023         RE_InitState(re, NULL, &scene->r, srl, winx, winy, &disprect);
2024         if (!re->ok)  /* if an error was printed, abort */
2025                 return 0;
2026         
2027         /* initstate makes new result, have to send changed tags around */
2028         ntreeCompositTagRender(re->scene);
2029
2030         validate_render_settings(re);
2031
2032         re->display_init(re->dih, re->result);
2033         re->display_clear(re->dch, re->result);
2034         
2035         return 1;
2036 }
2037
2038 void RE_SetReports(Render *re, ReportList *reports)
2039 {
2040         re->reports = reports;
2041 }
2042
2043 /* general Blender frame render call */
2044 void RE_BlenderFrame(Render *re, Main *bmain, Scene *scene, SceneRenderLayer *srl, Object *camera_override, unsigned int lay, int frame, const short write_still)
2045 {
2046         /* ugly global still... is to prevent preview events and signal subsurfs etc to make full resol */
2047         G.rendering = 1;
2048         
2049         scene->r.cfra = frame;
2050         
2051         if (render_initialize_from_main(re, bmain, scene, srl, camera_override, lay, 0, 0)) {
2052                 MEM_reset_peak_memory();
2053
2054                 BLI_callback_exec(re->main, (ID *)scene, BLI_CB_EVT_RENDER_PRE);
2055
2056                 do_render_all_options(re);
2057
2058                 if (write_still && !G.afbreek) {
2059                         if (BKE_imtype_is_movie(scene->r.im_format.imtype)) {
2060                                 /* operator checks this but in case its called from elsewhere */
2061                                 printf("Error: cant write single images with a movie format!\n");
2062                         }
2063                         else {
2064                                 char name[FILE_MAX];
2065                                 BKE_makepicstring(name, scene->r.pic, bmain->name, scene->r.cfra, scene->r.im_format.imtype, scene->r.scemode & R_EXTENSION, FALSE);
2066
2067                                 /* reports only used for Movie */
2068                                 do_write_image_or_movie(re, bmain, scene, NULL, name);
2069                         }
2070                 }
2071
2072                 BLI_callback_exec(re->main, (ID *)scene, BLI_CB_EVT_RENDER_POST); /* keep after file save */
2073         }
2074
2075         BLI_callback_exec(re->main, (ID *)scene, G.afbreek ? BLI_CB_EVT_RENDER_CANCEL : BLI_CB_EVT_RENDER_COMPLETE);
2076
2077         /* UGLY WARNING */
2078         G.rendering = 0;
2079 }
2080
2081 static void colormanage_image_for_write(ImBuf *ibuf, Scene *scene)
2082 {
2083         IMB_display_buffer_to_imbuf_rect(ibuf, &scene->r.im_format.view_settings,
2084                                          &scene->r.im_format.display_settings);
2085
2086         if (ibuf)
2087                 imb_freerectfloatImBuf(ibuf);
2088 }
2089
2090 static int do_write_image_or_movie(Render *re, Main *bmain, Scene *scene, bMovieHandle *mh, const char *name_override)
2091 {
2092         char name[FILE_MAX];
2093         RenderResult rres;
2094         Object *camera = RE_GetCamera(re);
2095         int ok = 1;
2096         
2097         RE_AcquireResultImage(re, &rres);
2098
2099         /* write movie or image */
2100         if (BKE_imtype_is_movie(scene->r.im_format.imtype)) {
2101                 int do_free = FALSE;
2102                 unsigned int *rect32 = (unsigned int *)rres.rect32;
2103                 /* note; the way it gets 32 bits rects is weak... */
2104                 if (rres.rect32 == NULL) {
2105                         rect32 = MEM_mapallocN(sizeof(int) * rres.rectx * rres.recty, "temp 32 bits rect");
2106                         RE_ResultGet32(re, rect32);
2107                         do_free = TRUE;
2108                 }
2109
2110                 ok = mh->append_movie(&re->r, scene->r.sfra, scene->r.cfra, (int *)rect32,
2111                                       rres.rectx, rres.recty, re->reports);
2112                 if (do_free) {
2113                         MEM_freeN(rect32);
2114                 }
2115                 printf("Append frame %d", scene->r.cfra);
2116         } 
2117         else {
2118                 if (name_override)
2119                         BLI_strncpy(name, name_override, sizeof(name));
2120                 else
2121                         BKE_makepicstring(name, scene->r.pic, bmain->name, scene->r.cfra, scene->r.im_format.imtype, scene->r.scemode & R_EXTENSION, TRUE);
2122                 
2123                 if (re->r.im_format.imtype == R_IMF_IMTYPE_MULTILAYER) {
2124                         if (re->result) {
2125                                 RE_WriteRenderResult(re->reports, re->result, name, scene->r.im_format.exr_codec);
2126                                 printf("Saved: %s", name);
2127                         }
2128                 }
2129                 else {
2130                         ImBuf *ibuf = render_result_rect_to_ibuf(&rres, &scene->r);
2131                         int do_colormanagement;
2132
2133                         do_colormanagement = !BKE_imtype_supports_float(scene->r.im_format.imtype);
2134
2135                         if (do_colormanagement)
2136                                 colormanage_image_for_write(ibuf, scene);
2137
2138                         ok = BKE_imbuf_write_stamp(scene, camera, ibuf, name, &scene->r.im_format);
2139                         
2140                         if (ok == 0) {
2141                                 printf("Render error: cannot save %s\n", name);
2142                         }
2143                         else printf("Saved: %s", name);
2144                         
2145                         /* optional preview images for exr */
2146                         if (ok && scene->r.im_format.imtype == R_IMF_IMTYPE_OPENEXR && (scene->r.im_format.flag & R_IMF_FLAG_PREVIEW_JPG)) {
2147                                 ImageFormatData imf = scene->r.im_format;
2148                                 imf.imtype = R_IMF_IMTYPE_JPEG90;
2149
2150                                 if (BLI_testextensie(name, ".exr"))
2151                                         name[strlen(name) - 4] = 0;
2152                                 BKE_add_image_extension(name, R_IMF_IMTYPE_JPEG90);
2153                                 ibuf->planes = 24;
2154
2155                                 colormanage_image_for_write(ibuf, scene);
2156
2157                                 BKE_imbuf_write_stamp(scene, camera, ibuf, name, &imf);
2158                                 printf("\nSaved: %s", name);
2159                         }
2160                         
2161                         /* imbuf knows which rects are not part of ibuf */
2162                         IMB_freeImBuf(ibuf);
2163                 }
2164         }
2165         
2166         RE_ReleaseResultImage(re);
2167
2168         BLI_timestr(re->i.lastframetime, name);
2169         printf(" Time: %s", name);
2170
2171         BLI_callback_exec(G.main, NULL, BLI_CB_EVT_RENDER_STATS);
2172
2173         fputc('\n', stdout);
2174         fflush(stdout); /* needed for renderd !! (not anymore... (ton)) */
2175
2176         return ok;
2177 }
2178
2179 /* saves images to disk */
2180 void RE_BlenderAnim(Render *re, Main *bmain, Scene *scene, Object *camera_override, unsigned int lay, int sfra, int efra, int tfra)
2181 {
2182         bMovieHandle *mh = BKE_movie_handle_get(scene->r.im_format.imtype);
2183         int cfrao = scene->r.cfra;
2184         int nfra, totrendered = 0, totskipped = 0;
2185         
2186         /* do not fully call for each frame, it initializes & pops output window */
2187         if (!render_initialize_from_main(re, bmain, scene, NULL, camera_override, lay, 0, 1))
2188                 return;
2189         
2190         /* ugly global still... is to prevent renderwin events and signal subsurfs etc to make full resol */
2191         /* is also set by caller renderwin.c */
2192         G.rendering = 1;
2193
2194         re->flag |= R_ANIMATION;
2195
2196         if (BKE_imtype_is_movie(scene->r.im_format.imtype))
2197                 if (!mh->start_movie(scene, &re->r, re->rectx, re->recty, re->reports))
2198                         G.afbreek = 1;
2199
2200         if (mh->get_next_frame) {
2201                 while (!(G.afbreek == 1)) {
2202                         int nf = mh->get_next_frame(&re->r, re->reports);
2203                         if (nf >= 0 && nf >= scene->r.sfra && nf <= scene->r.efra) {
2204                                 scene->r.cfra = re->r.cfra = nf;
2205
2206                                 BLI_callback_exec(re->main, (ID *)scene, BLI_CB_EVT_RENDER_PRE);
2207
2208                                 do_render_all_options(re);
2209                                 totrendered++;
2210
2211                                 if (re->test_break(re->tbh) == 0) {
2212                                         if (!do_write_image_or_movie(re, bmain, scene, mh, NULL))
2213                                                 G.afbreek = 1;
2214                                 }
2215
2216                                 if (G.afbreek == 0) {
2217                                         BLI_callback_exec(re->main, (ID *)scene, BLI_CB_EVT_RENDER_POST); /* keep after file save */
2218                                 }
2219                         }
2220                         else {
2221                                 if (re->test_break(re->tbh))
2222                                         G.afbreek = 1;
2223                         }
2224                 }
2225         }
2226         else {
2227                 for (nfra = sfra, scene->r.cfra = sfra; scene->r.cfra <= efra; scene->r.cfra++) {
2228                         char name[FILE_MAX];
2229                         
2230                         /* only border now, todo: camera lens. (ton) */
2231                         render_initialize_from_main(re, bmain, scene, NULL, camera_override, lay, 1, 0);
2232
2233                         if (nfra != scene->r.cfra) {
2234                                 /*
2235                                  * Skip this frame, but update for physics and particles system.
2236                                  * From convertblender.c:
2237                                  * in localview, lamps are using normal layers, objects only local bits.
2238                                  */
2239                                 unsigned int updatelay;
2240
2241                                 if (re->lay & 0xFF000000)
2242                                         updatelay = re->lay & 0xFF000000;
2243                                 else
2244                                         updatelay = re->lay;
2245
2246                                 BKE_scene_update_for_newframe(bmain, scene, updatelay);
2247                                 continue;
2248                         }
2249                         else
2250                                 nfra += tfra;
2251
2252                         /* Touch/NoOverwrite options are only valid for image's */
2253                         if (BKE_imtype_is_movie(scene->r.im_format.imtype) == 0) {
2254                                 if (scene->r.mode & (R_NO_OVERWRITE | R_TOUCH))
2255                                         BKE_makepicstring(name, scene->r.pic, bmain->name, scene->r.cfra, scene->r.im_format.imtype, scene->r.scemode & R_EXTENSION, TRUE);
2256
2257                                 if (scene->r.mode & R_NO_OVERWRITE && BLI_exists(name)) {
2258                                         printf("skipping existing frame \"%s\"\n", name);
2259                                         totskipped++;
2260                                         continue;
2261                                 }
2262                                 if (scene->r.mode & R_TOUCH && !BLI_exists(name)) {
2263                                         BLI_make_existing_file(name); /* makes the dir if its not there */
2264                                         BLI_file_touch(name);
2265                                 }
2266                         }
2267
2268                         re->r.cfra = scene->r.cfra;     /* weak.... */
2269
2270                         /* run callbacs before rendering, before the scene is updated */
2271                         BLI_callback_exec(re->main, (ID *)scene, BLI_CB_EVT_RENDER_PRE);
2272
2273                         
2274                         do_render_all_options(re);
2275                         totrendered++;
2276                         
2277                         if (re->test_break(re->tbh) == 0) {
2278                                 if (!G.afbreek)
2279                                         if (!do_write_image_or_movie(re, bmain, scene, mh, NULL))
2280                                                 G.afbreek = 1;
2281                         }
2282                         else
2283                                 G.afbreek = 1;
2284                 
2285                         if (G.afbreek == 1) {
2286                                 /* remove touched file */
2287                                 if (BKE_imtype_is_movie(scene->r.im_format.imtype) == 0) {
2288                                         if (scene->r.mode & R_TOUCH && BLI_exists(name) && BLI_file_size(name) == 0) {
2289                                                 BLI_delete(name, 0, 0);
2290                                         }
2291                                 }
2292                                 
2293                                 break;
2294                         }
2295
2296                         if (G.afbreek == 0) {
2297                                 BLI_callback_exec(re->main, (ID *)scene, BLI_CB_EVT_RENDER_POST); /* keep after file save */
2298                         }
2299                 }
2300         }
2301         
2302         /* end movie */
2303         if (BKE_imtype_is_movie(scene->r.im_format.imtype))
2304                 mh->end_movie();
2305         
2306         if (totskipped && totrendered == 0)
2307                 BKE_report(re->reports, RPT_INFO, "No frames rendered, skipped to not overwrite");
2308
2309         scene->r.cfra = cfrao;
2310
2311         re->flag &= ~R_ANIMATION;
2312
2313         BLI_callback_exec(re->main, (ID *)scene, G.afbreek ? BLI_CB_EVT_RENDER_CANCEL : BLI_CB_EVT_RENDER_COMPLETE);
2314
2315         /* UGLY WARNING */
2316         G.rendering = 0;
2317 }
2318
2319 void RE_PreviewRender(Render *re, Main *bmain, Scene *sce)
2320 {
2321         Object *camera;
2322         int winx, winy;
2323
2324         winx = (sce->r.size * sce->r.xsch) / 100;
2325         winy = (sce->r.size * sce->r.ysch) / 100;
2326
2327         RE_InitState(re, NULL, &sce->r, NULL, winx, winy, NULL);
2328
2329         re->main = bmain;
2330         re->scene = sce;
2331         re->lay = sce->lay;
2332
2333         camera = RE_GetCamera(re);
2334         RE_SetCamera(re, camera);
2335
2336         do_render_3d(re);
2337 }
2338
2339 /* note; repeated win/disprect calc... solve that nicer, also in compo */
2340
2341 /* only the temp file! */
2342 int RE_ReadRenderResult(Scene *scene, Scene *scenode)
2343 {
2344         Render *re;
2345         int winx, winy, success;
2346         rcti disprect;
2347         
2348         /* calculate actual render result and display size */
2349         winx = (scene->r.size * scene->r.xsch) / 100;
2350         winy = (scene->r.size * scene->r.ysch) / 100;
2351         
2352         /* only in movie case we render smaller part */
2353         if (scene->r.mode & R_BORDER) {
2354                 disprect.xmin = scene->r.border.xmin * winx;
2355                 disprect.xmax = scene->r.border.xmax * winx;
2356                 
2357                 disprect.ymin = scene->r.border.ymin * winy;
2358                 disprect.ymax = scene->r.border.ymax * winy;
2359         }
2360         else {
2361                 disprect.xmin = disprect.ymin = 0;
2362                 disprect.xmax = winx;
2363                 disprect.ymax = winy;
2364         }
2365         
2366         if (scenode)
2367                 scene = scenode;
2368         
2369         /* get render: it can be called from UI with draw callbacks */
2370         re = RE_GetRender(scene->id.name);
2371         if (re == NULL)
2372                 re = RE_NewRender(scene->id.name);
2373         RE_InitState(re, NULL, &scene->r, NULL, winx, winy, &disprect);
2374         re->scene = scene;
2375         
2376         BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
2377         success = render_result_exr_file_read(re, 0);
2378         BLI_rw_mutex_unlock(&re->resultmutex);
2379
2380         return success;
2381 }
2382
2383 void RE_set_max_threads(int threads)
2384 {
2385         if (threads == 0) {
2386                 RenderGlobal.threads = BLI_system_thread_count();
2387         }
2388         else if (threads >= 1 && threads <= BLENDER_MAX_THREADS) {
2389                 RenderGlobal.threads = threads;
2390         }
2391         else {
2392                 printf("Error, threads has to be in range 0-%d\n", BLENDER_MAX_THREADS);
2393         }
2394 }
2395
2396 void RE_init_threadcount(Render *re) 
2397 {
2398         if (RenderGlobal.threads >= 1) { /* only set as an arg in background mode */
2399                 re->r.threads = MIN2(RenderGlobal.threads, BLENDER_MAX_THREADS);
2400         }
2401         else if ((re->r.mode & R_FIXED_THREADS) == 0 || RenderGlobal.threads == 0) { /* Automatic threads */
2402                 re->r.threads = BLI_system_thread_count();
2403         }
2404 }
2405
2406 /* loads in image into a result, size must match
2407  * x/y offsets are only used on a partial copy when dimensions don't match */
2408 void RE_layer_load_from_file(RenderLayer *layer, ReportList *reports, const char *filename, int x, int y)
2409 {
2410         ImBuf *ibuf = IMB_loadiffname(filename, IB_rect);
2411
2412         if (ibuf && (ibuf->rect || ibuf->rect_float)) {
2413                 if (ibuf->x == layer->rectx && ibuf->y == layer->recty) {
2414                         if (ibuf->rect_float == NULL)
2415                                 IMB_float_from_rect(ibuf);
2416
2417                         memcpy(layer->rectf, ibuf->rect_float, sizeof(float) * 4 * layer->rectx * layer->recty);
2418                 }
2419                 else {
2420                         if ((ibuf->x - x >= layer->rectx) && (ibuf->y - y >= layer->recty)) {
2421                                 ImBuf *ibuf_clip;
2422
2423                                 if (ibuf->rect_float == NULL)
2424                                         IMB_float_from_rect(ibuf);
2425
2426                                 ibuf_clip = IMB_allocImBuf(layer->rectx, layer->recty, 32, IB_rectfloat);
2427                                 if (ibuf_clip) {
2428                                         IMB_rectcpy(ibuf_clip, ibuf, 0, 0, x, y, layer->rectx, layer->recty);
2429
2430                                         memcpy(layer->rectf, ibuf_clip->rect_float, sizeof(float) * 4 * layer->rectx * layer->recty);
2431                                         IMB_freeImBuf(ibuf_clip);
2432                                 }
2433                                 else {
2434                                         BKE_reportf(reports, RPT_ERROR, "RE_result_rect_from_file: failed to allocate clip buffer '%s'\n", filename);
2435                                 }
2436                         }
2437                         else {
2438                                 BKE_reportf(reports, RPT_ERROR, "RE_result_rect_from_file: incorrect dimensions for partial copy '%s'\n", filename);
2439                         }
2440                 }
2441
2442                 IMB_freeImBuf(ibuf);
2443         }
2444         else {
2445                 BKE_reportf(reports, RPT_ERROR, "RE_result_rect_from_file: failed to load '%s'\n", filename);
2446         }
2447 }
2448
2449 void RE_result_load_from_file(RenderResult *result, ReportList *reports, const char *filename)
2450 {
2451         if (!render_result_exr_file_read_path(result, NULL, filename)) {
2452                 BKE_reportf(reports, RPT_ERROR, "RE_result_rect_from_file: failed to load '%s'\n", filename);
2453                 return;
2454         }
2455 }
2456
2457 const float default_envmap_layout[] = { 0, 0, 1, 0, 2, 0, 0, 1, 1, 1, 2, 1 };
2458
2459 int RE_WriteEnvmapResult(struct ReportList *reports, Scene *scene, EnvMap *env, const char *relpath, const char imtype, float layout[12])
2460 {
2461         ImageFormatData imf;
2462         ImBuf *ibuf = NULL;
2463         int ok;
2464         int dx;
2465         int maxX = 0, maxY = 0, i = 0;
2466         char filepath[FILE_MAX];
2467
2468         if (env->cube[1] == NULL) {
2469                 BKE_report(reports, RPT_ERROR, "There is no generated environment map available to save");
2470                 return 0;
2471         }
2472
2473         imf = scene->r.im_format;
2474         imf.imtype = imtype;
2475
2476         dx = env->cube[1]->x;
2477
2478         if (env->type == ENV_CUBE) {
2479                 for (i = 0; i < 12; i += 2) {
2480                         maxX = MAX2(maxX, layout[i] + 1);
2481                         maxY = MAX2(maxY, layout[i + 1] + 1);
2482                 }
2483
2484                 ibuf = IMB_allocImBuf(maxX * dx, maxY * dx, 24, IB_rectfloat);
2485
2486                 for (i = 0; i < 12; i += 2)
2487                         if (layout[i] > -1 && layout[i + 1] > -1)
2488                                 IMB_rectcpy(ibuf, env->cube[i / 2], layout[i] * dx, layout[i + 1] * dx, 0, 0, dx, dx);
2489         }
2490         else if (env->type == ENV_PLANE) {
2491                 ibuf = IMB_allocImBuf(dx, dx, 24, IB_rectfloat);
2492                 IMB_rectcpy(ibuf, env->cube[1], 0, 0, 0, 0, dx, dx);
2493         }
2494         else {
2495                 BKE_report(reports, RPT_ERROR, "Invalid environment map type");
2496                 return 0;
2497         }
2498
2499         if (scene->r.color_mgt_flag & R_COLOR_MANAGEMENT)
2500                 ibuf->profile = IB_PROFILE_LINEAR_RGB;
2501
2502         /* to save, we first get absolute path */
2503         BLI_strncpy(filepath, relpath, sizeof(filepath));
2504         BLI_path_abs(filepath, G.main->name);
2505
2506         ok = BKE_imbuf_write(ibuf, filepath, &imf);
2507
2508         IMB_freeImBuf(ibuf);
2509
2510         if (ok) {
2511                 return TRUE;
2512         }
2513         else {
2514                 BKE_report(reports, RPT_ERROR, "Error writing environment map.");
2515                 return FALSE;
2516         }
2517 }
2518