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