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