d93ee448f6231129080daa8689999fc86ecce963
[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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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 #include <math.h>
30 #include <limits.h>
31 #include <string.h>
32 #include <stdlib.h>
33
34 #include "DNA_group_types.h"
35 #include "DNA_image_types.h"
36 #include "DNA_node_types.h"
37 #include "DNA_object_types.h"
38 #include "DNA_scene_types.h"
39 #include "DNA_userdef_types.h"
40
41 #include "BKE_global.h"
42 #include "BKE_image.h"
43 #include "BKE_main.h"
44 #include "BKE_node.h"
45 #include "BKE_object.h"
46 #include "BKE_scene.h"
47 #include "BKE_writeavi.h"       /* <------ should be replaced once with generic movie module */
48
49 #include "MEM_guardedalloc.h"
50
51 #include "BLI_arithb.h"
52 #include "BLI_blenlib.h"
53 #include "BLI_rand.h"
54 #include "BLI_threads.h"
55
56 #include "PIL_time.h"
57 #include "IMB_imbuf.h"
58 #include "IMB_imbuf_types.h"
59
60 #include "intern/openexr/openexr_multi.h"
61
62 #include "RE_pipeline.h"
63 #include "radio.h"
64
65 #include "BSE_sequence.h"  /* <----------------- bad!!! */
66
67 #ifndef DISABLE_YAFRAY
68 /* yafray: include for yafray export/render */
69 #include "YafRay_Api.h"
70
71 #endif /* disable yafray */
72
73 /* internal */
74 #include "render_types.h"
75 #include "renderpipeline.h"
76 #include "renderdatabase.h"
77 #include "rendercore.h"
78 #include "envmap.h"
79 #include "initrender.h"
80 #include "shadbuf.h"
81 #include "zbuf.h"
82
83
84 /* render flow
85
86 1) Initialize state
87 - state data, tables
88 - movie/image file init
89 - everything that doesn't change during animation
90
91 2) Initialize data
92 - camera, world, matrices
93 - make render verts, faces, halos, strands
94 - everything can change per frame/field
95
96 3) Render Processor
97 - multiple layers
98 - tiles, rect, baking
99 - layers/tiles optionally to disk or directly in Render Result
100
101 4) Composit Render Result
102 - also read external files etc
103
104 5) Image Files
105 - save file or append in movie
106
107 */
108
109
110 /* ********* globals ******** */
111
112 /* here we store all renders */
113 static struct ListBase RenderList= {NULL, NULL};
114
115 /* hardcopy of current render, used while rendering for speed */
116 Render R;
117
118 /* commandline thread override */
119 static int commandline_threads= 0;
120
121 /* ********* alloc and free ******** */
122
123
124 static volatile int g_break= 0;
125 static int thread_break(void)
126 {
127         return g_break;
128 }
129
130 /* default callbacks, set in each new render */
131 static void result_nothing(RenderResult *rr) {}
132 static void result_rcti_nothing(RenderResult *rr, volatile struct rcti *rect) {}
133 static void stats_nothing(RenderStats *rs) {}
134 static void int_nothing(int val) {}
135 static int void_nothing(void) {return 0;}
136 static void print_error(char *str) {printf("ERROR: %s\n", str);}
137
138 static void stats_background(RenderStats *rs)
139 {
140         extern unsigned long mem_in_use;
141         float megs_used_memory= mem_in_use/(1024.0*1024.0);
142         char str[400], *spos= str;
143         
144         if(rs->convertdone) {
145                 
146                 spos+= sprintf(spos, "Fra:%d Mem:%.2fM ", G.scene->r.cfra, megs_used_memory);
147                 
148                 if(rs->curfield)
149                         spos+= sprintf(spos, "Field %d ", rs->curfield);
150                 if(rs->curblur)
151                         spos+= sprintf(spos, "Blur %d ", rs->curblur);
152                 
153                 if(rs->infostr) {
154                         spos+= sprintf(spos, "| %s", rs->infostr);
155                 }
156                 else {
157                         if(rs->tothalo)
158                                 spos+= sprintf(spos, "Sce: %s Ve:%d Fa:%d Ha:%d La:%d", G.scene->id.name+2, rs->totvert, rs->totface, rs->tothalo, rs->totlamp);
159                         else 
160                                 spos+= sprintf(spos, "Sce: %s Ve:%d Fa:%d La:%d", G.scene->id.name+2, rs->totvert, rs->totface, rs->totlamp);
161                 }
162                 printf(str); printf("\n");
163         }       
164 }
165
166 void RE_FreeRenderResult(RenderResult *res)
167 {
168         if(res==NULL) return;
169
170         while(res->layers.first) {
171                 RenderLayer *rl= res->layers.first;
172                 
173                 if(rl->rectf) MEM_freeN(rl->rectf);
174                 /* acolrect is optionally allocated in shade_tile, only free here since it can be used for drawing */
175                 if(rl->acolrect) MEM_freeN(rl->acolrect);
176                 
177                 while(rl->passes.first) {
178                         RenderPass *rpass= rl->passes.first;
179                         if(rpass->rect) MEM_freeN(rpass->rect);
180                         BLI_remlink(&rl->passes, rpass);
181                         MEM_freeN(rpass);
182                 }
183                 BLI_remlink(&res->layers, rl);
184                 MEM_freeN(rl);
185         }
186         
187         if(res->rect32)
188                 MEM_freeN(res->rect32);
189         if(res->rectz)
190                 MEM_freeN(res->rectz);
191         if(res->rectf)
192                 MEM_freeN(res->rectf);
193         
194         MEM_freeN(res);
195 }
196
197 /* all layers except the active one get temporally pushed away */
198 static void push_render_result(Render *re)
199 {
200         /* officially pushed result should be NULL... error can happen with do_seq */
201         RE_FreeRenderResult(re->pushedresult);
202         
203         re->pushedresult= re->result;
204         re->result= NULL;
205 }
206
207 /* if scemode is R_SINGLE_LAYER, at end of rendering, merge the both render results */
208 static void pop_render_result(Render *re)
209 {
210         
211         if(re->result==NULL) {
212                 printf("pop render result error; no current result!\n");
213                 return;
214         }
215         if(re->pushedresult) {
216                 if(re->pushedresult->rectx==re->result->rectx && re->pushedresult->recty==re->result->recty) {
217                         /* find which layer in pushedresult should be replaced */
218                         SceneRenderLayer *srl;
219                         RenderLayer *rlpush;
220                         RenderLayer *rl= re->result->layers.first;
221                         int nr;
222                         
223                         /* render result should be empty after this */
224                         BLI_remlink(&re->result->layers, rl);
225                         
226                         /* reconstruct render result layers */
227                         for(nr=0, srl= re->scene->r.layers.first; srl; srl= srl->next, nr++) {
228                                 if(nr==re->r.actlay)
229                                         BLI_addtail(&re->result->layers, rl);
230                                 else {
231                                         rlpush= RE_GetRenderLayer(re->pushedresult, srl->name);
232                                         if(rlpush) {
233                                                 BLI_remlink(&re->pushedresult->layers, rlpush);
234                                                 BLI_addtail(&re->result->layers, rlpush);
235                                         }
236                                 }
237                         }
238                 }
239                 
240                 RE_FreeRenderResult(re->pushedresult);
241                 re->pushedresult= NULL;
242         }
243 }
244
245 /* NOTE: OpenEXR only supports 32 chars for layer+pass names
246    In blender we now use max 10 chars for pass, max 20 for layer */
247 static char *get_pass_name(int passtype, int channel)
248 {
249         
250         if(passtype == SCE_PASS_COMBINED) {
251                 if(channel==-1) return "Combined";
252                 if(channel==0) return "Combined.R";
253                 if(channel==1) return "Combined.G";
254                 if(channel==2) return "Combined.B";
255                 return "Combined.A";
256         }
257         if(passtype == SCE_PASS_Z) {
258                 if(channel==-1) return "Depth";
259                 return "Depth.Z";
260         }
261         if(passtype == SCE_PASS_VECTOR) {
262                 if(channel==-1) return "Vector";
263                 if(channel==0) return "Vector.X";
264                 if(channel==1) return "Vector.Y";
265                 if(channel==2) return "Vector.Z";
266                 return "Vector.W";
267         }
268         if(passtype == SCE_PASS_NORMAL) {
269                 if(channel==-1) return "Normal";
270                 if(channel==0) return "Normal.X";
271                 if(channel==1) return "Normal.Y";
272                 return "Normal.Z";
273         }
274         if(passtype == SCE_PASS_UV) {
275                 if(channel==-1) return "UV";
276                 if(channel==0) return "UV.U";
277                 if(channel==1) return "UV.V";
278                 return "UV.A";
279         }
280         if(passtype == SCE_PASS_RGBA) {
281                 if(channel==-1) return "Color";
282                 if(channel==0) return "Color.R";
283                 if(channel==1) return "Color.G";
284                 if(channel==2) return "Color.B";
285                 return "Color.A";
286         }
287         if(passtype == SCE_PASS_DIFFUSE) {
288                 if(channel==-1) return "Diffuse";
289                 if(channel==0) return "Diffuse.R";
290                 if(channel==1) return "Diffuse.G";
291                 return "Diffuse.B";
292         }
293         if(passtype == SCE_PASS_SPEC) {
294                 if(channel==-1) return "Spec";
295                 if(channel==0) return "Spec.R";
296                 if(channel==1) return "Spec.G";
297                 return "Spec.B";
298         }
299         if(passtype == SCE_PASS_SHADOW) {
300                 if(channel==-1) return "Shadow";
301                 if(channel==0) return "Shadow.R";
302                 if(channel==1) return "Shadow.G";
303                 return "Shadow.B";
304         }
305         if(passtype == SCE_PASS_AO) {
306                 if(channel==-1) return "AO";
307                 if(channel==0) return "AO.R";
308                 if(channel==1) return "AO.G";
309                 return "AO.B";
310         }
311         if(passtype == SCE_PASS_REFLECT) {
312                 if(channel==-1) return "Reflect";
313                 if(channel==0) return "Reflect.R";
314                 if(channel==1) return "Reflect.G";
315                 return "Reflect.B";
316         }
317         if(passtype == SCE_PASS_REFRACT) {
318                 if(channel==-1) return "Refract";
319                 if(channel==0) return "Refract.R";
320                 if(channel==1) return "Refract.G";
321                 return "Refract.B";
322         }
323         if(passtype == SCE_PASS_RADIO) {
324                 if(channel==-1) return "Radio";
325                 if(channel==0) return "Radio.R";
326                 if(channel==1) return "Radio.G";
327                 return "Radio.B";
328         }
329         if(passtype == SCE_PASS_INDEXOB) {
330                 if(channel==-1) return "IndexOB";
331                 return "IndexOB.X";
332         }
333         return "Unknown";
334 }
335
336 static int passtype_from_name(char *str)
337 {
338         
339         if(strcmp(str, "Combined")==0)
340                 return SCE_PASS_COMBINED;
341
342         if(strcmp(str, "Depth")==0)
343                 return SCE_PASS_Z;
344
345         if(strcmp(str, "Vector")==0)
346                 return SCE_PASS_VECTOR;
347
348         if(strcmp(str, "Normal")==0)
349                 return SCE_PASS_NORMAL;
350
351         if(strcmp(str, "UV")==0)
352                 return SCE_PASS_UV;
353
354         if(strcmp(str, "Color")==0)
355                 return SCE_PASS_RGBA;
356
357         if(strcmp(str, "Diffuse")==0)
358                 return SCE_PASS_DIFFUSE;
359
360         if(strcmp(str, "Spec")==0)
361                 return SCE_PASS_SPEC;
362
363         if(strcmp(str, "Shadow")==0)
364                 return SCE_PASS_SHADOW;
365         
366         if(strcmp(str, "AO")==0)
367                 return SCE_PASS_AO;
368
369         if(strcmp(str, "Reflect")==0)
370                 return SCE_PASS_REFLECT;
371
372         if(strcmp(str, "Refract")==0)
373                 return SCE_PASS_REFRACT;
374
375         if(strcmp(str, "Radio")==0)
376                 return SCE_PASS_RADIO;
377
378         if(strcmp(str, "IndexOB")==0)
379                 return SCE_PASS_INDEXOB;
380
381         return 0;
382 }
383
384
385
386 static void render_unique_exr_name(Render *re, char *str)
387 {
388         char di[FILE_MAX], name[FILE_MAXFILE], fi[FILE_MAXFILE];
389         
390         BLI_strncpy(di, G.sce, FILE_MAX);
391         BLI_splitdirstring(di, fi);
392         sprintf(name, "%s_%s.exr", fi, re->scene->id.name+2);
393         if(G.background)
394                 BLI_make_file_string("/", str, "/tmp/", name);
395         else
396                 BLI_make_file_string("/", str, U.tempdir, name);
397                 
398 }
399
400 static void render_layer_add_pass(RenderResult *rr, RenderLayer *rl, int channels, int passtype)
401 {
402         char *typestr= get_pass_name(passtype, 0);
403         RenderPass *rpass= MEM_callocN(sizeof(RenderPass), typestr);
404         int rectsize= rr->rectx*rr->recty*channels;
405         
406         BLI_addtail(&rl->passes, rpass);
407         rpass->passtype= passtype;
408         rpass->channels= channels;
409         
410         if(rr->exrhandle) {
411                 int a;
412                 for(a=0; a<channels; a++)
413                         IMB_exr_add_channel(rr->exrhandle, rl->name, get_pass_name(passtype, a), 0, 0, NULL);
414         }
415         else {
416                 if(passtype==SCE_PASS_VECTOR) {
417                         float *rect;
418                         int x;
419                         
420                         /* initialize to max speed */
421                         rect= rpass->rect= MEM_mapallocN(sizeof(float)*rectsize, typestr);
422                         for(x= rectsize-1; x>=0; x--)
423                                 rect[x]= PASS_VECTOR_MAX;
424                 }
425                 else
426                         rpass->rect= MEM_mapallocN(sizeof(float)*rectsize, typestr);
427         }
428 }
429
430 float *RE_RenderLayerGetPass(RenderLayer *rl, int passtype)
431 {
432         RenderPass *rpass;
433         
434         for(rpass=rl->passes.first; rpass; rpass= rpass->next)
435                 if(rpass->passtype== passtype)
436                         return rpass->rect;
437         return NULL;
438 }
439
440 RenderLayer *RE_GetRenderLayer(RenderResult *rr, const char *name)
441 {
442         RenderLayer *rl;
443         
444         if(rr==NULL) return NULL;
445         
446         for(rl= rr->layers.first; rl; rl= rl->next)
447                 if(strncmp(rl->name, name, RE_MAXNAME)==0)
448                         return rl;
449         return NULL;
450 }
451
452 #define RR_USEMEM       0
453 /* called by main render as well for parts */
454 /* will read info from Render *re to define layers */
455 /* called in threads */
456 /* re->winx,winy is coordinate space of entire image, partrct the part within */
457 static RenderResult *new_render_result(Render *re, rcti *partrct, int crop, int savebuffers)
458 {
459         RenderResult *rr;
460         RenderLayer *rl;
461         SceneRenderLayer *srl;
462         int rectx, recty, nr;
463         
464         rectx= partrct->xmax - partrct->xmin;
465         recty= partrct->ymax - partrct->ymin;
466         
467         if(rectx<=0 || recty<=0)
468                 return NULL;
469         
470         rr= MEM_callocN(sizeof(RenderResult), "new render result");
471         rr->rectx= rectx;
472         rr->recty= recty;
473         rr->renrect.xmin= 0; rr->renrect.xmax= rectx-2*crop;
474         /* crop is one or two extra pixels rendered for filtering, is used for merging and display too */
475         rr->crop= crop;
476         
477         /* tilerect is relative coordinates within render disprect. do not subtract crop yet */
478         rr->tilerect.xmin= partrct->xmin - re->disprect.xmin;
479         rr->tilerect.xmax= partrct->xmax - re->disprect.xmax;
480         rr->tilerect.ymin= partrct->ymin - re->disprect.ymin;
481         rr->tilerect.ymax= partrct->ymax - re->disprect.ymax;
482         
483         if(savebuffers) {
484                 rr->exrhandle= IMB_exr_get_handle();
485         }
486         
487         /* check renderdata for amount of layers */
488         for(nr=0, srl= re->r.layers.first; srl; srl= srl->next, nr++) {
489                 
490                 if((re->r.scemode & R_SINGLE_LAYER) && nr!=re->r.actlay)
491                         continue;
492                 if(srl->layflag & SCE_LAY_DISABLE)
493                         continue;
494                 
495                 rl= MEM_callocN(sizeof(RenderLayer), "new render layer");
496                 BLI_addtail(&rr->layers, rl);
497                 
498                 strcpy(rl->name, srl->name);
499                 rl->lay= srl->lay;
500                 rl->layflag= srl->layflag;
501                 rl->passflag= srl->passflag;
502                 rl->pass_xor= srl->pass_xor;
503                 rl->light_override= srl->light_override;
504                 rl->mat_override= srl->mat_override;
505                 
506                 if(rr->exrhandle) {
507                         IMB_exr_add_channel(rr->exrhandle, rl->name, "Combined.R", 0, 0, NULL);
508                         IMB_exr_add_channel(rr->exrhandle, rl->name, "Combined.G", 0, 0, NULL);
509                         IMB_exr_add_channel(rr->exrhandle, rl->name, "Combined.B", 0, 0, NULL);
510                         IMB_exr_add_channel(rr->exrhandle, rl->name, "Combined.A", 0, 0, NULL);
511                 }
512                 else
513                         rl->rectf= MEM_mapallocN(rectx*recty*sizeof(float)*4, "Combined rgba");
514                 
515                 if(srl->passflag  & SCE_PASS_Z)
516                         render_layer_add_pass(rr, rl, 1, SCE_PASS_Z);
517                 if(srl->passflag  & SCE_PASS_VECTOR)
518                         render_layer_add_pass(rr, rl, 4, SCE_PASS_VECTOR);
519                 if(srl->passflag  & SCE_PASS_NORMAL)
520                         render_layer_add_pass(rr, rl, 3, SCE_PASS_NORMAL);
521                 if(srl->passflag  & SCE_PASS_UV) 
522                         render_layer_add_pass(rr, rl, 3, SCE_PASS_UV);
523                 if(srl->passflag  & SCE_PASS_RGBA)
524                         render_layer_add_pass(rr, rl, 4, SCE_PASS_RGBA);
525                 if(srl->passflag  & SCE_PASS_DIFFUSE)
526                         render_layer_add_pass(rr, rl, 3, SCE_PASS_DIFFUSE);
527                 if(srl->passflag  & SCE_PASS_SPEC)
528                         render_layer_add_pass(rr, rl, 3, SCE_PASS_SPEC);
529                 if(re->r.mode & R_SHADOW)
530                         if(srl->passflag  & SCE_PASS_SHADOW)
531                                 render_layer_add_pass(rr, rl, 3, SCE_PASS_SHADOW);
532                 if(re->r.mode & R_RAYTRACE) {
533                         if(srl->passflag  & SCE_PASS_AO)
534                                 render_layer_add_pass(rr, rl, 3, SCE_PASS_AO);
535                         if(srl->passflag  & SCE_PASS_REFLECT)
536                                 render_layer_add_pass(rr, rl, 3, SCE_PASS_REFLECT);
537                         if(srl->passflag  & SCE_PASS_REFRACT)
538                                 render_layer_add_pass(rr, rl, 3, SCE_PASS_REFRACT);
539                 }
540                 if(re->r.mode & R_RADIO)
541                         if(srl->passflag  & SCE_PASS_RADIO)
542                                 render_layer_add_pass(rr, rl, 3, SCE_PASS_RADIO);
543                 if(srl->passflag  & SCE_PASS_INDEXOB)
544                         render_layer_add_pass(rr, rl, 1, SCE_PASS_INDEXOB);
545                 
546         }
547         /* previewrender and envmap don't do layers, so we make a default one */
548         if(rr->layers.first==NULL) {
549                 rl= MEM_callocN(sizeof(RenderLayer), "new render layer");
550                 BLI_addtail(&rr->layers, rl);
551                 
552                 rl->rectf= MEM_mapallocN(rectx*recty*sizeof(float)*4, "prev/env float rgba");
553                 
554                 /* note, this has to be in sync with scene.c */
555                 rl->lay= (1<<20) -1;
556                 rl->layflag= 0x7FFF;    /* solid ztra halo strand */
557                 rl->passflag= SCE_PASS_COMBINED;
558                 
559                 re->r.actlay= 0;
560         }
561         
562         /* border render; calculate offset for use in compositor. compo is centralized coords */
563         rr->xof= re->disprect.xmin + (re->disprect.xmax - re->disprect.xmin)/2 - re->winx/2;
564         rr->yof= re->disprect.ymin + (re->disprect.ymax - re->disprect.ymin)/2 - re->winy/2;
565         
566         return rr;
567 }
568
569 static int render_scene_needs_vector(Render *re)
570 {
571         if(re->r.scemode & R_DOCOMP) {
572                 SceneRenderLayer *srl;
573         
574                 for(srl= re->scene->r.layers.first; srl; srl= srl->next)
575                         if(!(srl->layflag & SCE_LAY_DISABLE))
576                                 if(srl->passflag & SCE_PASS_VECTOR)
577                                         return 1;
578         }
579         return 0;
580 }
581
582 static void do_merge_tile(RenderResult *rr, RenderResult *rrpart, float *target, float *tile, int pixsize)
583 {
584         int y, ofs, copylen, tilex, tiley;
585         
586         copylen= tilex= rrpart->rectx;
587         tiley= rrpart->recty;
588         
589         if(rrpart->crop) {      /* filters add pixel extra */
590                 tile+= pixsize*(rrpart->crop + rrpart->crop*tilex);
591                 
592                 copylen= tilex - 2*rrpart->crop;
593                 tiley -= 2*rrpart->crop;
594                 
595                 ofs= (rrpart->tilerect.ymin + rrpart->crop)*rr->rectx + (rrpart->tilerect.xmin+rrpart->crop);
596                 target+= pixsize*ofs;
597         }
598         else {
599                 ofs= (rrpart->tilerect.ymin*rr->rectx + rrpart->tilerect.xmin);
600                 target+= pixsize*ofs;
601         }
602
603         copylen *= sizeof(float)*pixsize;
604         tilex *= pixsize;
605         ofs= pixsize*rr->rectx;
606
607         for(y=0; y<tiley; y++) {
608                 memcpy(target, tile, copylen);
609                 target+= ofs;
610                 tile+= tilex;
611         }
612 }
613
614 /* used when rendering to a full buffer, or when reading the exr part-layer-pass file */
615 /* no test happens here if it fits... we also assume layers are in sync */
616 /* is used within threads */
617 static void merge_render_result(RenderResult *rr, RenderResult *rrpart)
618 {
619         RenderLayer *rl, *rlp;
620         RenderPass *rpass, *rpassp;
621         
622         for(rl= rr->layers.first, rlp= rrpart->layers.first; rl && rlp; rl= rl->next, rlp= rlp->next) {
623                 
624                 /* combined */
625                 if(rl->rectf && rlp->rectf)
626                         do_merge_tile(rr, rrpart, rl->rectf, rlp->rectf, 4);
627                 
628                 /* passes are allocated in sync */
629                 for(rpass= rl->passes.first, rpassp= rlp->passes.first; rpass && rpassp; rpass= rpass->next, rpassp= rpassp->next) {
630                         do_merge_tile(rr, rrpart, rpass->rect, rpassp->rect, rpass->channels);
631                 }
632         }
633 }
634
635
636 static void save_render_result_tile(Render *re, RenderPart *pa)
637 {
638         RenderResult *rrpart= pa->result;
639         RenderLayer *rlp;
640         RenderPass *rpassp;
641         int offs, partx, party;
642         
643         BLI_lock_thread(LOCK_IMAGE);
644         
645         for(rlp= rrpart->layers.first; rlp; rlp= rlp->next) {
646                 
647                 if(rrpart->crop) {      /* filters add pixel extra */
648                         offs= (rrpart->crop + rrpart->crop*rrpart->rectx);
649                 }
650                 else {
651                         offs= 0;
652                 }
653                 
654                 /* combined */
655                 if(rlp->rectf) {
656                         int a, xstride= 4;
657                         for(a=0; a<xstride; a++)
658                                 IMB_exr_set_channel(re->result->exrhandle, rlp->name, get_pass_name(SCE_PASS_COMBINED, a), 
659                                                                 xstride, xstride*pa->rectx, rlp->rectf+a + xstride*offs);
660                 }
661                 
662                 /* passes are allocated in sync */
663                 for(rpassp= rlp->passes.first; rpassp; rpassp= rpassp->next) {
664                         int a, xstride= rpassp->channels;
665                         for(a=0; a<xstride; a++)
666                                 IMB_exr_set_channel(re->result->exrhandle, rlp->name, get_pass_name(rpassp->passtype, a), 
667                                                                         xstride, xstride*pa->rectx, rpassp->rect+a + xstride*offs);
668                 }
669                 
670         }
671
672         party= rrpart->tilerect.ymin + rrpart->crop;
673         partx= rrpart->tilerect.xmin + rrpart->crop;
674         IMB_exrtile_write_channels(re->result->exrhandle, partx, party);
675
676         BLI_unlock_thread(LOCK_IMAGE);
677
678 }
679
680 static void save_empty_result_tiles(Render *re)
681 {
682         RenderPart *pa;
683         
684         IMB_exrtile_clear_channels(re->result->exrhandle);
685                 
686         for(pa= re->parts.first; pa; pa= pa->next) {
687                 if(pa->ready==0) {
688                         int party= pa->disprect.ymin - re->disprect.ymin + pa->crop;
689                         int partx= pa->disprect.xmin - re->disprect.xmin + pa->crop;
690                         IMB_exrtile_write_channels(re->result->exrhandle, partx, party);
691                 }
692         }
693 }
694
695
696 /* for passes read from files, these have names stored */
697 static char *make_pass_name(RenderPass *rpass, int chan)
698 {
699         static char name[16];
700         int len;
701         
702         BLI_strncpy(name, rpass->name, EXR_PASS_MAXNAME);
703         len= strlen(name);
704         name[len]= '.';
705         name[len+1]= rpass->chan_id[chan];
706         name[len+2]= 0;
707
708         return name;
709 }
710
711 /* filename already made absolute */
712 /* called from within UI, saves both rendered result as a file-read result */
713 void RE_WriteRenderResult(RenderResult *rr, char *filename, int compress)
714 {
715         RenderLayer *rl;
716         RenderPass *rpass;
717         void *exrhandle= IMB_exr_get_handle();
718         
719         /* composite result */
720         if(rr->rectf) {
721                 IMB_exr_add_channel(exrhandle, "Composite", "Combined.R", 4, 4*rr->rectx, rr->rectf);
722                 IMB_exr_add_channel(exrhandle, "Composite", "Combined.G", 4, 4*rr->rectx, rr->rectf+1);
723                 IMB_exr_add_channel(exrhandle, "Composite", "Combined.B", 4, 4*rr->rectx, rr->rectf+2);
724                 IMB_exr_add_channel(exrhandle, "Composite", "Combined.A", 4, 4*rr->rectx, rr->rectf+3);
725         }
726         
727         /* add layers/passes and assign channels */
728         for(rl= rr->layers.first; rl; rl= rl->next) {
729                 
730                 /* combined */
731                 if(rl->rectf) {
732                         int a, xstride= 4;
733                         for(a=0; a<xstride; a++)
734                                 IMB_exr_add_channel(exrhandle, rl->name, get_pass_name(SCE_PASS_COMBINED, a), 
735                                                                         xstride, xstride*rr->rectx, rl->rectf+a);
736                 }
737                 
738                 /* passes are allocated in sync */
739                 for(rpass= rl->passes.first; rpass; rpass= rpass->next) {
740                         int a, xstride= rpass->channels;
741                         for(a=0; a<xstride; a++) {
742                                 if(rpass->passtype)
743                                         IMB_exr_add_channel(exrhandle, rl->name, get_pass_name(rpass->passtype, a), 
744                                                                                 xstride, xstride*rr->rectx, rpass->rect+a);
745                                 else
746                                         IMB_exr_add_channel(exrhandle, rl->name, make_pass_name(rpass, a), 
747                                                                                 xstride, xstride*rr->rectx, rpass->rect+a);
748                         }
749                 }
750         }
751         
752         IMB_exr_begin_write(exrhandle, filename, rr->rectx, rr->recty, compress);
753         
754         IMB_exr_write_channels(exrhandle);
755         IMB_exr_close(exrhandle);
756 }
757
758 /* callbacks for RE_MultilayerConvert */
759 static void *ml_addlayer_cb(void *base, char *str)
760 {
761         RenderResult *rr= base;
762         RenderLayer *rl;
763         
764         rl= MEM_callocN(sizeof(RenderLayer), "new render layer");
765         BLI_addtail(&rr->layers, rl);
766         
767         BLI_strncpy(rl->name, str, EXR_LAY_MAXNAME);
768         return rl;
769 }
770 static void ml_addpass_cb(void *base, void *lay, char *str, float *rect, int totchan, char *chan_id)
771 {
772         RenderLayer *rl= lay;   
773         RenderPass *rpass= MEM_callocN(sizeof(RenderPass), "loaded pass");
774         int a;
775         
776         BLI_addtail(&rl->passes, rpass);
777         rpass->channels= totchan;
778         
779         rpass->passtype= passtype_from_name(str);
780         if(rpass->passtype==0) printf("unknown pass %s\n", str);
781         rl->passflag |= rpass->passtype;
782         
783         BLI_strncpy(rpass->name, str, EXR_PASS_MAXNAME);
784         /* channel id chars */
785         for(a=0; a<totchan; a++)
786                 rpass->chan_id[a]= chan_id[a];
787         
788         rpass->rect= rect;
789 }
790
791 /* from imbuf, if a handle was returned we convert this to render result */
792 RenderResult *RE_MultilayerConvert(void *exrhandle, int rectx, int recty)
793 {
794         RenderResult *rr= MEM_callocN(sizeof(RenderResult), "loaded render result");
795         
796         rr->rectx= rectx;
797         rr->recty= recty;
798         
799         IMB_exr_multilayer_convert(exrhandle, rr, ml_addlayer_cb, ml_addpass_cb);
800         
801         return rr;
802 }
803
804 /* called in end of render, to add names to passes... for UI only */
805 static void renderresult_add_names(RenderResult *rr)
806 {
807         RenderLayer *rl;
808         RenderPass *rpass;
809         
810         for(rl= rr->layers.first; rl; rl= rl->next)
811                 for(rpass= rl->passes.first; rpass; rpass= rpass->next)
812                         strcpy(rpass->name, get_pass_name(rpass->passtype, -1));
813 }
814
815
816 /* only for temp buffer files, makes exact copy of render result */
817 static void read_render_result(Render *re)
818 {
819         RenderLayer *rl;
820         RenderPass *rpass;
821         void *exrhandle= IMB_exr_get_handle();
822         int rectx, recty;
823         char str[FILE_MAX];
824         
825         RE_FreeRenderResult(re->result);
826         re->result= new_render_result(re, &re->disprect, 0, RR_USEMEM);
827
828         render_unique_exr_name(re, str);
829         if(IMB_exr_begin_read(exrhandle, str, &rectx, &recty)==0) {
830                 IMB_exr_close(exrhandle);
831                 printf("cannot read: %s\n", str);
832                 return;
833         }
834         
835         if(rectx!=re->result->rectx || recty!=re->result->recty) {
836                 printf("error in reading render result\n");
837         }
838         else {
839                 for(rl= re->result->layers.first; rl; rl= rl->next) {
840                         
841                         /* combined */
842                         if(rl->rectf) {
843                                 int a, xstride= 4;
844                                 for(a=0; a<xstride; a++)
845                                         IMB_exr_set_channel(exrhandle, rl->name, get_pass_name(SCE_PASS_COMBINED, a), 
846                                                                                 xstride, xstride*rectx, rl->rectf+a);
847                         }
848                         
849                         /* passes are allocated in sync */
850                         for(rpass= rl->passes.first; rpass; rpass= rpass->next) {
851                                 int a, xstride= rpass->channels;
852                                 for(a=0; a<xstride; a++)
853                                         IMB_exr_set_channel(exrhandle, rl->name, get_pass_name(rpass->passtype, a), 
854                                                                                 xstride, xstride*rectx, rpass->rect+a);
855                         }
856                         
857                 }
858                 IMB_exr_read_channels(exrhandle);
859                 renderresult_add_names(re->result);
860         }
861         
862         IMB_exr_close(exrhandle);
863 }
864
865 /* *************************************************** */
866
867 Render *RE_GetRender(const char *name)
868 {
869         Render *re;
870         
871         /* search for existing renders */
872         for(re= RenderList.first; re; re= re->next) {
873                 if(strncmp(re->name, name, RE_MAXNAME)==0) {
874                         break;
875                 }
876         }
877         return re;
878 }
879
880 /* if you want to know exactly what has been done */
881 RenderResult *RE_GetResult(Render *re)
882 {
883         if(re)
884                 return re->result;
885         return NULL;
886 }
887
888 RenderLayer *render_get_active_layer(Render *re, RenderResult *rr)
889 {
890         RenderLayer *rl= BLI_findlink(&rr->layers, re->r.actlay);
891         
892         if(rl) 
893                 return rl;
894         else 
895                 return rr->layers.first;
896 }
897
898
899 /* fill provided result struct with what's currently active or done */
900 void RE_GetResultImage(Render *re, RenderResult *rr)
901 {
902         memset(rr, 0, sizeof(RenderResult));
903
904         if(re && re->result) {
905                 RenderLayer *rl;
906                 
907                 rr->rectx= re->result->rectx;
908                 rr->recty= re->result->recty;
909                 
910                 rr->rectf= re->result->rectf;
911                 rr->rectz= re->result->rectz;
912                 rr->rect32= re->result->rect32;
913                 
914                 /* active layer */
915                 rl= render_get_active_layer(re, re->result);
916
917                 if(rl) {
918                         if(rr->rectf==NULL)
919                                 rr->rectf= rl->rectf;
920                         if(rr->rectz==NULL)
921                                 rr->rectz= RE_RenderLayerGetPass(rl, SCE_PASS_Z);       
922                 }
923         }
924 }
925
926 #define FTOCHAR(val) val<=0.0f?0: (val>=1.0f?255: (char)(255.0f*val))
927 /* caller is responsible for allocating rect in correct size! */
928 void RE_ResultGet32(Render *re, unsigned int *rect)
929 {
930         RenderResult rres;
931         
932         RE_GetResultImage(re, &rres);
933         if(rres.rect32) 
934                 memcpy(rect, rres.rect32, sizeof(int)*rres.rectx*rres.recty);
935         else if(rres.rectf) {
936                 float *fp= rres.rectf;
937                 int tot= rres.rectx*rres.recty;
938                 char *cp= (char *)rect;
939                 
940                 for(;tot>0; tot--, cp+=4, fp+=4) {
941                         cp[0] = FTOCHAR(fp[0]);
942                         cp[1] = FTOCHAR(fp[1]);
943                         cp[2] = FTOCHAR(fp[2]);
944                         cp[3] = FTOCHAR(fp[3]);
945                 }
946         }
947         else
948                 /* else fill with black */
949                 memset(rect, 0, sizeof(int)*re->rectx*re->recty);
950 }
951
952
953 RenderStats *RE_GetStats(Render *re)
954 {
955         return &re->i;
956 }
957
958 Render *RE_NewRender(const char *name)
959 {
960         Render *re;
961         
962         /* only one render per name exists */
963         re= RE_GetRender(name);
964         if(re==NULL) {
965                 
966                 /* new render data struct */
967                 re= MEM_callocN(sizeof(Render), "new render");
968                 BLI_addtail(&RenderList, re);
969                 strncpy(re->name, name, RE_MAXNAME);
970         }
971         
972         /* set default empty callbacks */
973         re->display_init= result_nothing;
974         re->display_clear= result_nothing;
975         re->display_draw= result_rcti_nothing;
976         re->timecursor= int_nothing;
977         re->test_break= void_nothing;
978         re->test_return= void_nothing;
979         re->error= print_error;
980         if(G.background)
981                 re->stats_draw= stats_background;
982         else
983                 re->stats_draw= stats_nothing;
984         
985         /* init some variables */
986         re->ycor= 1.0f;
987         
988         return re;
989 }
990
991 /* only call this while you know it will remove the link too */
992 void RE_FreeRender(Render *re)
993 {
994         
995         free_renderdata_tables(re);
996         free_sample_tables(re);
997         
998         RE_FreeRenderResult(re->result);
999         RE_FreeRenderResult(re->pushedresult);
1000         
1001         BLI_remlink(&RenderList, re);
1002         MEM_freeN(re);
1003 }
1004
1005 /* exit blender */
1006 void RE_FreeAllRender(void)
1007 {
1008         while(RenderList.first) {
1009                 RE_FreeRender(RenderList.first);
1010         }
1011 }
1012
1013 /* ********* initialize state ******** */
1014
1015
1016 /* what doesn't change during entire render sequence */
1017 /* disprect is optional, if NULL it assumes full window render */
1018 void RE_InitState(Render *re, RenderData *rd, int winx, int winy, rcti *disprect)
1019 {
1020         re->ok= TRUE;   /* maybe flag */
1021         
1022         re->i.starttime= PIL_check_seconds_timer();
1023         re->r= *rd;             /* hardcopy */
1024         
1025         re->winx= winx;
1026         re->winy= winy;
1027         if(disprect) {
1028                 re->disprect= *disprect;
1029                 re->rectx= disprect->xmax-disprect->xmin;
1030                 re->recty= disprect->ymax-disprect->ymin;
1031         }
1032         else {
1033                 re->disprect.xmin= re->disprect.ymin= 0;
1034                 re->disprect.xmax= winx;
1035                 re->disprect.ymax= winy;
1036                 re->rectx= winx;
1037                 re->recty= winy;
1038         }
1039         
1040         if(re->rectx < 2 || re->recty < 2) {
1041                 re->error("Image too small");
1042                 re->ok= 0;
1043         }
1044         else {
1045                 /* check state variables, osa? */
1046                 if(re->r.mode & (R_OSA)) {
1047                         re->osa= re->r.osa;
1048                         if(re->osa>16) re->osa= 16;
1049                 }
1050                 else re->osa= 0;
1051                 
1052                 /* always call, checks for gamma, gamma tables and jitter too */
1053                 make_sample_tables(re); 
1054                 
1055                 /* make empty render result, so display callbacks can initialize */
1056                 RE_FreeRenderResult(re->result);
1057                 re->result= MEM_callocN(sizeof(RenderResult), "new render result");
1058                 re->result->rectx= re->rectx;
1059                 re->result->recty= re->recty;
1060                 
1061                 /* we clip faces with a minimum of 2 pixel boundary outside of image border. see zbuf.c */
1062                 re->clipcrop= 1.0f + 2.0f/(float)(re->winx>re->winy?re->winy:re->winx);
1063                 
1064                 if(commandline_threads>0 && commandline_threads<=BLENDER_MAX_THREADS)
1065                         re->r.threads= commandline_threads;
1066         }
1067 }
1068
1069 void RE_SetDispRect (struct Render *re, rcti *disprect)
1070 {
1071         re->disprect= *disprect;
1072         re->rectx= disprect->xmax-disprect->xmin;
1073         re->recty= disprect->ymax-disprect->ymin;
1074         
1075         /* initialize render result */
1076         RE_FreeRenderResult(re->result);
1077         re->result= new_render_result(re, &re->disprect, 0, RR_USEMEM);
1078 }
1079
1080 void RE_SetWindow(Render *re, rctf *viewplane, float clipsta, float clipend)
1081 {
1082         /* re->ok flag? */
1083         
1084         re->viewplane= *viewplane;
1085         re->clipsta= clipsta;
1086         re->clipend= clipend;
1087         re->r.mode &= ~R_ORTHO;
1088
1089         i_window(re->viewplane.xmin, re->viewplane.xmax, re->viewplane.ymin, re->viewplane.ymax, re->clipsta, re->clipend, re->winmat);
1090         
1091 }
1092
1093 void RE_SetOrtho(Render *re, rctf *viewplane, float clipsta, float clipend)
1094 {
1095         /* re->ok flag? */
1096         
1097         re->viewplane= *viewplane;
1098         re->clipsta= clipsta;
1099         re->clipend= clipend;
1100         re->r.mode |= R_ORTHO;
1101
1102         i_ortho(re->viewplane.xmin, re->viewplane.xmax, re->viewplane.ymin, re->viewplane.ymax, re->clipsta, re->clipend, re->winmat);
1103 }
1104
1105 void RE_SetView(Render *re, float mat[][4])
1106 {
1107         /* re->ok flag? */
1108         Mat4CpyMat4(re->viewmat, mat);
1109         Mat4Invert(re->viewinv, re->viewmat);
1110 }
1111
1112 /* image and movie output has to move to either imbuf or kernel */
1113 void RE_display_init_cb(Render *re, void (*f)(RenderResult *rr))
1114 {
1115         re->display_init= f;
1116 }
1117 void RE_display_clear_cb(Render *re, void (*f)(RenderResult *rr))
1118 {
1119         re->display_clear= f;
1120 }
1121 void RE_display_draw_cb(Render *re, void (*f)(RenderResult *rr, volatile rcti *rect))
1122 {
1123         re->display_draw= f;
1124 }
1125
1126 void RE_stats_draw_cb(Render *re, void (*f)(RenderStats *rs))
1127 {
1128         re->stats_draw= f;
1129 }
1130 void RE_timecursor_cb(Render *re, void (*f)(int))
1131 {
1132         re->timecursor= f;
1133 }
1134
1135 void RE_test_break_cb(Render *re, int (*f)(void))
1136 {
1137         re->test_break= f;
1138 }
1139 void RE_test_return_cb(Render *re, int (*f)(void))
1140 {
1141         re->test_return= f;
1142 }
1143 void RE_error_cb(Render *re, void (*f)(char *str))
1144 {
1145         re->error= f;
1146 }
1147
1148
1149 /* ********* add object data (later) ******** */
1150
1151 /* object is considered fully prepared on correct time etc */
1152 /* includes lights */
1153 void RE_AddObject(Render *re, Object *ob)
1154 {
1155         
1156 }
1157
1158 /* *************************************** */
1159
1160 static void *do_part_thread(void *pa_v)
1161 {
1162         RenderPart *pa= pa_v;
1163         
1164         /* need to return nicely all parts on esc */
1165         if(R.test_break()==0) {
1166                 
1167                 pa->result= new_render_result(&R, &pa->disprect, pa->crop, RR_USEMEM);
1168                 
1169                 if(R.osa)
1170                         zbufshadeDA_tile(pa);
1171                 else
1172                         zbufshade_tile(pa);
1173                 
1174                 /* merge too on break! */
1175                 if(R.result->exrhandle)
1176                         save_render_result_tile(&R, pa);
1177                 else
1178                         merge_render_result(R.result, pa->result);
1179         }
1180         
1181         pa->ready= 1;
1182         
1183         return NULL;
1184 }
1185
1186 /* returns with render result filled, not threaded, used for preview now only */
1187 static void render_tile_processor(Render *re, int firsttile)
1188 {
1189         RenderPart *pa;
1190         
1191         if(re->test_break())
1192                 return;
1193
1194         /* hrmf... exception, this is used for preview render, re-entrant, so render result has to be re-used */
1195         if(re->result==NULL || re->result->layers.first==NULL) {
1196                 if(re->result) RE_FreeRenderResult(re->result);
1197                 re->result= new_render_result(re, &re->disprect, 0, RR_USEMEM);
1198         }
1199         
1200         re->stats_draw(&re->i);
1201  
1202         if(re->result==NULL)
1203                 return;
1204         
1205         initparts(re);
1206         
1207         /* assuming no new data gets added to dbase... */
1208         R= *re;
1209         
1210         for(pa= re->parts.first; pa; pa= pa->next) {
1211                 if(firsttile) {
1212                         re->i.partsdone++;      /* was reset in initparts */
1213                         firsttile--;
1214                 }
1215                 else {
1216                         do_part_thread(pa);
1217                         
1218                         if(pa->result) {
1219                                 if(!re->test_break()) {
1220                                         re->display_draw(pa->result, NULL);
1221                                         
1222                                         re->i.partsdone++;
1223                                         re->stats_draw(&re->i);
1224                                 }
1225                                 RE_FreeRenderResult(pa->result);
1226                                 pa->result= NULL;
1227                         }               
1228                         if(re->test_break())
1229                                 break;
1230                 }
1231         }
1232         
1233         freeparts(re);
1234 }
1235
1236 /* calculus for how much 1 pixel rendered should rotate the 3d geometry */
1237 /* is not that simple, needs to be corrected for errors of larger viewplane sizes */
1238 /* called in initrender.c, initparts() and convertblender.c, for speedvectors */
1239 float panorama_pixel_rot(Render *re)
1240 {
1241         float psize, phi, xfac;
1242         
1243         /* size of 1 pixel mapped to viewplane coords */
1244         psize= (re->viewplane.xmax-re->viewplane.xmin)/(float)re->winx;
1245         /* angle of a pixel */
1246         phi= atan(psize/re->clipsta);
1247         
1248         /* correction factor for viewplane shifting, first calculate how much the viewplane angle is */
1249         xfac= ((re->viewplane.xmax-re->viewplane.xmin))/(float)re->xparts;
1250         xfac= atan(0.5f*xfac/re->clipsta); 
1251         /* and how much the same viewplane angle is wrapped */
1252         psize= 0.5f*phi*((float)re->partx);
1253         
1254         /* the ratio applied to final per-pixel angle */
1255         phi*= xfac/psize;
1256         
1257         return phi;
1258 }
1259
1260 /* call when all parts stopped rendering, to find the next Y slice */
1261 /* if slice found, it rotates the dbase */
1262 static RenderPart *find_next_pano_slice(Render *re, int *minx, rctf *viewplane)
1263 {
1264         RenderPart *pa, *best= NULL;
1265         
1266         *minx= re->winx;
1267         
1268         /* most left part of the non-rendering parts */
1269         for(pa= re->parts.first; pa; pa= pa->next) {
1270                 if(pa->ready==0 && pa->nr==0) {
1271                         if(pa->disprect.xmin < *minx) {
1272                                 best= pa;
1273                                 *minx= pa->disprect.xmin;
1274                         }
1275                 }
1276         }
1277                         
1278         if(best) {
1279                 float phi= panorama_pixel_rot(re);
1280
1281                 R.panodxp= (re->winx - (best->disprect.xmin + best->disprect.xmax) )/2;
1282                 R.panodxv= ((viewplane->xmax-viewplane->xmin)*R.panodxp)/(float)R.winx;
1283                 
1284                 /* shift viewplane */
1285                 R.viewplane.xmin = viewplane->xmin + R.panodxv;
1286                 R.viewplane.xmax = viewplane->xmax + R.panodxv;
1287                 RE_SetWindow(re, &R.viewplane, R.clipsta, R.clipend);
1288                 Mat4CpyMat4(R.winmat, re->winmat);
1289                 
1290                 /* rotate database according to part coordinates */
1291                 project_renderdata(re, projectverto, 1, -R.panodxp*phi);
1292                 R.panosi= sin(R.panodxp*phi);
1293                 R.panoco= cos(R.panodxp*phi);
1294         }
1295         return best;
1296 }
1297
1298 static RenderPart *find_next_part(Render *re, int minx)
1299 {
1300         RenderPart *pa, *best= NULL;
1301         int centx=re->winx/2, centy=re->winy/2, tot=1;
1302         int mindist, distx, disty;
1303         
1304         /* find center of rendered parts, image center counts for 1 too */
1305         for(pa= re->parts.first; pa; pa= pa->next) {
1306                 if(pa->ready) {
1307                         centx+= (pa->disprect.xmin+pa->disprect.xmax)/2;
1308                         centy+= (pa->disprect.ymin+pa->disprect.ymax)/2;
1309                         tot++;
1310                 }
1311         }
1312         centx/=tot;
1313         centy/=tot;
1314         
1315         /* closest of the non-rendering parts */
1316         mindist= re->winx*re->winy;
1317         for(pa= re->parts.first; pa; pa= pa->next) {
1318                 if(pa->ready==0 && pa->nr==0) {
1319                         distx= centx - (pa->disprect.xmin+pa->disprect.xmax)/2;
1320                         disty= centy - (pa->disprect.ymin+pa->disprect.ymax)/2;
1321                         distx= (int)sqrt(distx*distx + disty*disty);
1322                         if(distx<mindist) {
1323                                 if(re->r.mode & R_PANORAMA) {
1324                                         if(pa->disprect.xmin==minx) {
1325                                                 best= pa;
1326                                                 mindist= distx;
1327                                         }
1328                                 }
1329                                 else {
1330                                         best= pa;
1331                                         mindist= distx;
1332                                 }
1333                         }
1334                 }
1335         }
1336         return best;
1337 }
1338
1339 static void print_part_stats(Render *re, RenderPart *pa)
1340 {
1341         char str[64];
1342         
1343         sprintf(str, "Part %d-%d", pa->nr, re->i.totpart);
1344         re->i.infostr= str;
1345         re->stats_draw(&re->i);
1346         re->i.infostr= NULL;
1347 }
1348
1349 static void threaded_tile_processor(Render *re)
1350 {
1351         ListBase threads;
1352         RenderPart *pa, *nextpa;
1353         RenderResult *rr;
1354         rctf viewplane= re->viewplane;
1355         int rendering=1, counter= 1, drawtimer=0, hasdrawn, minx=0;
1356         
1357         /* first step; the entire render result, or prepare exr buffer saving */
1358         RE_FreeRenderResult(re->result);
1359         rr= re->result= new_render_result(re, &re->disprect, 0, re->r.scemode & R_EXR_TILE_FILE);
1360         
1361         if(rr==NULL)
1362                 return;
1363         /* warning; no return here without closing exr file */
1364 //      if(re->re->test_break())
1365 //              return;
1366         
1367         initparts(re);
1368         
1369         if(rr->exrhandle) {
1370                 char str[FILE_MAX];
1371                 
1372                 render_unique_exr_name(re, str);
1373                 
1374                 printf("write exr tmp file, %dx%d, %s\n", rr->rectx, rr->recty, str);
1375                 IMB_exrtile_begin_write(rr->exrhandle, str, rr->rectx, rr->recty, rr->rectx/re->xparts, rr->recty/re->yparts);
1376         }
1377         
1378         BLI_init_threads(&threads, do_part_thread, re->r.threads);
1379         
1380         /* assuming no new data gets added to dbase... */
1381         R= *re;
1382         
1383         /* set threadsafe break */
1384         R.test_break= thread_break;
1385         
1386         /* timer loop demands to sleep when no parts are left, so we enter loop with a part */
1387         if(re->r.mode & R_PANORAMA)
1388                 nextpa= find_next_pano_slice(re, &minx, &viewplane);
1389         else
1390                 nextpa= find_next_part(re, 0);
1391         
1392         while(rendering) {
1393                 
1394                 if(re->test_break())
1395                         PIL_sleep_ms(50);
1396                 else if(nextpa && BLI_available_threads(&threads)) {
1397                         drawtimer= 0;
1398                         nextpa->nr= counter++;  /* for nicest part, and for stats */
1399                         nextpa->thread= BLI_available_thread_index(&threads);   /* sample index */
1400                         BLI_insert_thread(&threads, nextpa);
1401
1402                         nextpa= find_next_part(re, minx);
1403                 }
1404                 else if(re->r.mode & R_PANORAMA) {
1405                         if(nextpa==NULL && BLI_available_threads(&threads)==re->r.threads)
1406                                 nextpa= find_next_pano_slice(re, &minx, &viewplane);
1407                         else {
1408                                 PIL_sleep_ms(50);
1409                                 drawtimer++;
1410                         }
1411                 }
1412                 else {
1413                         PIL_sleep_ms(50);
1414                         drawtimer++;
1415                 }
1416                 
1417                 /* check for ready ones to display, and if we need to continue */
1418                 rendering= 0;
1419                 hasdrawn= 0;
1420                 for(pa= re->parts.first; pa; pa= pa->next) {
1421                         if(pa->ready) {
1422                                 
1423                                 BLI_remove_thread(&threads, pa);
1424                                 
1425                                 if(pa->result) {
1426                                         re->display_draw(pa->result, NULL);
1427                                         print_part_stats(re, pa);
1428                                         
1429                                         RE_FreeRenderResult(pa->result);
1430                                         pa->result= NULL;
1431                                         re->i.partsdone++;
1432                                         hasdrawn= 1;
1433                                 }
1434                         }
1435                         else {
1436                                 rendering= 1;
1437                                 if(pa->nr && pa->result && drawtimer>20) {
1438                                         re->display_draw(pa->result, &pa->result->renrect);
1439                                         hasdrawn= 1;
1440                                 }
1441                         }
1442                 }
1443                 if(hasdrawn)
1444                         drawtimer= 0;
1445
1446                 /* on break, wait for all slots to get freed */
1447                 if( (g_break=re->test_break()) && BLI_available_threads(&threads)==re->r.threads)
1448                         rendering= 0;
1449                 
1450         }
1451         
1452         if(rr->exrhandle) {
1453                 save_empty_result_tiles(re);
1454                 IMB_exr_close(rr->exrhandle);
1455                 rr->exrhandle= NULL;
1456                 read_render_result(re);
1457         }
1458         
1459         /* unset threadsafety */
1460         g_break= 0;
1461         
1462         BLI_end_threads(&threads);
1463         freeparts(re);
1464 }
1465
1466 /* currently only called by preview renders and envmap */
1467 void RE_TileProcessor(Render *re, int firsttile)
1468 {
1469         /* the partsdone variable has to be reset to firsttile, to survive esc before it was set to zero */
1470         
1471         re->i.partsdone= firsttile;
1472
1473         re->i.starttime= PIL_check_seconds_timer();
1474
1475         //      threaded_tile_processor(re);
1476         render_tile_processor(re, firsttile);
1477                 
1478         re->i.lastframetime= PIL_check_seconds_timer()- re->i.starttime;
1479         re->stats_draw(&re->i);
1480 }
1481
1482
1483 /* ************  This part uses API, for rendering Blender scenes ********** */
1484
1485 static void do_render_3d(Render *re)
1486 {
1487         
1488 //      re->cfra= cfra; /* <- unused! */
1489         
1490         /* make render verts/faces/halos/lamps */
1491         if(render_scene_needs_vector(re))
1492                 RE_Database_FromScene_Vectors(re, re->scene);
1493         else
1494            RE_Database_FromScene(re, re->scene, 1);
1495         
1496         threaded_tile_processor(re);
1497         
1498         /* do left-over 3d post effects (flares) */
1499         if(re->flag & R_HALO)
1500                 if(!re->test_break())
1501                         add_halo_flare(re);
1502
1503         
1504         /* free all render verts etc */
1505         RE_Database_Free(re);
1506 }
1507
1508 /* called by blur loop, accumulate renderlayers */
1509 static void addblur_rect(RenderResult *rr, float *rectf, float *rectf1, float blurfac, int channels)
1510 {
1511         float mfac= 1.0f - blurfac;
1512         int a, b, stride= channels*rr->rectx;
1513         int len= stride*sizeof(float);
1514         
1515         for(a=0; a<rr->recty; a++) {
1516                 if(blurfac==1.0f) {
1517                         memcpy(rectf, rectf1, len);
1518                 }
1519                 else {
1520                         float *rf= rectf, *rf1= rectf1;
1521                         
1522                         for( b= rr->rectx*channels; b>0; b--, rf++, rf1++) {
1523                                 rf[0]= mfac*rf[0] + blurfac*rf1[0];
1524                         }
1525                 }
1526                 rectf+= stride;
1527                 rectf1+= stride;
1528         }
1529 }
1530
1531 /* called by blur loop, accumulate renderlayers */
1532 static void merge_renderresult_blur(RenderResult *rr, RenderResult *brr, float blurfac)
1533 {
1534         RenderLayer *rl, *rl1;
1535         RenderPass *rpass, *rpass1;
1536         
1537         rl1= brr->layers.first;
1538         for(rl= rr->layers.first; rl && rl1; rl= rl->next, rl1= rl1->next) {
1539                 
1540                 /* combined */
1541                 if(rl->rectf && rl1->rectf)
1542                         addblur_rect(rr, rl->rectf, rl1->rectf, blurfac, 4);
1543                 
1544                 /* passes are allocated in sync */
1545                 rpass1= rl1->passes.first;
1546                 for(rpass= rl->passes.first; rpass && rpass1; rpass= rpass->next, rpass1= rpass1->next) {
1547                         addblur_rect(rr, rpass->rect, rpass1->rect, blurfac, rpass->channels);
1548                 }
1549         }
1550 }
1551
1552 /* main blur loop, can be called by fields too */
1553 static void do_render_blur_3d(Render *re)
1554 {
1555         RenderResult *rres;
1556         float blurfac;
1557         int blur= re->r.osa;
1558         
1559         /* create accumulation render result */
1560         rres= new_render_result(re, &re->disprect, 0, RR_USEMEM);
1561         
1562         /* do the blur steps */
1563         while(blur--) {
1564                 set_mblur_offs( re->r.blurfac*((float)(re->r.osa-blur))/(float)re->r.osa );
1565                 
1566                 re->i.curblur= re->r.osa-blur;  /* stats */
1567                 
1568                 do_render_3d(re);
1569                 
1570                 blurfac= 1.0f/(float)(re->r.osa-blur);
1571                 
1572                 merge_renderresult_blur(rres, re->result, blurfac);
1573                 if(re->test_break()) break;
1574         }
1575         
1576         /* swap results */
1577         RE_FreeRenderResult(re->result);
1578         re->result= rres;
1579         
1580         set_mblur_offs(0.0f);
1581         re->i.curblur= 0;       /* stats */
1582         
1583         /* weak... the display callback wants an active renderlayer pointer... */
1584         re->result->renlay= render_get_active_layer(re, re->result);
1585         re->display_draw(re->result, NULL);     
1586 }
1587
1588
1589 /* function assumes rectf1 and rectf2 to be half size of rectf */
1590 static void interleave_rect(RenderResult *rr, float *rectf, float *rectf1, float *rectf2, int channels)
1591 {
1592         int a, stride= channels*rr->rectx;
1593         int len= stride*sizeof(float);
1594         
1595         for(a=0; a<rr->recty; a+=2) {
1596                 memcpy(rectf, rectf1, len);
1597                 rectf+= stride;
1598                 rectf1+= stride;
1599                 memcpy(rectf, rectf2, len);
1600                 rectf+= stride;
1601                 rectf2+= stride;
1602         }
1603 }
1604
1605 /* merge render results of 2 fields */
1606 static void merge_renderresult_fields(RenderResult *rr, RenderResult *rr1, RenderResult *rr2)
1607 {
1608         RenderLayer *rl, *rl1, *rl2;
1609         RenderPass *rpass, *rpass1, *rpass2;
1610         
1611         rl1= rr1->layers.first;
1612         rl2= rr2->layers.first;
1613         for(rl= rr->layers.first; rl && rl1 && rl2; rl= rl->next, rl1= rl1->next, rl2= rl2->next) {
1614                 
1615                 /* combined */
1616                 if(rl->rectf && rl1->rectf && rl2->rectf)
1617                         interleave_rect(rr, rl->rectf, rl1->rectf, rl2->rectf, 4);
1618                 
1619                 /* passes are allocated in sync */
1620                 rpass1= rl1->passes.first;
1621                 rpass2= rl2->passes.first;
1622                 for(rpass= rl->passes.first; rpass && rpass1 && rpass2; rpass= rpass->next, rpass1= rpass1->next, rpass2= rpass2->next) {
1623                         interleave_rect(rr, rpass->rect, rpass1->rect, rpass2->rect, rpass->channels);
1624                 }
1625         }
1626 }
1627
1628
1629 /* interleaves 2 frames */
1630 static void do_render_fields_3d(Render *re)
1631 {
1632         RenderResult *rr1, *rr2= NULL;
1633         
1634         /* no render result was created, we can safely halve render y */
1635         re->winy /= 2;
1636         re->recty /= 2;
1637         re->disprect.ymin /= 2;
1638         re->disprect.ymax /= 2;
1639         
1640         re->i.curfield= 1;      /* stats */
1641         
1642         /* first field, we have to call camera routine for correct aspect and subpixel offset */
1643         RE_SetCamera(re, re->scene->camera);
1644         if(re->r.mode & R_MBLUR)
1645                 do_render_blur_3d(re);
1646         else
1647                 do_render_3d(re);
1648         rr1= re->result;
1649         re->result= NULL;
1650         
1651         /* second field */
1652         if(!re->test_break()) {
1653                 
1654                 re->i.curfield= 2;      /* stats */
1655                 
1656                 re->flag |= R_SEC_FIELD;
1657                 if((re->r.mode & R_FIELDSTILL)==0) 
1658                         set_field_offs(0.5f);
1659                 RE_SetCamera(re, re->scene->camera);
1660                 if(re->r.mode & R_MBLUR)
1661                         do_render_blur_3d(re);
1662                 else
1663                         do_render_3d(re);
1664                 re->flag &= ~R_SEC_FIELD;
1665                 set_field_offs(0.0f);
1666                 
1667                 rr2= re->result;
1668         }
1669         
1670         /* allocate original height new buffers */
1671         re->winy *= 2;
1672         re->recty *= 2;
1673         re->disprect.ymin *= 2;
1674         re->disprect.ymax *= 2;
1675         re->result= new_render_result(re, &re->disprect, 0, RR_USEMEM);
1676         
1677         if(rr2) {
1678                 if(re->r.mode & R_ODDFIELD)
1679                         merge_renderresult_fields(re->result, rr2, rr1);
1680                 else
1681                         merge_renderresult_fields(re->result, rr1, rr2);
1682                 
1683                 RE_FreeRenderResult(rr2);
1684         }
1685         RE_FreeRenderResult(rr1);
1686         
1687         re->i.curfield= 0;      /* stats */
1688         
1689         /* weak... the display callback wants an active renderlayer pointer... */
1690         re->result->renlay= render_get_active_layer(re, re->result);
1691         re->display_draw(re->result, NULL);
1692 }
1693
1694 static void load_backbuffer(Render *re)
1695 {
1696         if(re->r.alphamode == R_ADDSKY) {
1697                 ImBuf *ibuf;
1698                 char name[256];
1699                 
1700                 strcpy(name, re->r.backbuf);
1701                 BLI_convertstringcode(name, G.sce, re->r.cfra);
1702                 
1703                 if(re->backbuf) {
1704                         re->backbuf->id.us--;
1705                         if(re->backbuf->id.us<1)
1706                                 BKE_image_signal(re->backbuf, NULL, IMA_SIGNAL_RELOAD);
1707                 }
1708                 
1709                 re->backbuf= BKE_add_image_file(name);
1710                 ibuf= BKE_image_get_ibuf(re->backbuf, NULL);
1711                 if(ibuf==NULL) {
1712                         // error() doesnt work with render window open
1713                         //error("No backbuf there!");
1714                         printf("Error: No backbuf %s\n", name);
1715                 }
1716                 else {
1717                         if (re->r.mode & R_FIELDS)
1718                                 image_de_interlace(re->backbuf, re->r.mode & R_ODDFIELD);
1719                 }
1720         }
1721 }
1722
1723 /* main render routine, no compositing */
1724 static void do_render_fields_blur_3d(Render *re)
1725 {
1726         /* also check for camera here */
1727         if(re->scene->camera==NULL) {
1728                 printf("ERROR: Cannot render, no camera\n");
1729                 G.afbreek= 1;
1730                 return;
1731         }
1732         
1733         /* backbuffer initialize */
1734         if(re->r.bufflag & 1)
1735                 load_backbuffer(re);
1736
1737         /* now use renderdata and camera to set viewplane */
1738         RE_SetCamera(re, re->scene->camera);
1739         
1740         if(re->r.mode & R_FIELDS)
1741                 do_render_fields_3d(re);
1742         else if(re->r.mode & R_MBLUR)
1743                 do_render_blur_3d(re);
1744         else
1745                 do_render_3d(re);
1746         
1747         /* when border render, check if we have to insert it in black */
1748         if(re->result) {
1749                 if(re->r.mode & R_BORDER) {
1750                         if((re->r.mode & R_CROP)==0) {
1751                                 RenderResult *rres;
1752                                 
1753                                 /* sub-rect for merge call later on */
1754                                 re->result->tilerect= re->disprect;
1755                                 
1756                                 /* this copying sequence could become function? */
1757                                 re->disprect.xmin= re->disprect.ymin= 0;
1758                                 re->disprect.xmax= re->winx;
1759                                 re->disprect.ymax= re->winy;
1760                                 re->rectx= re->winx;
1761                                 re->recty= re->winy;
1762                                 
1763                                 rres= new_render_result(re, &re->disprect, 0, RR_USEMEM);
1764                                 
1765                                 merge_render_result(rres, re->result);
1766                                 RE_FreeRenderResult(re->result);
1767                                 re->result= rres;
1768                                 
1769                                 /* weak... the display callback wants an active renderlayer pointer... */
1770                                 re->result->renlay= render_get_active_layer(re, re->result);
1771                                 
1772                                 re->display_init(re->result);
1773                                 re->display_draw(re->result, NULL);
1774                         }
1775                 }
1776         }
1777 }
1778
1779
1780 /* within context of current Render *re, render another scene.
1781    it uses current render image size and disprect, but doesn't execute composite
1782 */
1783 static void render_scene(Render *re, Scene *sce, int cfra)
1784 {
1785         Render *resc= RE_NewRender(sce->id.name);
1786         
1787         sce->r.cfra= cfra;
1788                 
1789         /* initial setup */
1790         RE_InitState(resc, &sce->r, re->winx, re->winy, &re->disprect);
1791         
1792         /* this to enable this scene to create speed vectors */
1793         resc->r.scemode |= R_DOCOMP;
1794         
1795         /* still unsure entity this... */
1796         resc->scene= sce;
1797         
1798         /* ensure scene has depsgraph, base flags etc OK. Warning... also sets G.scene */
1799         set_scene_bg(sce);
1800
1801         /* copy callbacks */
1802         resc->display_draw= re->display_draw;
1803         resc->test_break= re->test_break;
1804         resc->stats_draw= re->stats_draw;
1805         
1806         do_render_fields_blur_3d(resc);
1807 }
1808
1809 static void ntree_render_scenes(Render *re)
1810 {
1811         bNode *node;
1812         int cfra= re->scene->r.cfra;
1813         
1814         if(re->scene->nodetree==NULL) return;
1815         
1816         /* check for render-layers nodes using other scenes, we tag them LIB_DOIT */
1817         for(node= re->scene->nodetree->nodes.first; node; node= node->next) {
1818                 if(node->type==CMP_NODE_R_LAYERS) {
1819                         if(node->id) {
1820                                 if(node->id != (ID *)re->scene)
1821                                         node->id->flag |= LIB_DOIT;
1822                                 else
1823                                         node->id->flag &= ~LIB_DOIT;
1824                         }
1825                 }
1826         }
1827         
1828         /* now foreach render-result node tagged we do a full render */
1829         /* results are stored in a way compisitor will find it */
1830         for(node= re->scene->nodetree->nodes.first; node; node= node->next) {
1831                 if(node->type==CMP_NODE_R_LAYERS) {
1832                         if(node->id && node->id != (ID *)re->scene) {
1833                                 if(node->id->flag & LIB_DOIT) {
1834                                         render_scene(re, (Scene *)node->id, cfra);
1835                                         node->id->flag &= ~LIB_DOIT;
1836                                 }
1837                         }
1838                 }
1839         }
1840         
1841         /* still the global... */
1842         if(G.scene!=re->scene)
1843                 set_scene_bg(re->scene);
1844         
1845 }
1846
1847 /* helper call to detect if theres a composite with render-result node */
1848 static int composite_needs_render(Scene *sce)
1849 {
1850         bNodeTree *ntree= sce->nodetree;
1851         bNode *node;
1852         
1853         if(ntree==NULL) return 1;
1854         if(sce->use_nodes==0) return 1;
1855         if((sce->r.scemode & R_DOCOMP)==0) return 1;
1856                 
1857         for(node= ntree->nodes.first; node; node= node->next) {
1858                 if(node->type==CMP_NODE_R_LAYERS)
1859                         if(node->id==NULL || node->id==&sce->id)
1860                                 return 1;
1861         }
1862         return 0;
1863 }
1864
1865 /* bad call... need to think over proper method still */
1866 static void render_composit_stats(char *str)
1867 {
1868         R.i.infostr= str;
1869         R.stats_draw(&R.i);
1870         R.i.infostr= NULL;
1871 }
1872
1873 /* returns fully composited render-result on given time step (in RenderData) */
1874 static void do_render_composite_fields_blur_3d(Render *re)
1875 {
1876         bNodeTree *ntree= re->scene->nodetree;
1877         
1878         /* INIT seeding, compositor can use random texture */
1879         BLI_srandom(re->r.cfra);
1880         
1881         if(composite_needs_render(re->scene)) {
1882                 /* save memory... free all cached images */
1883                 ntreeFreeCache(ntree);
1884                 
1885                 do_render_fields_blur_3d(re);
1886         }
1887         
1888         /* swap render result */
1889         if(re->r.scemode & R_SINGLE_LAYER)
1890                 pop_render_result(re);
1891         
1892         if(!re->test_break() && ntree) {
1893                 ntreeCompositTagRender(re->scene);
1894                 ntreeCompositTagAnimated(ntree);
1895                 
1896                 if(re->r.scemode & R_DOCOMP) {
1897                         /* checks if there are render-result nodes that need scene */
1898                         if((re->r.scemode & R_SINGLE_LAYER)==0)
1899                                 ntree_render_scenes(re);
1900                         
1901                         if(!re->test_break()) {
1902                                 ntree->stats_draw= render_composit_stats;
1903                                 ntree->test_break= re->test_break;
1904                                 /* in case it was never initialized */
1905                                 R.stats_draw= re->stats_draw;
1906                                 
1907                                 ntreeCompositExecTree(ntree, &re->r, G.background==0);
1908                                 ntree->stats_draw= NULL;
1909                                 ntree->test_break= NULL;
1910                         }
1911                 }
1912         }
1913
1914         re->display_draw(re->result, NULL);
1915 }
1916
1917 #ifndef DISABLE_YAFRAY
1918 /* yafray: main yafray render/export call */
1919 static void yafrayRender(Render *re)
1920 {
1921         RE_FreeRenderResult(re->result);
1922         re->result= new_render_result(re, &re->disprect, 0, RR_USEMEM);
1923         
1924         // need this too, for aspect/ortho/etc info
1925         RE_SetCamera(re, re->scene->camera);
1926
1927         // switch must be done before prepareScene()
1928         if (!re->r.YFexportxml)
1929                 YAF_switchFile();
1930         else
1931                 YAF_switchPlugin();
1932         
1933         printf("Starting scene conversion.\n");
1934         RE_Database_FromScene(re, re->scene, 1);
1935         printf("Scene conversion done.\n");
1936         
1937         re->i.starttime = PIL_check_seconds_timer();
1938         
1939         YAF_exportScene(re);
1940
1941         /* also needed for yafray border render, straight copy from do_render_fields_blur_3d() */
1942         /* when border render, check if we have to insert it in black */
1943         if(re->result) {
1944                 if(re->r.mode & R_BORDER) {
1945                         if((re->r.mode & R_CROP)==0) {
1946                                 RenderResult *rres;
1947                                 
1948                                 /* sub-rect for merge call later on */
1949                                 re->result->tilerect= re->disprect;
1950                                 
1951                                 /* this copying sequence could become function? */
1952                                 re->disprect.xmin= re->disprect.ymin= 0;
1953                                 re->disprect.xmax= re->winx;
1954                                 re->disprect.ymax= re->winy;
1955                                 re->rectx= re->winx;
1956                                 re->recty= re->winy;
1957                                 
1958                                 rres= new_render_result(re, &re->disprect, 0, RR_USEMEM);
1959                                 
1960                                 merge_render_result(rres, re->result);
1961                                 RE_FreeRenderResult(re->result);
1962                                 re->result= rres;
1963                                 
1964                                 re->display_init(re->result);
1965                                 re->display_draw(re->result, NULL);
1966                         }
1967                 }
1968         }
1969
1970         re->i.lastframetime = PIL_check_seconds_timer()- re->i.starttime;
1971         re->stats_draw(&re->i);
1972         
1973         RE_Database_Free(re);
1974 }
1975
1976 #endif /* disable yafray */
1977
1978
1979 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
1980
1981 /* main loop: doing sequence + fields + blur + 3d render + compositing */
1982 static void do_render_all_options(Render *re)
1983 {
1984         re->i.starttime= PIL_check_seconds_timer();
1985
1986         /* ensure no images are in memory from previous animated sequences */
1987         BKE_image_all_free_anim_ibufs(re->r.cfra);
1988         
1989         if(re->r.scemode & R_DOSEQ) {
1990                 /* note: do_render_seq() frees rect32 when sequencer returns float images */
1991                 if(!re->test_break()) 
1992                         do_render_seq(re->result, re->r.cfra);
1993                 
1994                 re->stats_draw(&re->i);
1995                 re->display_draw(re->result, NULL);
1996                 
1997         }
1998         else {
1999 #ifndef DISABLE_YAFRAY
2000                 if(re->r.renderer==R_YAFRAY)
2001                         yafrayRender(re);
2002                 else
2003                         do_render_composite_fields_blur_3d(re);
2004 #else
2005                 do_render_composite_fields_blur_3d(re);
2006 #endif
2007         }
2008         
2009         /* for UI only */
2010         renderresult_add_names(re->result);
2011         
2012         re->i.lastframetime= PIL_check_seconds_timer()- re->i.starttime;
2013         re->stats_draw(&re->i);
2014 }
2015
2016 static int is_rendering_allowed(Render *re)
2017 {
2018         SceneRenderLayer *srl;
2019         
2020         /* forbidden combinations */
2021         if(re->r.mode & R_PANORAMA) {
2022                 if(re->r.mode & R_BORDER) {
2023                         re->error("No border supported for Panorama");
2024                         return 0;
2025                 }
2026                 if(re->r.mode & R_ORTHO) {
2027                         re->error("No Ortho render possible for Panorama");
2028                         return 0;
2029                 }
2030         }
2031         
2032         if(re->r.mode & R_BORDER) {
2033                 if(re->r.border.xmax <= re->r.border.xmin || 
2034                    re->r.border.ymax <= re->r.border.ymin) {
2035                         re->error("No border area selected.");
2036                         return 0;
2037                 }
2038                 if(re->r.scemode & R_EXR_TILE_FILE) {
2039                         re->error("Border render and Buffer-save not supported yet");
2040                         return 0;
2041                 }
2042         }
2043         
2044         if(re->r.scemode & R_EXR_TILE_FILE) {
2045                 char str[FILE_MAX];
2046                 
2047                 render_unique_exr_name(re, str);
2048                 
2049                 if (BLI_is_writable(str)==0) {
2050                         re->error("Can not save render buffers, check the temp default path");
2051                         return 0;
2052                 }
2053                 
2054         }
2055         
2056         if(re->r.scemode & R_DOCOMP) {
2057                 if(re->scene->use_nodes) {
2058                         bNodeTree *ntree= re->scene->nodetree;
2059                         bNode *node;
2060                 
2061                         if(ntree==NULL) {
2062                                 re->error("No Nodetree in Scene");
2063                                 return 0;
2064                         }
2065                         
2066                         for(node= ntree->nodes.first; node; node= node->next)
2067                                 if(node->type==CMP_NODE_COMPOSITE)
2068                                         break;
2069                         
2070                         
2071                         if(node==NULL) {
2072                                 re->error("No Render Output Node in Scene");
2073                                 return 0;
2074                         }
2075                 }
2076         }
2077         
2078         /* check valid camera, without camera render is OK (compo, seq) */
2079         if(re->scene->camera==NULL)
2080                 re->scene->camera= scene_find_camera(re->scene);
2081         
2082         if(!(re->r.scemode & (R_DOSEQ|R_DOCOMP))) {
2083                 if(re->scene->camera==NULL) {
2084                         re->error("No camera");
2085                         return 0;
2086                 }
2087         }
2088         
2089         /* layer flag tests */
2090         if(re->r.scemode & R_SINGLE_LAYER) {
2091                 srl= BLI_findlink(&re->scene->r.layers, re->r.actlay);
2092                 /* force layer to be enabled */
2093                 srl->layflag &= ~SCE_LAY_DISABLE;
2094         }
2095         
2096         for(srl= re->scene->r.layers.first; srl; srl= srl->next)
2097                 if(!(srl->layflag & SCE_LAY_DISABLE))
2098                         break;
2099         if(srl==NULL) {
2100                 re->error("All RenderLayers are disabled");
2101                 return 0;
2102         }
2103         
2104         /* renderer */
2105         if(!ELEM(re->r.renderer, R_INTERN, R_YAFRAY)) {
2106                 re->error("Unknown render engine set");
2107                 return 0;
2108         }
2109         return 1;
2110 }
2111
2112 /* evaluating scene options for general Blender render */
2113 static int render_initialize_from_scene(Render *re, Scene *scene)
2114 {
2115         int winx, winy;
2116         rcti disprect;
2117         
2118         /* r.xsch and r.ysch has the actual view window size
2119                 r.border is the clipping rect */
2120         
2121         /* calculate actual render result and display size */
2122         winx= (scene->r.size*scene->r.xsch)/100;
2123         winy= (scene->r.size*scene->r.ysch)/100;
2124         
2125         /* we always render smaller part, inserting it in larger image is compositor bizz, it uses disprect for it */
2126         if(scene->r.mode & R_BORDER) {
2127                 disprect.xmin= scene->r.border.xmin*winx;
2128                 disprect.xmax= scene->r.border.xmax*winx;
2129                 
2130                 disprect.ymin= scene->r.border.ymin*winy;
2131                 disprect.ymax= scene->r.border.ymax*winy;
2132         }
2133         else {
2134                 disprect.xmin= disprect.ymin= 0;
2135                 disprect.xmax= winx;
2136                 disprect.ymax= winy;
2137         }
2138         
2139         if(scene->r.scemode & R_EXR_TILE_FILE) {
2140                 int partx= winx/scene->r.xparts, party= winy/scene->r.yparts;
2141                 
2142                 /* stupid exr tiles dont like different sizes */
2143                 if(winx != partx*scene->r.xparts || winy != party*scene->r.yparts) {
2144                         re->error("Sorry... exr tile saving only allowed with equally sized parts");
2145                         return 0;
2146                 }
2147                 if((scene->r.mode & R_FIELDS) && (party & 1)) {
2148                         re->error("Sorry... exr tile saving only allowed with equally sized parts");
2149                         return 0;
2150                 }
2151         }
2152         
2153         if(scene->r.scemode & R_SINGLE_LAYER)
2154                 push_render_result(re);
2155         
2156         RE_InitState(re, &scene->r, winx, winy, &disprect);
2157         
2158         /* initstate makes new result, have to send changed tags around */
2159         ntreeCompositTagRender(re->scene);
2160         
2161         re->scene= scene;
2162         if(!is_rendering_allowed(re))
2163                 return 0;
2164         
2165         re->display_init(re->result);
2166         re->display_clear(re->result);
2167         
2168         return 1;
2169 }
2170
2171 /* general Blender frame render call */
2172 void RE_BlenderFrame(Render *re, Scene *scene, int frame)
2173 {
2174         /* ugly global still... is to prevent renderwin events and signal subsurfs etc to make full resol */
2175         /* is also set by caller renderwin.c */
2176         G.rendering= 1;
2177         
2178         scene->r.cfra= frame;
2179         
2180         if(render_initialize_from_scene(re, scene)) {
2181                 do_render_all_options(re);
2182         }
2183         
2184         /* UGLY WARNING */
2185         G.rendering= 0;
2186 }
2187
2188 static void do_write_image_or_movie(Render *re, Scene *scene, bMovieHandle *mh)
2189 {
2190         char name[FILE_MAX];
2191         RenderResult rres;
2192         
2193         RE_GetResultImage(re, &rres);
2194
2195         /* write movie or image */
2196         if(BKE_imtype_is_movie(scene->r.imtype)) {
2197                 int dofree = 0;
2198                 /* note; the way it gets 32 bits rects is weak... */
2199                 if(rres.rect32==NULL) {
2200                         rres.rect32= MEM_mapallocN(sizeof(int)*rres.rectx*rres.recty, "temp 32 bits rect");
2201                         dofree = 1;
2202                 }
2203                 RE_ResultGet32(re, (unsigned int *)rres.rect32);
2204                 mh->append_movie(scene->r.cfra, rres.rect32, rres.rectx, rres.recty);
2205                 if(dofree) {
2206                         MEM_freeN(rres.rect32);
2207                 }
2208                 printf("Append frame %d", scene->r.cfra);
2209         } 
2210         else {
2211                 BKE_makepicstring(name, scene->r.pic, scene->r.cfra, scene->r.imtype);
2212                 
2213                 if(re->r.imtype==R_MULTILAYER) {
2214                         if(re->result) {
2215                                 RE_WriteRenderResult(re->result, name, scene->r.quality);
2216                                 printf("Saved: %s", name);
2217                         }
2218                 }
2219                 else {
2220                         ImBuf *ibuf= IMB_allocImBuf(rres.rectx, rres.recty, scene->r.planes, 0, 0);
2221                         int ok;
2222                         
2223                                         /* if not exists, BKE_write_ibuf makes one */
2224                         ibuf->rect= (unsigned int *)rres.rect32;    
2225                         ibuf->rect_float= rres.rectf;
2226                         ibuf->zbuf_float= rres.rectz;
2227                         
2228                         /* float factor for random dither, imbuf takes care of it */
2229                         ibuf->dither= scene->r.dither_intensity;
2230
2231                         ok= BKE_write_ibuf(ibuf, name, scene->r.imtype, scene->r.subimtype, scene->r.quality);
2232                         
2233                         if(ok==0) {
2234                                 printf("Render error: cannot save %s\n", name);
2235                                 G.afbreek=1;
2236                         }
2237                         else printf("Saved: %s", name);
2238                         
2239                         /* optional preview images for exr */
2240                         if(ok && scene->r.imtype==R_OPENEXR && (scene->r.subimtype & R_PREVIEW_JPG)) {
2241                                 if(BLI_testextensie(name, ".exr")) 
2242                                         name[strlen(name)-4]= 0;
2243                                 BKE_add_image_extension(name, R_JPEG90);
2244                                 ibuf->depth= 24; 
2245                                 BKE_write_ibuf(ibuf, name, R_JPEG90, scene->r.subimtype, scene->r.quality);
2246                                 printf("\nSaved: %s", name);
2247                         }
2248                         
2249                                         /* imbuf knows which rects are not part of ibuf */
2250                         IMB_freeImBuf(ibuf);
2251                 }
2252         }
2253         
2254         BLI_timestr(re->i.lastframetime, name);
2255         printf(" Time: %s\n", name);
2256         fflush(stdout); /* needed for renderd !! (not anymore... (ton)) */
2257 }
2258
2259 /* saves images to disk */
2260 void RE_BlenderAnim(Render *re, Scene *scene, int sfra, int efra)
2261 {
2262         bMovieHandle *mh= BKE_get_movie_handle(scene->r.imtype);
2263         int cfrao= scene->r.cfra;
2264         
2265         /* do not call for each frame, it initializes & pops output window */
2266         if(!render_initialize_from_scene(re, scene))
2267                 return;
2268         
2269         /* ugly global still... is to prevent renderwin events and signal subsurfs etc to make full resol */
2270         /* is also set by caller renderwin.c */
2271         G.rendering= 1;
2272         
2273         if(BKE_imtype_is_movie(scene->r.imtype))
2274                 mh->start_movie(&re->r, re->rectx, re->recty);
2275         
2276         if (mh->get_next_frame) {
2277                 while (!(G.afbreek == 1)) {
2278                         int nf = mh->get_next_frame();
2279                         if (nf >= 0 && nf >= scene->r.sfra && nf <= scene->r.efra) {
2280                                 scene->r.cfra = re->r.cfra = nf;
2281                                 
2282                                 do_render_all_options(re);
2283
2284                                 if(re->test_break() == 0) {
2285                                         do_write_image_or_movie(re, scene, mh);
2286                                 }
2287                         } else {
2288                                 re->test_break();
2289                         }
2290                 }
2291         } else {
2292                 for(scene->r.cfra= sfra; 
2293                     scene->r.cfra<=efra; scene->r.cfra++) {
2294                         re->r.cfra= scene->r.cfra;         /* weak.... */
2295                 
2296                         do_render_all_options(re);
2297
2298                         if(re->test_break() == 0) {
2299                                 do_write_image_or_movie(re, scene, mh);
2300                         }
2301                 
2302                         if(G.afbreek==1) break;
2303                 }
2304         }
2305         
2306         /* end movie */
2307         if(BKE_imtype_is_movie(scene->r.imtype))
2308                 mh->end_movie();
2309
2310         scene->r.cfra= cfrao;
2311         
2312         /* UGLY WARNING */
2313         G.rendering= 0;
2314 }
2315
2316 /* note; repeated win/disprect calc... solve that nicer, also in compo */
2317
2318 /* only temp file! */
2319 void RE_ReadRenderResult(Scene *scene, Scene *scenode)
2320 {
2321         Render *re;
2322         int winx, winy;
2323         rcti disprect;
2324         
2325         /* calculate actual render result and display size */
2326         winx= (scene->r.size*scene->r.xsch)/100;
2327         winy= (scene->r.size*scene->r.ysch)/100;
2328         
2329         /* only in movie case we render smaller part */
2330         if(scene->r.mode & R_BORDER) {
2331                 disprect.xmin= scene->r.border.xmin*winx;
2332                 disprect.xmax= scene->r.border.xmax*winx;
2333                 
2334                 disprect.ymin= scene->r.border.ymin*winy;
2335                 disprect.ymax= scene->r.border.ymax*winy;
2336         }
2337         else {
2338                 disprect.xmin= disprect.ymin= 0;
2339                 disprect.xmax= winx;
2340                 disprect.ymax= winy;
2341         }
2342         
2343         if(scenode)
2344                 scene= scenode;
2345         
2346         re= RE_NewRender(scene->id.name);
2347         RE_InitState(re, &scene->r, winx, winy, &disprect);
2348         re->scene= scene;
2349         
2350         read_render_result(re);
2351 }
2352
2353 void RE_set_max_threads(int threads)
2354 {
2355         if(threads>0 && threads<=BLENDER_MAX_THREADS)
2356                 commandline_threads= threads;
2357         else
2358                 printf("Error, threads has to be in range 1-%d\n", BLENDER_MAX_THREADS);
2359 }