Subsurface scattering:
[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                 pa->result= new_render_result(&R, &pa->disprect, pa->crop, RR_USEMEM);
1167
1168                 if(R.sss_points)
1169                         zbufshade_sss_tile(pa);
1170                 else if(R.osa)
1171                         zbufshadeDA_tile(pa);
1172                 else
1173                         zbufshade_tile(pa);
1174                 
1175                 /* merge too on break! */
1176                 if(!R.sss_points && R.result->exrhandle)
1177                         save_render_result_tile(&R, pa);
1178                 else
1179                         merge_render_result(R.result, pa->result);
1180         }
1181         
1182         pa->ready= 1;
1183         
1184         return NULL;
1185 }
1186
1187 /* returns with render result filled, not threaded, used for preview now only */
1188 static void render_tile_processor(Render *re, int firsttile)
1189 {
1190         RenderPart *pa;
1191         
1192         if(re->test_break())
1193                 return;
1194
1195         /* hrmf... exception, this is used for preview render, re-entrant, so render result has to be re-used */
1196         if(re->result==NULL || re->result->layers.first==NULL) {
1197                 if(re->result) RE_FreeRenderResult(re->result);
1198                 re->result= new_render_result(re, &re->disprect, 0, RR_USEMEM);
1199         }
1200         
1201         re->stats_draw(&re->i);
1202  
1203         if(re->result==NULL)
1204                 return;
1205         
1206         initparts(re);
1207         
1208         /* assuming no new data gets added to dbase... */
1209         R= *re;
1210         
1211         for(pa= re->parts.first; pa; pa= pa->next) {
1212                 if(firsttile) {
1213                         re->i.partsdone++;      /* was reset in initparts */
1214                         firsttile--;
1215                 }
1216                 else {
1217                         do_part_thread(pa);
1218                         
1219                         if(pa->result) {
1220                                 if(!re->test_break()) {
1221                                         re->display_draw(pa->result, NULL);
1222                                         
1223                                         re->i.partsdone++;
1224                                         re->stats_draw(&re->i);
1225                                 }
1226                                 RE_FreeRenderResult(pa->result);
1227                                 pa->result= NULL;
1228                         }               
1229                         if(re->test_break())
1230                                 break;
1231                 }
1232         }
1233         
1234         freeparts(re);
1235 }
1236
1237 /* calculus for how much 1 pixel rendered should rotate the 3d geometry */
1238 /* is not that simple, needs to be corrected for errors of larger viewplane sizes */
1239 /* called in initrender.c, initparts() and convertblender.c, for speedvectors */
1240 float panorama_pixel_rot(Render *re)
1241 {
1242         float psize, phi, xfac;
1243         
1244         /* size of 1 pixel mapped to viewplane coords */
1245         psize= (re->viewplane.xmax-re->viewplane.xmin)/(float)re->winx;
1246         /* angle of a pixel */
1247         phi= atan(psize/re->clipsta);
1248         
1249         /* correction factor for viewplane shifting, first calculate how much the viewplane angle is */
1250         xfac= ((re->viewplane.xmax-re->viewplane.xmin))/(float)re->xparts;
1251         xfac= atan(0.5f*xfac/re->clipsta); 
1252         /* and how much the same viewplane angle is wrapped */
1253         psize= 0.5f*phi*((float)re->partx);
1254         
1255         /* the ratio applied to final per-pixel angle */
1256         phi*= xfac/psize;
1257         
1258         return phi;
1259 }
1260
1261 /* call when all parts stopped rendering, to find the next Y slice */
1262 /* if slice found, it rotates the dbase */
1263 static RenderPart *find_next_pano_slice(Render *re, int *minx, rctf *viewplane)
1264 {
1265         RenderPart *pa, *best= NULL;
1266         
1267         *minx= re->winx;
1268         
1269         /* most left part of the non-rendering parts */
1270         for(pa= re->parts.first; pa; pa= pa->next) {
1271                 if(pa->ready==0 && pa->nr==0) {
1272                         if(pa->disprect.xmin < *minx) {
1273                                 best= pa;
1274                                 *minx= pa->disprect.xmin;
1275                         }
1276                 }
1277         }
1278                         
1279         if(best) {
1280                 float phi= panorama_pixel_rot(re);
1281
1282                 R.panodxp= (re->winx - (best->disprect.xmin + best->disprect.xmax) )/2;
1283                 R.panodxv= ((viewplane->xmax-viewplane->xmin)*R.panodxp)/(float)R.winx;
1284                 
1285                 /* shift viewplane */
1286                 R.viewplane.xmin = viewplane->xmin + R.panodxv;
1287                 R.viewplane.xmax = viewplane->xmax + R.panodxv;
1288                 RE_SetWindow(re, &R.viewplane, R.clipsta, R.clipend);
1289                 Mat4CpyMat4(R.winmat, re->winmat);
1290                 
1291                 /* rotate database according to part coordinates */
1292                 project_renderdata(re, projectverto, 1, -R.panodxp*phi);
1293                 R.panosi= sin(R.panodxp*phi);
1294                 R.panoco= cos(R.panodxp*phi);
1295         }
1296         return best;
1297 }
1298
1299 static RenderPart *find_next_part(Render *re, int minx)
1300 {
1301         RenderPart *pa, *best= NULL;
1302         int centx=re->winx/2, centy=re->winy/2, tot=1;
1303         int mindist, distx, disty;
1304         
1305         /* find center of rendered parts, image center counts for 1 too */
1306         for(pa= re->parts.first; pa; pa= pa->next) {
1307                 if(pa->ready) {
1308                         centx+= (pa->disprect.xmin+pa->disprect.xmax)/2;
1309                         centy+= (pa->disprect.ymin+pa->disprect.ymax)/2;
1310                         tot++;
1311                 }
1312         }
1313         centx/=tot;
1314         centy/=tot;
1315         
1316         /* closest of the non-rendering parts */
1317         mindist= re->winx*re->winy;
1318         for(pa= re->parts.first; pa; pa= pa->next) {
1319                 if(pa->ready==0 && pa->nr==0) {
1320                         distx= centx - (pa->disprect.xmin+pa->disprect.xmax)/2;
1321                         disty= centy - (pa->disprect.ymin+pa->disprect.ymax)/2;
1322                         distx= (int)sqrt(distx*distx + disty*disty);
1323                         if(distx<mindist) {
1324                                 if(re->r.mode & R_PANORAMA) {
1325                                         if(pa->disprect.xmin==minx) {
1326                                                 best= pa;
1327                                                 mindist= distx;
1328                                         }
1329                                 }
1330                                 else {
1331                                         best= pa;
1332                                         mindist= distx;
1333                                 }
1334                         }
1335                 }
1336         }
1337         return best;
1338 }
1339
1340 static void print_part_stats(Render *re, RenderPart *pa)
1341 {
1342         char str[64];
1343         
1344         sprintf(str, "Part %d-%d", pa->nr, re->i.totpart);
1345         re->i.infostr= str;
1346         re->stats_draw(&re->i);
1347         re->i.infostr= NULL;
1348 }
1349
1350 static void threaded_tile_processor(Render *re)
1351 {
1352         ListBase threads;
1353         RenderPart *pa, *nextpa;
1354         RenderResult *rr;
1355         rctf viewplane= re->viewplane;
1356         int rendering=1, counter= 1, drawtimer=0, hasdrawn, minx=0;
1357         
1358         /* first step; the entire render result, or prepare exr buffer saving */
1359         RE_FreeRenderResult(re->result);
1360         rr= re->result= new_render_result(re, &re->disprect, 0, re->r.scemode & R_EXR_TILE_FILE);
1361         
1362         if(rr==NULL)
1363                 return;
1364         /* warning; no return here without closing exr file */
1365 //      if(re->re->test_break())
1366 //              return;
1367         
1368         initparts(re);
1369         
1370         if(rr->exrhandle) {
1371                 char str[FILE_MAX];
1372                 
1373                 render_unique_exr_name(re, str);
1374                 
1375                 printf("write exr tmp file, %dx%d, %s\n", rr->rectx, rr->recty, str);
1376                 IMB_exrtile_begin_write(rr->exrhandle, str, rr->rectx, rr->recty, rr->rectx/re->xparts, rr->recty/re->yparts);
1377         }
1378         
1379         BLI_init_threads(&threads, do_part_thread, re->r.threads);
1380         
1381         /* assuming no new data gets added to dbase... */
1382         R= *re;
1383         
1384         /* set threadsafe break */
1385         R.test_break= thread_break;
1386         
1387         /* timer loop demands to sleep when no parts are left, so we enter loop with a part */
1388         if(re->r.mode & R_PANORAMA)
1389                 nextpa= find_next_pano_slice(re, &minx, &viewplane);
1390         else
1391                 nextpa= find_next_part(re, 0);
1392         
1393         while(rendering) {
1394                 
1395                 if(re->test_break())
1396                         PIL_sleep_ms(50);
1397                 else if(nextpa && BLI_available_threads(&threads)) {
1398                         drawtimer= 0;
1399                         nextpa->nr= counter++;  /* for nicest part, and for stats */
1400                         nextpa->thread= BLI_available_thread_index(&threads);   /* sample index */
1401                         BLI_insert_thread(&threads, nextpa);
1402
1403                         nextpa= find_next_part(re, minx);
1404                 }
1405                 else if(re->r.mode & R_PANORAMA) {
1406                         if(nextpa==NULL && BLI_available_threads(&threads)==re->r.threads)
1407                                 nextpa= find_next_pano_slice(re, &minx, &viewplane);
1408                         else {
1409                                 PIL_sleep_ms(50);
1410                                 drawtimer++;
1411                         }
1412                 }
1413                 else {
1414                         PIL_sleep_ms(50);
1415                         drawtimer++;
1416                 }
1417                 
1418                 /* check for ready ones to display, and if we need to continue */
1419                 rendering= 0;
1420                 hasdrawn= 0;
1421                 for(pa= re->parts.first; pa; pa= pa->next) {
1422                         if(pa->ready) {
1423                                 
1424                                 BLI_remove_thread(&threads, pa);
1425                                 
1426                                 if(pa->result) {
1427                                         re->display_draw(pa->result, NULL);
1428                                         print_part_stats(re, pa);
1429                                         
1430                                         RE_FreeRenderResult(pa->result);
1431                                         pa->result= NULL;
1432                                         re->i.partsdone++;
1433                                         hasdrawn= 1;
1434                                 }
1435                         }
1436                         else {
1437                                 rendering= 1;
1438                                 if(pa->nr && pa->result && drawtimer>20) {
1439                                         re->display_draw(pa->result, &pa->result->renrect);
1440                                         hasdrawn= 1;
1441                                 }
1442                         }
1443                 }
1444                 if(hasdrawn)
1445                         drawtimer= 0;
1446
1447                 /* on break, wait for all slots to get freed */
1448                 if( (g_break=re->test_break()) && BLI_available_threads(&threads)==re->r.threads)
1449                         rendering= 0;
1450                 
1451         }
1452         
1453         if(rr->exrhandle) {
1454                 save_empty_result_tiles(re);
1455                 IMB_exr_close(rr->exrhandle);
1456                 rr->exrhandle= NULL;
1457                 read_render_result(re);
1458         }
1459         
1460         /* unset threadsafety */
1461         g_break= 0;
1462         
1463         BLI_end_threads(&threads);
1464         freeparts(re);
1465 }
1466
1467 /* currently only called by preview renders and envmap */
1468 void RE_TileProcessor(Render *re, int firsttile, int threaded)
1469 {
1470         /* the partsdone variable has to be reset to firsttile, to survive esc before it was set to zero */
1471         
1472         re->i.partsdone= firsttile;
1473
1474         re->i.starttime= PIL_check_seconds_timer();
1475
1476         if(threaded)
1477                 threaded_tile_processor(re);
1478         else
1479                 render_tile_processor(re, firsttile);
1480                 
1481         re->i.lastframetime= PIL_check_seconds_timer()- re->i.starttime;
1482         re->stats_draw(&re->i);
1483 }
1484
1485
1486 /* ************  This part uses API, for rendering Blender scenes ********** */
1487
1488 static void do_render_3d(Render *re)
1489 {
1490         
1491 //      re->cfra= cfra; /* <- unused! */
1492         
1493         /* make render verts/faces/halos/lamps */
1494         if(render_scene_needs_vector(re))
1495                 RE_Database_FromScene_Vectors(re, re->scene);
1496         else
1497            RE_Database_FromScene(re, re->scene, 1);
1498         
1499         threaded_tile_processor(re);
1500         
1501         /* do left-over 3d post effects (flares) */
1502         if(re->flag & R_HALO)
1503                 if(!re->test_break())
1504                         add_halo_flare(re);
1505
1506         
1507         /* free all render verts etc */
1508         RE_Database_Free(re);
1509 }
1510
1511 /* called by blur loop, accumulate renderlayers */
1512 static void addblur_rect(RenderResult *rr, float *rectf, float *rectf1, float blurfac, int channels)
1513 {
1514         float mfac= 1.0f - blurfac;
1515         int a, b, stride= channels*rr->rectx;
1516         int len= stride*sizeof(float);
1517         
1518         for(a=0; a<rr->recty; a++) {
1519                 if(blurfac==1.0f) {
1520                         memcpy(rectf, rectf1, len);
1521                 }
1522                 else {
1523                         float *rf= rectf, *rf1= rectf1;
1524                         
1525                         for( b= rr->rectx*channels; b>0; b--, rf++, rf1++) {
1526                                 rf[0]= mfac*rf[0] + blurfac*rf1[0];
1527                         }
1528                 }
1529                 rectf+= stride;
1530                 rectf1+= stride;
1531         }
1532 }
1533
1534 /* called by blur loop, accumulate renderlayers */
1535 static void merge_renderresult_blur(RenderResult *rr, RenderResult *brr, float blurfac)
1536 {
1537         RenderLayer *rl, *rl1;
1538         RenderPass *rpass, *rpass1;
1539         
1540         rl1= brr->layers.first;
1541         for(rl= rr->layers.first; rl && rl1; rl= rl->next, rl1= rl1->next) {
1542                 
1543                 /* combined */
1544                 if(rl->rectf && rl1->rectf)
1545                         addblur_rect(rr, rl->rectf, rl1->rectf, blurfac, 4);
1546                 
1547                 /* passes are allocated in sync */
1548                 rpass1= rl1->passes.first;
1549                 for(rpass= rl->passes.first; rpass && rpass1; rpass= rpass->next, rpass1= rpass1->next) {
1550                         addblur_rect(rr, rpass->rect, rpass1->rect, blurfac, rpass->channels);
1551                 }
1552         }
1553 }
1554
1555 /* main blur loop, can be called by fields too */
1556 static void do_render_blur_3d(Render *re)
1557 {
1558         RenderResult *rres;
1559         float blurfac;
1560         int blur= re->r.osa;
1561         
1562         /* create accumulation render result */
1563         rres= new_render_result(re, &re->disprect, 0, RR_USEMEM);
1564         
1565         /* do the blur steps */
1566         while(blur--) {
1567                 set_mblur_offs( re->r.blurfac*((float)(re->r.osa-blur))/(float)re->r.osa );
1568                 
1569                 re->i.curblur= re->r.osa-blur;  /* stats */
1570                 
1571                 do_render_3d(re);
1572                 
1573                 blurfac= 1.0f/(float)(re->r.osa-blur);
1574                 
1575                 merge_renderresult_blur(rres, re->result, blurfac);
1576                 if(re->test_break()) break;
1577         }
1578         
1579         /* swap results */
1580         RE_FreeRenderResult(re->result);
1581         re->result= rres;
1582         
1583         set_mblur_offs(0.0f);
1584         re->i.curblur= 0;       /* stats */
1585         
1586         /* weak... the display callback wants an active renderlayer pointer... */
1587         re->result->renlay= render_get_active_layer(re, re->result);
1588         re->display_draw(re->result, NULL);     
1589 }
1590
1591
1592 /* function assumes rectf1 and rectf2 to be half size of rectf */
1593 static void interleave_rect(RenderResult *rr, float *rectf, float *rectf1, float *rectf2, int channels)
1594 {
1595         int a, stride= channels*rr->rectx;
1596         int len= stride*sizeof(float);
1597         
1598         for(a=0; a<rr->recty; a+=2) {
1599                 memcpy(rectf, rectf1, len);
1600                 rectf+= stride;
1601                 rectf1+= stride;
1602                 memcpy(rectf, rectf2, len);
1603                 rectf+= stride;
1604                 rectf2+= stride;
1605         }
1606 }
1607
1608 /* merge render results of 2 fields */
1609 static void merge_renderresult_fields(RenderResult *rr, RenderResult *rr1, RenderResult *rr2)
1610 {
1611         RenderLayer *rl, *rl1, *rl2;
1612         RenderPass *rpass, *rpass1, *rpass2;
1613         
1614         rl1= rr1->layers.first;
1615         rl2= rr2->layers.first;
1616         for(rl= rr->layers.first; rl && rl1 && rl2; rl= rl->next, rl1= rl1->next, rl2= rl2->next) {
1617                 
1618                 /* combined */
1619                 if(rl->rectf && rl1->rectf && rl2->rectf)
1620                         interleave_rect(rr, rl->rectf, rl1->rectf, rl2->rectf, 4);
1621                 
1622                 /* passes are allocated in sync */
1623                 rpass1= rl1->passes.first;
1624                 rpass2= rl2->passes.first;
1625                 for(rpass= rl->passes.first; rpass && rpass1 && rpass2; rpass= rpass->next, rpass1= rpass1->next, rpass2= rpass2->next) {
1626                         interleave_rect(rr, rpass->rect, rpass1->rect, rpass2->rect, rpass->channels);
1627                 }
1628         }
1629 }
1630
1631
1632 /* interleaves 2 frames */
1633 static void do_render_fields_3d(Render *re)
1634 {
1635         RenderResult *rr1, *rr2= NULL;
1636         
1637         /* no render result was created, we can safely halve render y */
1638         re->winy /= 2;
1639         re->recty /= 2;
1640         re->disprect.ymin /= 2;
1641         re->disprect.ymax /= 2;
1642         
1643         re->i.curfield= 1;      /* stats */
1644         
1645         /* first field, we have to call camera routine for correct aspect and subpixel offset */
1646         RE_SetCamera(re, re->scene->camera);
1647         if(re->r.mode & R_MBLUR)
1648                 do_render_blur_3d(re);
1649         else
1650                 do_render_3d(re);
1651         rr1= re->result;
1652         re->result= NULL;
1653         
1654         /* second field */
1655         if(!re->test_break()) {
1656                 
1657                 re->i.curfield= 2;      /* stats */
1658                 
1659                 re->flag |= R_SEC_FIELD;
1660                 if((re->r.mode & R_FIELDSTILL)==0) 
1661                         set_field_offs(0.5f);
1662                 RE_SetCamera(re, re->scene->camera);
1663                 if(re->r.mode & R_MBLUR)
1664                         do_render_blur_3d(re);
1665                 else
1666                         do_render_3d(re);
1667                 re->flag &= ~R_SEC_FIELD;
1668                 set_field_offs(0.0f);
1669                 
1670                 rr2= re->result;
1671         }
1672         
1673         /* allocate original height new buffers */
1674         re->winy *= 2;
1675         re->recty *= 2;
1676         re->disprect.ymin *= 2;
1677         re->disprect.ymax *= 2;
1678         re->result= new_render_result(re, &re->disprect, 0, RR_USEMEM);
1679         
1680         if(rr2) {
1681                 if(re->r.mode & R_ODDFIELD)
1682                         merge_renderresult_fields(re->result, rr2, rr1);
1683                 else
1684                         merge_renderresult_fields(re->result, rr1, rr2);
1685                 
1686                 RE_FreeRenderResult(rr2);
1687         }
1688         RE_FreeRenderResult(rr1);
1689         
1690         re->i.curfield= 0;      /* stats */
1691         
1692         /* weak... the display callback wants an active renderlayer pointer... */
1693         re->result->renlay= render_get_active_layer(re, re->result);
1694         re->display_draw(re->result, NULL);
1695 }
1696
1697 static void load_backbuffer(Render *re)
1698 {
1699         if(re->r.alphamode == R_ADDSKY) {
1700                 ImBuf *ibuf;
1701                 char name[256];
1702                 
1703                 strcpy(name, re->r.backbuf);
1704                 BLI_convertstringcode(name, G.sce, re->r.cfra);
1705                 
1706                 if(re->backbuf) {
1707                         re->backbuf->id.us--;
1708                         if(re->backbuf->id.us<1)
1709                                 BKE_image_signal(re->backbuf, NULL, IMA_SIGNAL_RELOAD);
1710                 }
1711                 
1712                 re->backbuf= BKE_add_image_file(name);
1713                 ibuf= BKE_image_get_ibuf(re->backbuf, NULL);
1714                 if(ibuf==NULL) {
1715                         // error() doesnt work with render window open
1716                         //error("No backbuf there!");
1717                         printf("Error: No backbuf %s\n", name);
1718                 }
1719                 else {
1720                         if (re->r.mode & R_FIELDS)
1721                                 image_de_interlace(re->backbuf, re->r.mode & R_ODDFIELD);
1722                 }
1723         }
1724 }
1725
1726 /* main render routine, no compositing */
1727 static void do_render_fields_blur_3d(Render *re)
1728 {
1729         /* also check for camera here */
1730         if(re->scene->camera==NULL) {
1731                 printf("ERROR: Cannot render, no camera\n");
1732                 G.afbreek= 1;
1733                 return;
1734         }
1735         
1736         /* backbuffer initialize */
1737         if(re->r.bufflag & 1)
1738                 load_backbuffer(re);
1739
1740         /* now use renderdata and camera to set viewplane */
1741         RE_SetCamera(re, re->scene->camera);
1742         
1743         if(re->r.mode & R_FIELDS)
1744                 do_render_fields_3d(re);
1745         else if(re->r.mode & R_MBLUR)
1746                 do_render_blur_3d(re);
1747         else
1748                 do_render_3d(re);
1749         
1750         /* when border render, check if we have to insert it in black */
1751         if(re->result) {
1752                 if(re->r.mode & R_BORDER) {
1753                         if((re->r.mode & R_CROP)==0) {
1754                                 RenderResult *rres;
1755                                 
1756                                 /* sub-rect for merge call later on */
1757                                 re->result->tilerect= re->disprect;
1758                                 
1759                                 /* this copying sequence could become function? */
1760                                 re->disprect.xmin= re->disprect.ymin= 0;
1761                                 re->disprect.xmax= re->winx;
1762                                 re->disprect.ymax= re->winy;
1763                                 re->rectx= re->winx;
1764                                 re->recty= re->winy;
1765                                 
1766                                 rres= new_render_result(re, &re->disprect, 0, RR_USEMEM);
1767                                 
1768                                 merge_render_result(rres, re->result);
1769                                 RE_FreeRenderResult(re->result);
1770                                 re->result= rres;
1771                                 
1772                                 /* weak... the display callback wants an active renderlayer pointer... */
1773                                 re->result->renlay= render_get_active_layer(re, re->result);
1774                                 
1775                                 re->display_init(re->result);
1776                                 re->display_draw(re->result, NULL);
1777                         }
1778                 }
1779         }
1780 }
1781
1782
1783 /* within context of current Render *re, render another scene.
1784    it uses current render image size and disprect, but doesn't execute composite
1785 */
1786 static void render_scene(Render *re, Scene *sce, int cfra)
1787 {
1788         Render *resc= RE_NewRender(sce->id.name);
1789         
1790         sce->r.cfra= cfra;
1791                 
1792         /* initial setup */
1793         RE_InitState(resc, &sce->r, re->winx, re->winy, &re->disprect);
1794         
1795         /* this to enable this scene to create speed vectors */
1796         resc->r.scemode |= R_DOCOMP;
1797         
1798         /* still unsure entity this... */
1799         resc->scene= sce;
1800         
1801         /* ensure scene has depsgraph, base flags etc OK. Warning... also sets G.scene */
1802         set_scene_bg(sce);
1803
1804         /* copy callbacks */
1805         resc->display_draw= re->display_draw;
1806         resc->test_break= re->test_break;
1807         resc->stats_draw= re->stats_draw;
1808         
1809         do_render_fields_blur_3d(resc);
1810 }
1811
1812 static void ntree_render_scenes(Render *re)
1813 {
1814         bNode *node;
1815         int cfra= re->scene->r.cfra;
1816         
1817         if(re->scene->nodetree==NULL) return;
1818         
1819         /* check for render-layers nodes using other scenes, we tag them LIB_DOIT */
1820         for(node= re->scene->nodetree->nodes.first; node; node= node->next) {
1821                 if(node->type==CMP_NODE_R_LAYERS) {
1822                         if(node->id) {
1823                                 if(node->id != (ID *)re->scene)
1824                                         node->id->flag |= LIB_DOIT;
1825                                 else
1826                                         node->id->flag &= ~LIB_DOIT;
1827                         }
1828                 }
1829         }
1830         
1831         /* now foreach render-result node tagged we do a full render */
1832         /* results are stored in a way compisitor will find it */
1833         for(node= re->scene->nodetree->nodes.first; node; node= node->next) {
1834                 if(node->type==CMP_NODE_R_LAYERS) {
1835                         if(node->id && node->id != (ID *)re->scene) {
1836                                 if(node->id->flag & LIB_DOIT) {
1837                                         render_scene(re, (Scene *)node->id, cfra);
1838                                         node->id->flag &= ~LIB_DOIT;
1839                                 }
1840                         }
1841                 }
1842         }
1843         
1844         /* still the global... */
1845         if(G.scene!=re->scene)
1846                 set_scene_bg(re->scene);
1847         
1848 }
1849
1850 /* helper call to detect if theres a composite with render-result node */
1851 static int composite_needs_render(Scene *sce)
1852 {
1853         bNodeTree *ntree= sce->nodetree;
1854         bNode *node;
1855         
1856         if(ntree==NULL) return 1;
1857         if(sce->use_nodes==0) return 1;
1858         if((sce->r.scemode & R_DOCOMP)==0) return 1;
1859                 
1860         for(node= ntree->nodes.first; node; node= node->next) {
1861                 if(node->type==CMP_NODE_R_LAYERS)
1862                         if(node->id==NULL || node->id==&sce->id)
1863                                 return 1;
1864         }
1865         return 0;
1866 }
1867
1868 /* bad call... need to think over proper method still */
1869 static void render_composit_stats(char *str)
1870 {
1871         R.i.infostr= str;
1872         R.stats_draw(&R.i);
1873         R.i.infostr= NULL;
1874 }
1875
1876 /* returns fully composited render-result on given time step (in RenderData) */
1877 static void do_render_composite_fields_blur_3d(Render *re)
1878 {
1879         bNodeTree *ntree= re->scene->nodetree;
1880         
1881         /* INIT seeding, compositor can use random texture */
1882         BLI_srandom(re->r.cfra);
1883         
1884         if(composite_needs_render(re->scene)) {
1885                 /* save memory... free all cached images */
1886                 ntreeFreeCache(ntree);
1887                 
1888                 do_render_fields_blur_3d(re);
1889         }
1890         
1891         /* swap render result */
1892         if(re->r.scemode & R_SINGLE_LAYER)
1893                 pop_render_result(re);
1894         
1895         if(!re->test_break() && ntree) {
1896                 ntreeCompositTagRender(re->scene);
1897                 ntreeCompositTagAnimated(ntree);
1898                 
1899                 if(re->r.scemode & R_DOCOMP) {
1900                         /* checks if there are render-result nodes that need scene */
1901                         if((re->r.scemode & R_SINGLE_LAYER)==0)
1902                                 ntree_render_scenes(re);
1903                         
1904                         if(!re->test_break()) {
1905                                 ntree->stats_draw= render_composit_stats;
1906                                 ntree->test_break= re->test_break;
1907                                 /* in case it was never initialized */
1908                                 R.stats_draw= re->stats_draw;
1909                                 
1910                                 ntreeCompositExecTree(ntree, &re->r, G.background==0);
1911                                 ntree->stats_draw= NULL;
1912                                 ntree->test_break= NULL;
1913                         }
1914                 }
1915         }
1916
1917         re->display_draw(re->result, NULL);
1918 }
1919
1920 #ifndef DISABLE_YAFRAY
1921 /* yafray: main yafray render/export call */
1922 static void yafrayRender(Render *re)
1923 {
1924         RE_FreeRenderResult(re->result);
1925         re->result= new_render_result(re, &re->disprect, 0, RR_USEMEM);
1926         
1927         // need this too, for aspect/ortho/etc info
1928         RE_SetCamera(re, re->scene->camera);
1929
1930         // switch must be done before prepareScene()
1931         if (!re->r.YFexportxml)
1932                 YAF_switchFile();
1933         else
1934                 YAF_switchPlugin();
1935         
1936         printf("Starting scene conversion.\n");
1937         RE_Database_FromScene(re, re->scene, 1);
1938         printf("Scene conversion done.\n");
1939         
1940         re->i.starttime = PIL_check_seconds_timer();
1941         
1942         YAF_exportScene(re);
1943
1944         /* also needed for yafray border render, straight copy from do_render_fields_blur_3d() */
1945         /* when border render, check if we have to insert it in black */
1946         if(re->result) {
1947                 if(re->r.mode & R_BORDER) {
1948                         if((re->r.mode & R_CROP)==0) {
1949                                 RenderResult *rres;
1950                                 
1951                                 /* sub-rect for merge call later on */
1952                                 re->result->tilerect= re->disprect;
1953                                 
1954                                 /* this copying sequence could become function? */
1955                                 re->disprect.xmin= re->disprect.ymin= 0;
1956                                 re->disprect.xmax= re->winx;
1957                                 re->disprect.ymax= re->winy;
1958                                 re->rectx= re->winx;
1959                                 re->recty= re->winy;
1960                                 
1961                                 rres= new_render_result(re, &re->disprect, 0, RR_USEMEM);
1962                                 
1963                                 merge_render_result(rres, re->result);
1964                                 RE_FreeRenderResult(re->result);
1965                                 re->result= rres;
1966                                 
1967                                 re->display_init(re->result);
1968                                 re->display_draw(re->result, NULL);
1969                         }
1970                 }
1971         }
1972
1973         re->i.lastframetime = PIL_check_seconds_timer()- re->i.starttime;
1974         re->stats_draw(&re->i);
1975         
1976         RE_Database_Free(re);
1977 }
1978
1979 #endif /* disable yafray */
1980
1981
1982 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
1983
1984 /* main loop: doing sequence + fields + blur + 3d render + compositing */
1985 static void do_render_all_options(Render *re)
1986 {
1987         re->i.starttime= PIL_check_seconds_timer();
1988
1989         /* ensure no images are in memory from previous animated sequences */
1990         BKE_image_all_free_anim_ibufs(re->r.cfra);
1991         
1992         if(re->r.scemode & R_DOSEQ) {
1993                 /* note: do_render_seq() frees rect32 when sequencer returns float images */
1994                 if(!re->test_break()) 
1995                         do_render_seq(re->result, re->r.cfra);
1996                 
1997                 re->stats_draw(&re->i);
1998                 re->display_draw(re->result, NULL);
1999                 
2000         }
2001         else {
2002 #ifndef DISABLE_YAFRAY
2003                 if(re->r.renderer==R_YAFRAY)
2004                         yafrayRender(re);
2005                 else
2006                         do_render_composite_fields_blur_3d(re);
2007 #else
2008                 do_render_composite_fields_blur_3d(re);
2009 #endif
2010         }
2011         
2012         /* for UI only */
2013         renderresult_add_names(re->result);
2014         
2015         re->i.lastframetime= PIL_check_seconds_timer()- re->i.starttime;
2016         re->stats_draw(&re->i);
2017 }
2018
2019 static int is_rendering_allowed(Render *re)
2020 {
2021         SceneRenderLayer *srl;
2022         
2023         /* forbidden combinations */
2024         if(re->r.mode & R_PANORAMA) {
2025                 if(re->r.mode & R_BORDER) {
2026                         re->error("No border supported for Panorama");
2027                         return 0;
2028                 }
2029                 if(re->r.mode & R_ORTHO) {
2030                         re->error("No Ortho render possible for Panorama");
2031                         return 0;
2032                 }
2033         }
2034         
2035         if(re->r.mode & R_BORDER) {
2036                 if(re->r.border.xmax <= re->r.border.xmin || 
2037                    re->r.border.ymax <= re->r.border.ymin) {
2038                         re->error("No border area selected.");
2039                         return 0;
2040                 }
2041                 if(re->r.scemode & R_EXR_TILE_FILE) {
2042                         re->error("Border render and Buffer-save not supported yet");
2043                         return 0;
2044                 }
2045         }
2046         
2047         if(re->r.scemode & R_EXR_TILE_FILE) {
2048                 char str[FILE_MAX];
2049                 
2050                 render_unique_exr_name(re, str);
2051                 
2052                 if (BLI_is_writable(str)==0) {
2053                         re->error("Can not save render buffers, check the temp default path");
2054                         return 0;
2055                 }
2056                 
2057         }
2058         
2059         if(re->r.scemode & R_DOCOMP) {
2060                 if(re->scene->use_nodes) {
2061                         bNodeTree *ntree= re->scene->nodetree;
2062                         bNode *node;
2063                 
2064                         if(ntree==NULL) {
2065                                 re->error("No Nodetree in Scene");
2066                                 return 0;
2067                         }
2068                         
2069                         for(node= ntree->nodes.first; node; node= node->next)
2070                                 if(node->type==CMP_NODE_COMPOSITE)
2071                                         break;
2072                         
2073                         
2074                         if(node==NULL) {
2075                                 re->error("No Render Output Node in Scene");
2076                                 return 0;
2077                         }
2078                 }
2079         }
2080         
2081         /* check valid camera, without camera render is OK (compo, seq) */
2082         if(re->scene->camera==NULL)
2083                 re->scene->camera= scene_find_camera(re->scene);
2084         
2085         if(!(re->r.scemode & (R_DOSEQ|R_DOCOMP))) {
2086                 if(re->scene->camera==NULL) {
2087                         re->error("No camera");
2088                         return 0;
2089                 }
2090         }
2091         
2092         /* layer flag tests */
2093         if(re->r.scemode & R_SINGLE_LAYER) {
2094                 srl= BLI_findlink(&re->scene->r.layers, re->r.actlay);
2095                 /* force layer to be enabled */
2096                 srl->layflag &= ~SCE_LAY_DISABLE;
2097         }
2098         
2099         for(srl= re->scene->r.layers.first; srl; srl= srl->next)
2100                 if(!(srl->layflag & SCE_LAY_DISABLE))
2101                         break;
2102         if(srl==NULL) {
2103                 re->error("All RenderLayers are disabled");
2104                 return 0;
2105         }
2106         
2107         /* renderer */
2108         if(!ELEM(re->r.renderer, R_INTERN, R_YAFRAY)) {
2109                 re->error("Unknown render engine set");
2110                 return 0;
2111         }
2112         return 1;
2113 }
2114
2115 /* evaluating scene options for general Blender render */
2116 static int render_initialize_from_scene(Render *re, Scene *scene)
2117 {
2118         int winx, winy;
2119         rcti disprect;
2120         
2121         /* r.xsch and r.ysch has the actual view window size
2122                 r.border is the clipping rect */
2123         
2124         /* calculate actual render result and display size */
2125         winx= (scene->r.size*scene->r.xsch)/100;
2126         winy= (scene->r.size*scene->r.ysch)/100;
2127         
2128         /* we always render smaller part, inserting it in larger image is compositor bizz, it uses disprect for it */
2129         if(scene->r.mode & R_BORDER) {
2130                 disprect.xmin= scene->r.border.xmin*winx;
2131                 disprect.xmax= scene->r.border.xmax*winx;
2132                 
2133                 disprect.ymin= scene->r.border.ymin*winy;
2134                 disprect.ymax= scene->r.border.ymax*winy;
2135         }
2136         else {
2137                 disprect.xmin= disprect.ymin= 0;
2138                 disprect.xmax= winx;
2139                 disprect.ymax= winy;
2140         }
2141         
2142         if(scene->r.scemode & R_EXR_TILE_FILE) {
2143                 int partx= winx/scene->r.xparts, party= winy/scene->r.yparts;
2144                 
2145                 /* stupid exr tiles dont like different sizes */
2146                 if(winx != partx*scene->r.xparts || winy != party*scene->r.yparts) {
2147                         re->error("Sorry... exr tile saving only allowed with equally sized parts");
2148                         return 0;
2149                 }
2150                 if((scene->r.mode & R_FIELDS) && (party & 1)) {
2151                         re->error("Sorry... exr tile saving only allowed with equally sized parts");
2152                         return 0;
2153                 }
2154         }
2155         
2156         if(scene->r.scemode & R_SINGLE_LAYER)
2157                 push_render_result(re);
2158         
2159         RE_InitState(re, &scene->r, winx, winy, &disprect);
2160         
2161         /* initstate makes new result, have to send changed tags around */
2162         ntreeCompositTagRender(re->scene);
2163         
2164         re->scene= scene;
2165         if(!is_rendering_allowed(re))
2166                 return 0;
2167         
2168         re->display_init(re->result);
2169         re->display_clear(re->result);
2170         
2171         return 1;
2172 }
2173
2174 /* general Blender frame render call */
2175 void RE_BlenderFrame(Render *re, Scene *scene, int frame)
2176 {
2177         /* ugly global still... is to prevent renderwin events and signal subsurfs etc to make full resol */
2178         /* is also set by caller renderwin.c */
2179         G.rendering= 1;
2180         
2181         scene->r.cfra= frame;
2182         
2183         if(render_initialize_from_scene(re, scene)) {
2184                 do_render_all_options(re);
2185         }
2186         
2187         /* UGLY WARNING */
2188         G.rendering= 0;
2189 }
2190
2191 static void do_write_image_or_movie(Render *re, Scene *scene, bMovieHandle *mh)
2192 {
2193         char name[FILE_MAX];
2194         RenderResult rres;
2195         
2196         RE_GetResultImage(re, &rres);
2197
2198         /* write movie or image */
2199         if(BKE_imtype_is_movie(scene->r.imtype)) {
2200                 int dofree = 0;
2201                 /* note; the way it gets 32 bits rects is weak... */
2202                 if(rres.rect32==NULL) {
2203                         rres.rect32= MEM_mapallocN(sizeof(int)*rres.rectx*rres.recty, "temp 32 bits rect");
2204                         dofree = 1;
2205                 }
2206                 RE_ResultGet32(re, (unsigned int *)rres.rect32);
2207                 mh->append_movie(scene->r.cfra, rres.rect32, rres.rectx, rres.recty);
2208                 if(dofree) {
2209                         MEM_freeN(rres.rect32);
2210                 }
2211                 printf("Append frame %d", scene->r.cfra);
2212         } 
2213         else {
2214                 BKE_makepicstring(name, scene->r.pic, scene->r.cfra, scene->r.imtype);
2215                 
2216                 if(re->r.imtype==R_MULTILAYER) {
2217                         if(re->result) {
2218                                 RE_WriteRenderResult(re->result, name, scene->r.quality);
2219                                 printf("Saved: %s", name);
2220                         }
2221                 }
2222                 else {
2223                         ImBuf *ibuf= IMB_allocImBuf(rres.rectx, rres.recty, scene->r.planes, 0, 0);
2224                         int ok;
2225                         
2226                                         /* if not exists, BKE_write_ibuf makes one */
2227                         ibuf->rect= (unsigned int *)rres.rect32;    
2228                         ibuf->rect_float= rres.rectf;
2229                         ibuf->zbuf_float= rres.rectz;
2230                         
2231                         /* float factor for random dither, imbuf takes care of it */
2232                         ibuf->dither= scene->r.dither_intensity;
2233
2234                         ok= BKE_write_ibuf(ibuf, name, scene->r.imtype, scene->r.subimtype, scene->r.quality);
2235                         
2236                         if(ok==0) {
2237                                 printf("Render error: cannot save %s\n", name);
2238                                 G.afbreek=1;
2239                         }
2240                         else printf("Saved: %s", name);
2241                         
2242                         /* optional preview images for exr */
2243                         if(ok && scene->r.imtype==R_OPENEXR && (scene->r.subimtype & R_PREVIEW_JPG)) {
2244                                 if(BLI_testextensie(name, ".exr")) 
2245                                         name[strlen(name)-4]= 0;
2246                                 BKE_add_image_extension(name, R_JPEG90);
2247                                 ibuf->depth= 24; 
2248                                 BKE_write_ibuf(ibuf, name, R_JPEG90, scene->r.subimtype, scene->r.quality);
2249                                 printf("\nSaved: %s", name);
2250                         }
2251                         
2252                                         /* imbuf knows which rects are not part of ibuf */
2253                         IMB_freeImBuf(ibuf);
2254                 }
2255         }
2256         
2257         BLI_timestr(re->i.lastframetime, name);
2258         printf(" Time: %s\n", name);
2259         fflush(stdout); /* needed for renderd !! (not anymore... (ton)) */
2260 }
2261
2262 /* saves images to disk */
2263 void RE_BlenderAnim(Render *re, Scene *scene, int sfra, int efra)
2264 {
2265         bMovieHandle *mh= BKE_get_movie_handle(scene->r.imtype);
2266         int cfrao= scene->r.cfra;
2267         
2268         /* do not call for each frame, it initializes & pops output window */
2269         if(!render_initialize_from_scene(re, scene))
2270                 return;
2271         
2272         /* ugly global still... is to prevent renderwin events and signal subsurfs etc to make full resol */
2273         /* is also set by caller renderwin.c */
2274         G.rendering= 1;
2275         
2276         if(BKE_imtype_is_movie(scene->r.imtype))
2277                 mh->start_movie(&re->r, re->rectx, re->recty);
2278         
2279         if (mh->get_next_frame) {
2280                 while (!(G.afbreek == 1)) {
2281                         int nf = mh->get_next_frame();
2282                         if (nf >= 0 && nf >= scene->r.sfra && nf <= scene->r.efra) {
2283                                 scene->r.cfra = re->r.cfra = nf;
2284                                 
2285                                 do_render_all_options(re);
2286
2287                                 if(re->test_break() == 0) {
2288                                         do_write_image_or_movie(re, scene, mh);
2289                                 }
2290                         } else {
2291                                 re->test_break();
2292                         }
2293                 }
2294         } else {
2295                 for(scene->r.cfra= sfra; 
2296                     scene->r.cfra<=efra; scene->r.cfra++) {
2297                         re->r.cfra= scene->r.cfra;         /* weak.... */
2298                 
2299                         do_render_all_options(re);
2300
2301                         if(re->test_break() == 0) {
2302                                 do_write_image_or_movie(re, scene, mh);
2303                         }
2304                 
2305                         if(G.afbreek==1) break;
2306                 }
2307         }
2308         
2309         /* end movie */
2310         if(BKE_imtype_is_movie(scene->r.imtype))
2311                 mh->end_movie();
2312
2313         scene->r.cfra= cfrao;
2314         
2315         /* UGLY WARNING */
2316         G.rendering= 0;
2317 }
2318
2319 /* note; repeated win/disprect calc... solve that nicer, also in compo */
2320
2321 /* only temp file! */
2322 void RE_ReadRenderResult(Scene *scene, Scene *scenode)
2323 {
2324         Render *re;
2325         int winx, winy;
2326         rcti disprect;
2327         
2328         /* calculate actual render result and display size */
2329         winx= (scene->r.size*scene->r.xsch)/100;
2330         winy= (scene->r.size*scene->r.ysch)/100;
2331         
2332         /* only in movie case we render smaller part */
2333         if(scene->r.mode & R_BORDER) {
2334                 disprect.xmin= scene->r.border.xmin*winx;
2335                 disprect.xmax= scene->r.border.xmax*winx;
2336                 
2337                 disprect.ymin= scene->r.border.ymin*winy;
2338                 disprect.ymax= scene->r.border.ymax*winy;
2339         }
2340         else {
2341                 disprect.xmin= disprect.ymin= 0;
2342                 disprect.xmax= winx;
2343                 disprect.ymax= winy;
2344         }
2345         
2346         if(scenode)
2347                 scene= scenode;
2348         
2349         re= RE_NewRender(scene->id.name);
2350         RE_InitState(re, &scene->r, winx, winy, &disprect);
2351         re->scene= scene;
2352         
2353         read_render_result(re);
2354 }
2355
2356 void RE_set_max_threads(int threads)
2357 {
2358         if(threads>0 && threads<=BLENDER_MAX_THREADS)
2359                 commandline_threads= threads;
2360         else
2361                 printf("Error, threads has to be in range 1-%d\n", BLENDER_MAX_THREADS);
2362 }