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