4ba5d83afa3fbc6adeea51b4f1a6ed0b3345c143
[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_node_types.h"
36 #include "DNA_object_types.h"
37 #include "DNA_scene_types.h"
38
39 #include "BKE_global.h"
40 #include "BKE_image.h"
41 #include "BKE_node.h"
42 #include "BKE_scene.h"
43 #include "BKE_writeavi.h"       /* <------ should be replaced once with generic movie module */
44
45 #include "MEM_guardedalloc.h"
46
47 #include "BLI_arithb.h"
48 #include "BLI_blenlib.h"
49 #include "BLI_threads.h"
50
51 #include "PIL_time.h"
52 #include "IMB_imbuf.h"
53 #include "IMB_imbuf_types.h"
54
55 #include "intern/openexr/openexr_multi.h"
56
57 #include "RE_pipeline.h"
58 #include "radio.h"
59
60 #include "BSE_sequence.h"  /* <----------------- bad!!! */
61
62 #include "SDL_thread.h"
63
64 /* internal */
65 #include "render_types.h"
66 #include "renderpipeline.h"
67 #include "renderdatabase.h"
68 #include "rendercore.h"
69 #include "envmap.h"
70 #include "initrender.h"
71 #include "shadbuf.h"
72 #include "zbuf.h"
73
74 /* render flow
75
76 1) Initialize state
77 - state data, tables
78 - movie/image file init
79 - everything that doesn't change during animation
80
81 2) Initialize data
82 - camera, world, matrices
83 - make render verts, faces, halos, strands
84 - everything can change per frame/field
85
86 3) Render Processor
87 - multiple layers
88 - tiles, rect, baking
89 - layers/tiles optionally to disk or directly in Render Result
90
91 4) Composit Render Result
92 - also read external files etc
93
94 5) Image Files
95 - save file or append in movie
96
97 */
98
99
100 /* ********* globals ******** */
101
102 /* here we store all renders */
103 static struct ListBase RenderList= {NULL, NULL};
104
105 /* hardcopy of current render, used while rendering for speed */
106 volatile Render R;
107
108 static SDL_mutex *exrtile_lock= NULL;
109
110 /* ********* alloc and free ******** */
111
112
113 static volatile int g_break= 0;
114 static int thread_break(void)
115 {
116         return g_break;
117 }
118
119 /* default callbacks, set in each new render */
120 static void result_nothing(RenderResult *rr) {}
121 static void result_rcti_nothing(RenderResult *rr, volatile struct rcti *rect) {}
122 static void stats_nothing(RenderStats *rs) {}
123 static void int_nothing(int val) {}
124 static int void_nothing(void) {return 0;}
125 static void print_error(const char *str) {printf("ERROR: %s\n", str);}
126
127 static void stats_background(RenderStats *rs)
128 {
129         extern int mem_in_use;
130         float megs_used_memory= mem_in_use/(1024.0*1024.0);
131         char str[400], *spos= str;
132         
133         if(rs->convertdone) {
134                 
135                 spos+= sprintf(spos, "Fra:%d Mem:%.2fM ", G.scene->r.cfra, megs_used_memory);
136                 
137                 if(rs->infostr) {
138                         spos+= sprintf(spos, " | %s", rs->infostr);
139                 }
140                 else {
141                         if(rs->tothalo)
142                                 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);
143                         else 
144                                 spos+= sprintf(spos, "Sce: %s Ve:%d Fa:%d La:%d", G.scene->id.name+2, rs->totvert, rs->totface, rs->totlamp);
145                 }
146                 printf(str); printf("\n");
147         }       
148 }
149
150 static void free_render_result(RenderResult *res)
151 {
152         if(res==NULL) return;
153
154         while(res->layers.first) {
155                 RenderLayer *rl= res->layers.first;
156                 
157                 if(rl->rectf) MEM_freeT(rl->rectf);
158                 while(rl->passes.first) {
159                         RenderPass *rpass= rl->passes.first;
160                         if(rpass->rect) MEM_freeT(rpass->rect);
161                         BLI_remlink(&rl->passes, rpass);
162                         MEM_freeT(rpass);
163                 }
164                 BLI_remlink(&res->layers, rl);
165                 MEM_freeT(rl);
166         }
167         
168         if(res->rect32)
169                 MEM_freeT(res->rect32);
170         if(res->rectz)
171                 MEM_freeT(res->rectz);
172         if(res->rectf)
173                 MEM_freeT(res->rectf);
174         
175         MEM_freeT(res);
176 }
177
178 static char *get_pass_name(int passtype, int channel)
179 {
180         
181         if(passtype == SCE_PASS_COMBINED) {
182                 if(channel==0) return "Combined.R";
183                 else if(channel==1) return "Combined.G";
184                 else if(channel==2) return "Combined.B";
185                 else return "Combined.A";
186         }
187         if(passtype == SCE_PASS_Z)
188                 return "Z";
189         if(passtype == SCE_PASS_VECTOR) {
190                 if(channel==0) return "Vector.X";
191                 else if(channel==1) return "Vector.Y";
192                 else if(channel==2) return "Vector.Z";
193                 else return "Vector.W";
194         }
195         if(passtype == SCE_PASS_NORMAL) {
196                 if(channel==0) return "Normal.X";
197                 else if(channel==1) return "Normal.Y";
198                 else return "Normal.Z";
199         }
200         if(passtype == SCE_PASS_RGBA) {
201                 if(channel==0) return "Color.R";
202                 else if(channel==1) return "Color.G";
203                 else if(channel==2) return "Color.B";
204                 else return "Color.A";
205         }
206         if(passtype == SCE_PASS_DIFFUSE) {
207                 if(channel==0) return "Diffuse.R";
208                 else if(channel==1) return "Diffuse.G";
209                 else return "Diffuse.B";
210         }
211         if(passtype == SCE_PASS_SPEC) {
212                 if(channel==0) return "Spec.R";
213                 else if(channel==1) return "Spec.G";
214                 else return "Spec.B";
215         }
216         if(passtype == SCE_PASS_SHADOW) {
217                 if(channel==0) return "Shadow.R";
218                 else if(channel==1) return "Shadow.G";
219                 else return "Shadow.B";
220         }
221         if(passtype == SCE_PASS_AO) {
222                 if(channel==0) return "AO.R";
223                 else if(channel==1) return "AO.G";
224                 else return "AO.B";
225         }
226         if(passtype == SCE_PASS_RAY) {
227                 if(channel==0) return "Ray.R";
228                 else if(channel==1) return "Ray.G";
229                 else return "Ray.B";
230         }
231         return "Unknown";
232 }
233
234 static void render_layer_add_pass(RenderResult *rr, RenderLayer *rl, int channels, int passtype)
235 {
236         char *typestr= get_pass_name(passtype, 0);
237         RenderPass *rpass= MEM_callocT(sizeof(RenderPass), typestr);
238         int rectsize= rr->rectx*rr->recty*channels;
239         
240         BLI_addtail(&rl->passes, rpass);
241         rpass->passtype= passtype;
242         rpass->channels= channels;
243         
244         if(rr->exrhandle) {
245                 int a;
246                 for(a=0; a<channels; a++)
247                         IMB_exr_add_channel(rr->exrhandle, rl->name, get_pass_name(passtype, a));
248         }
249         else {
250                 if(passtype==SCE_PASS_VECTOR) {
251                         float *rect;
252                         int x;
253                         
254                         /* initialize to max speed */
255                         rect= rpass->rect= MEM_mapallocT(sizeof(float)*rectsize, typestr);
256                         for(x= rectsize-1; x>=0; x--)
257                                 rect[x]= PASS_VECTOR_MAX;
258                 }
259                 else
260                         rpass->rect= MEM_mapallocT(sizeof(float)*rectsize, typestr);
261         }
262 }
263
264 float *RE_RenderLayerGetPass(RenderLayer *rl, int passtype)
265 {
266         RenderPass *rpass;
267         
268         for(rpass=rl->passes.first; rpass; rpass= rpass->next)
269                 if(rpass->passtype== passtype)
270                         return rpass->rect;
271         return NULL;
272 }
273
274
275 /* called by main render as well for parts */
276 /* will read info from Render *re to define layers */
277 /* called in threads */
278 /* re->winx,winy is coordinate space of entire image, partrct the part within */
279 static RenderResult *new_render_result(volatile Render *re, rcti *partrct, int crop)
280 {
281         RenderResult *rr;
282         RenderLayer *rl;
283         SceneRenderLayer *srl;
284         int rectx, recty, nr;
285         
286         rectx= partrct->xmax - partrct->xmin;
287         recty= partrct->ymax - partrct->ymin;
288         
289         if(rectx<=0 || recty<=0)
290                 return NULL;
291         
292         rr= MEM_callocT(sizeof(RenderResult), "new render result");
293         rr->rectx= rectx;
294         rr->recty= recty;
295         /* crop is one or two extra pixels rendered for filtering, is used for merging and display too */
296         rr->crop= crop;
297         
298         /* tilerect is relative coordinates within render disprect. do not subtract crop yet */
299         rr->tilerect.xmin= partrct->xmin - re->disprect.xmin;
300         rr->tilerect.xmax= partrct->xmax - re->disprect.xmax;
301         rr->tilerect.ymin= partrct->ymin - re->disprect.ymin;
302         rr->tilerect.ymax= partrct->ymax - re->disprect.ymax;
303         
304         /* this flag needs cleared immediate after result was made */
305         if(re->flag & R_FILEBUFFER) {
306                 rr->exrhandle= IMB_exr_get_handle();
307         }
308         
309         /* check renderdata for amount of layers */
310         for(nr=0, srl= re->r.layers.first; srl; srl= srl->next, nr++) {
311                 
312                 if((re->r.scemode & R_SINGLE_LAYER) && nr!=re->r.actlay)
313                         continue;
314                 
315                 rl= MEM_callocT(sizeof(RenderLayer), "new render layer");
316                 BLI_addtail(&rr->layers, rl);
317                 
318                 strcpy(rl->name, srl->name);
319                 rl->lay= srl->lay;
320                 rl->layflag= srl->layflag;
321                 rl->passflag= srl->passflag;
322                 
323                 if(rr->exrhandle) {
324                         IMB_exr_add_channel(rr->exrhandle, rl->name, "Combined.R");
325                         IMB_exr_add_channel(rr->exrhandle, rl->name, "Combined.G");
326                         IMB_exr_add_channel(rr->exrhandle, rl->name, "Combined.B");
327                         IMB_exr_add_channel(rr->exrhandle, rl->name, "Combined.A");
328                 }
329                 else
330                         rl->rectf= MEM_mapallocT(rectx*recty*sizeof(float)*4, "Combined rgba");
331                 
332                 if(srl->passflag  & SCE_PASS_Z)
333                         render_layer_add_pass(rr, rl, 1, SCE_PASS_Z);
334                 if(srl->passflag  & SCE_PASS_VECTOR)
335                         render_layer_add_pass(rr, rl, 4, SCE_PASS_VECTOR);
336                 if(srl->passflag  & SCE_PASS_NORMAL)
337                         render_layer_add_pass(rr, rl, 3, SCE_PASS_NORMAL);
338                 if(srl->passflag  & SCE_PASS_RGBA)
339                         render_layer_add_pass(rr, rl, 4, SCE_PASS_RGBA);
340                 if(srl->passflag  & SCE_PASS_DIFFUSE)
341                         render_layer_add_pass(rr, rl, 3, SCE_PASS_DIFFUSE);
342                 if(srl->passflag  & SCE_PASS_SPEC)
343                         render_layer_add_pass(rr, rl, 3, SCE_PASS_SPEC);
344                 if(srl->passflag  & SCE_PASS_SHADOW)
345                         render_layer_add_pass(rr, rl, 3, SCE_PASS_SHADOW);
346                 if(srl->passflag  & SCE_PASS_AO)
347                         render_layer_add_pass(rr, rl, 3, SCE_PASS_AO);
348                 if(srl->passflag  & SCE_PASS_RAY)
349                         render_layer_add_pass(rr, rl, 3, SCE_PASS_RAY);
350                 
351         }
352         /* previewrender and envmap don't do layers, so we make a default one */
353         if(rr->layers.first==NULL) {
354                 rl= MEM_callocT(sizeof(RenderLayer), "new render layer");
355                 BLI_addtail(&rr->layers, rl);
356                 
357                 rl->rectf= MEM_mapallocT(rectx*recty*sizeof(float)*4, "prev/env float rgba");
358                 
359                 /* note, this has to be in sync with scene.c */
360                 rl->lay= (1<<20) -1;
361                 rl->layflag= 0x7FFF;    /* solid ztra halo strand */
362                 rl->passflag= SCE_PASS_COMBINED;
363                 
364                 re->r.actlay= 0;
365         }
366         
367         return rr;
368 }
369
370 static int render_result_needs_vector(Render *re)
371 {
372         if(re->r.scemode & R_DOCOMP) {
373                 RenderLayer *rl;
374         
375                 for(rl= re->result->layers.first; rl; rl= rl->next)
376                         if(rl->passflag & SCE_PASS_VECTOR)
377                                 return 1;
378         }
379         return 0;
380 }
381
382 static void do_merge_tile(RenderResult *rr, RenderResult *rrpart, float *target, float *tile, int pixsize)
383 {
384         int y, ofs, copylen, tilex, tiley;
385         
386         copylen= tilex= rrpart->rectx;
387         tiley= rrpart->recty;
388         
389         if(rrpart->crop) {      /* filters add pixel extra */
390                 tile+= pixsize*(rrpart->crop + rrpart->crop*tilex);
391                 
392                 copylen= tilex - 2*rrpart->crop;
393                 tiley -= 2*rrpart->crop;
394                 
395                 ofs= (rrpart->tilerect.ymin + rrpart->crop)*rr->rectx + (rrpart->tilerect.xmin+rrpart->crop);
396                 target+= pixsize*ofs;
397         }
398         else {
399                 ofs= (rrpart->tilerect.ymin*rr->rectx + rrpart->tilerect.xmin);
400                 target+= pixsize*ofs;
401         }
402
403         copylen *= sizeof(float)*pixsize;
404         tilex *= pixsize;
405         ofs= pixsize*rr->rectx;
406
407         for(y=0; y<tiley; y++) {
408                 memcpy(target, tile, copylen);
409                 target+= ofs;
410                 tile+= tilex;
411         }
412 }
413
414 /* used when rendering to a full buffer, or when reading the exr part-layer-pass file */
415 /* no test happens here if it fits... we also assume layers are in sync */
416 /* is used within threads */
417 static void merge_render_result(RenderResult *rr, RenderResult *rrpart)
418 {
419         RenderLayer *rl, *rlp;
420         RenderPass *rpass, *rpassp;
421         
422         for(rl= rr->layers.first, rlp= rrpart->layers.first; rl && rlp; rl= rl->next, rlp= rlp->next) {
423                 
424                 /* combined */
425                 if(rl->rectf && rlp->rectf)
426                         do_merge_tile(rr, rrpart, rl->rectf, rlp->rectf, 4);
427                 
428                 /* passes are allocated in sync */
429                 for(rpass= rl->passes.first, rpassp= rlp->passes.first; rpass && rpassp; rpass= rpass->next, rpassp= rpassp->next) {
430                         do_merge_tile(rr, rrpart, rpass->rect, rpassp->rect, rpass->channels);
431                 }
432         }
433 }
434
435
436 static void save_render_result_tile(volatile Render *re, RenderPart *pa)
437 {
438         RenderResult *rrpart= pa->result;
439         RenderLayer *rlp;
440         RenderPass *rpassp;
441         int offs, partx, party;
442         
443         if(exrtile_lock) SDL_mutexP(exrtile_lock);
444         
445         for(rlp= rrpart->layers.first; rlp; rlp= rlp->next) {
446                 
447                 if(rrpart->crop) {      /* filters add pixel extra */
448                         offs= (rrpart->crop + rrpart->crop*rrpart->rectx);
449                 }
450                 else {
451                         offs= 0;
452                 }
453                 
454                 /* combined */
455                 if(rlp->rectf) {
456                         int a, xstride= 4;
457                         for(a=0; a<xstride; a++)
458                                 IMB_exr_set_channel(re->result->exrhandle, rlp->name, get_pass_name(SCE_PASS_COMBINED, a), 
459                                                                 xstride, xstride*pa->rectx, rlp->rectf+a + xstride*offs);
460                 }
461                 
462                 /* passes are allocated in sync */
463                 for(rpassp= rlp->passes.first; rpassp; rpassp= rpassp->next) {
464                         int a, xstride= rpassp->channels;
465                         for(a=0; a<xstride; a++)
466                                 IMB_exr_set_channel(re->result->exrhandle, rlp->name, get_pass_name(rpassp->passtype, a), 
467                                                                         xstride, xstride*pa->rectx, rpassp->rect+a + xstride*offs);
468                 }
469                 
470         }
471
472         party= rrpart->tilerect.ymin + rrpart->crop;
473         partx= rrpart->tilerect.xmin + rrpart->crop;
474
475         IMB_exrtile_write_channels(re->result->exrhandle, partx, party);
476
477         if(exrtile_lock) SDL_mutexV(exrtile_lock);
478
479 }
480
481 static void read_render_result(Render *re)
482 {
483         RenderLayer *rl;
484         RenderPass *rpass;
485         void *exrhandle= IMB_exr_get_handle();
486         int rectx, recty;
487         
488         free_render_result(re->result);
489         re->result= new_render_result(re, &re->disprect, 0);
490
491         IMB_exr_begin_read(exrhandle, "/tmp/render.exr", &rectx, &recty);
492         if(rectx!=re->result->rectx || recty!=re->result->recty) {
493                 printf("error in reading render result\n");
494         }
495         else {
496                 for(rl= re->result->layers.first; rl; rl= rl->next) {
497                         
498                         /* combined */
499                         if(rl->rectf) {
500                                 int a, xstride= 4;
501                                 for(a=0; a<xstride; a++)
502                                         IMB_exr_set_channel(exrhandle, rl->name, get_pass_name(SCE_PASS_COMBINED, a), 
503                                                                                 xstride, xstride*rectx, rl->rectf+a);
504                         }
505                         
506                         /* passes are allocated in sync */
507                         for(rpass= rl->passes.first; rpass; rpass= rpass->next) {
508                                 int a, xstride= rpass->channels;
509                                 for(a=0; a<xstride; a++)
510                                         IMB_exr_set_channel(exrhandle, rl->name, get_pass_name(rpass->passtype, a), 
511                                                                                 xstride, xstride*rectx, rpass->rect+a);
512                         }
513                         
514                 }
515                 printf("before read\n");
516                 IMB_exr_read_channels(exrhandle);
517                 printf("after read\n");
518         }
519         
520         IMB_exr_close(exrhandle);
521 }
522
523 /* *************************************************** */
524
525 Render *RE_GetRender(const char *name)
526 {
527         Render *re;
528         
529         /* search for existing renders */
530         for(re= RenderList.first; re; re= re->next) {
531                 if(strncmp(re->name, name, RE_MAXNAME)==0) {
532                         break;
533                 }
534         }
535         return re;
536 }
537
538 /* if you want to know exactly what has been done */
539 RenderResult *RE_GetResult(Render *re)
540 {
541         if(re)
542                 return re->result;
543         return NULL;
544 }
545
546 RenderLayer *render_get_active_layer(Render *re, RenderResult *rr)
547 {
548         if(re->r.scemode & R_SINGLE_LAYER)
549                 return rr->layers.first;
550         else 
551                 return BLI_findlink(&rr->layers, re->r.actlay);
552         
553 }
554
555
556 /* fill provided result struct with what's currently active or done */
557 void RE_GetResultImage(Render *re, RenderResult *rr)
558 {
559         memset(rr, 0, sizeof(RenderResult));
560
561         if(re && re->result) {
562                 RenderLayer *rl;
563                 
564                 rr->rectx= re->result->rectx;
565                 rr->recty= re->result->recty;
566                 
567                 rr->rectf= re->result->rectf;
568                 rr->rectz= re->result->rectz;
569                 rr->rect32= re->result->rect32;
570                 
571                 /* active layer */
572                 rl= render_get_active_layer(re, re->result);
573
574                 if(rl) {
575                         if(rr->rectf==NULL)
576                                 rr->rectf= rl->rectf;
577                         if(rr->rectz==NULL)
578                                 rr->rectz= RE_RenderLayerGetPass(rl, SCE_PASS_Z);       
579                 }
580         }
581 }
582
583 #define FTOCHAR(val) val<=0.0f?0: (val>=1.0f?255: (char)(255.0f*val))
584 /* caller is responsible for allocating rect in correct size! */
585 void RE_ResultGet32(Render *re, unsigned int *rect)
586 {
587         RenderResult rres;
588         
589         RE_GetResultImage(re, &rres);
590         if(rres.rect32) 
591                 memcpy(rect, rres.rect32, sizeof(int)*rres.rectx*rres.recty);
592         else if(rres.rectf) {
593                 float *fp= rres.rectf;
594                 int tot= rres.rectx*rres.recty;
595                 char *cp= (char *)rect;
596                 
597                 for(;tot>0; tot--, cp+=4, fp+=4) {
598                         cp[0] = FTOCHAR(fp[0]);
599                         cp[1] = FTOCHAR(fp[1]);
600                         cp[2] = FTOCHAR(fp[2]);
601                         cp[3] = FTOCHAR(fp[3]);
602                 }
603         }
604         else
605                 /* else fill with black */
606                 memset(rect, 0, sizeof(int)*re->rectx*re->recty);
607 }
608
609
610 RenderStats *RE_GetStats(Render *re)
611 {
612         return &re->i;
613 }
614
615 Render *RE_NewRender(const char *name)
616 {
617         Render *re;
618         
619         /* only one render per name exists */
620         re= RE_GetRender(name);
621         if(re) {
622                 BLI_remlink(&RenderList, re);
623                 RE_FreeRender(re);
624         }
625         
626         /* new render data struct */
627         re= MEM_callocT(sizeof(Render), "new render");
628         BLI_addtail(&RenderList, re);
629         strncpy(re->name, name, RE_MAXNAME);
630         
631         /* set default empty callbacks */
632         re->display_init= result_nothing;
633         re->display_clear= result_nothing;
634         re->display_draw= result_rcti_nothing;
635         re->timecursor= int_nothing;
636         re->test_break= void_nothing;
637         re->test_return= void_nothing;
638         re->error= print_error;
639         if(G.background)
640                 re->stats_draw= stats_background;
641         else
642                 re->stats_draw= stats_nothing;
643         
644         /* init some variables */
645         re->ycor= 1.0f;
646         
647         return re;
648 }
649
650 /* only call this while you know it will remove the link too */
651 void RE_FreeRender(Render *re)
652 {
653         
654         free_renderdata_tables(re);
655         free_sample_tables(re);
656         
657         free_render_result(re->result);
658         
659         BLI_remlink(&RenderList, re);
660         MEM_freeT(re);
661 }
662
663 /* exit blender */
664 void RE_FreeAllRender(void)
665 {
666         while(RenderList.first) {
667                 RE_FreeRender(RenderList.first);
668         }
669 }
670
671 /* ********* initialize state ******** */
672
673
674 /* what doesn't change during entire render sequence */
675 /* disprect is optional, if NULL it assumes full window render */
676 void RE_InitState(Render *re, RenderData *rd, int winx, int winy, rcti *disprect)
677 {
678         re->ok= TRUE;   /* maybe flag */
679         
680         re->i.starttime= PIL_check_seconds_timer();
681         re->r= *rd;             /* hardcopy */
682         
683         re->winx= winx;
684         re->winy= winy;
685         if(disprect) {
686                 re->disprect= *disprect;
687                 re->rectx= disprect->xmax-disprect->xmin;
688                 re->recty= disprect->ymax-disprect->ymin;
689         }
690         else {
691                 re->disprect.xmin= re->disprect.xmax= 0;
692                 re->disprect.xmax= winx;
693                 re->disprect.ymax= winy;
694                 re->rectx= winx;
695                 re->recty= winy;
696         }
697         
698         if(re->rectx < 2 || re->recty < 2) {
699                 re->error("Image too small");
700                 re->ok= 0;
701         }
702         else {
703                 /* check state variables, osa? */
704                 if(re->r.mode & (R_OSA|R_MBLUR)) {
705                         re->osa= re->r.osa;
706                         if(re->osa>16) re->osa= 16;
707                 }
708                 else re->osa= 0;
709                 
710                 /* always call, checks for gamma, gamma tables and jitter too */
711                 make_sample_tables(re); 
712                 
713                 /* initialize render result */
714                 free_render_result(re->result);
715                 re->result= new_render_result(re, &re->disprect, 0);
716                         
717                 /* single layer render disables composit */
718                 if(re->r.scemode & R_SINGLE_LAYER)
719                         re->r.scemode &= ~R_DOCOMP;
720         }
721 }
722
723 void RE_SetDispRect (struct Render *re, rcti *disprect)
724 {
725         re->disprect= *disprect;
726         re->rectx= disprect->xmax-disprect->xmin;
727         re->recty= disprect->ymax-disprect->ymin;
728         
729         /* initialize render result */
730         free_render_result(re->result);
731         re->result= new_render_result(re, &re->disprect, 0);
732 }
733
734 void RE_SetWindow(Render *re, rctf *viewplane, float clipsta, float clipend)
735 {
736         /* re->ok flag? */
737         
738         re->viewplane= *viewplane;
739         re->clipsta= clipsta;
740         re->clipend= clipend;
741
742         i_window(re->viewplane.xmin, re->viewplane.xmax, re->viewplane.ymin, re->viewplane.ymax, re->clipsta, re->clipend, re->winmat);
743 }
744
745 void RE_SetOrtho(Render *re, rctf *viewplane, float clipsta, float clipend)
746 {
747         /* re->ok flag? */
748         
749         re->viewplane= *viewplane;
750         re->clipsta= clipsta;
751         re->clipend= clipend;
752         re->r.mode |= R_ORTHO;
753
754         i_ortho(re->viewplane.xmin, re->viewplane.xmax, re->viewplane.ymin, re->viewplane.ymax, re->clipsta, re->clipend, re->winmat);
755 }
756
757 void RE_SetView(Render *re, float mat[][4])
758 {
759         /* re->ok flag? */
760         Mat4CpyMat4(re->viewmat, mat);
761         Mat4Invert(re->viewinv, re->viewmat);
762 }
763
764 /* image and movie output has to move to either imbuf or kernel */
765 void RE_display_init_cb(Render *re, void (*f)(RenderResult *rr))
766 {
767         re->display_init= f;
768 }
769 void RE_display_clear_cb(Render *re, void (*f)(RenderResult *rr))
770 {
771         re->display_clear= f;
772 }
773 void RE_display_draw_cb(Render *re, void (*f)(RenderResult *rr, volatile rcti *rect))
774 {
775         re->display_draw= f;
776 }
777
778 void RE_stats_draw_cb(Render *re, void (*f)(RenderStats *rs))
779 {
780         re->stats_draw= f;
781 }
782 void RE_timecursor_cb(Render *re, void (*f)(int))
783 {
784         re->timecursor= f;
785 }
786
787 void RE_test_break_cb(Render *re, int (*f)(void))
788 {
789         re->test_break= f;
790 }
791 void RE_test_return_cb(Render *re, int (*f)(void))
792 {
793         re->test_return= f;
794 }
795 void RE_error_cb(Render *re, void (*f)(const char *str))
796 {
797         re->error= f;
798 }
799
800
801 /* ********* add object data (later) ******** */
802
803 /* object is considered fully prepared on correct time etc */
804 /* includes lights */
805 void RE_AddObject(Render *re, Object *ob)
806 {
807         
808 }
809
810 /* *************************************** */
811
812 static int do_part_thread(void *pa_v)
813 {
814         RenderPart *pa= pa_v;
815         
816         /* need to return nicely all parts on esc */
817         if(R.test_break()==0) {
818                 
819                 pa->result= new_render_result(&R, &pa->disprect, pa->crop);
820                 
821                 if(R.osa)
822                         zbufshadeDA_tile(pa);
823                 else
824                         zbufshade_tile(pa);
825                 
826                 /* merge too on break! */
827                 if(R.result->exrhandle)
828                         save_render_result_tile(&R, pa);
829                 else
830                         merge_render_result(R.result, pa->result);
831         }
832         
833         pa->ready= 1;
834         
835         return 0;
836 }
837
838 /* returns with render result filled, not threaded */
839 static void render_tile_processor(Render *re, int firsttile)
840 {
841         RenderPart *pa;
842         
843         if(re->test_break())
844                 return;
845         
846         re->i.lastframetime= PIL_check_seconds_timer()- re->i.starttime;
847         re->stats_draw(&re->i);
848         re->i.starttime= PIL_check_seconds_timer();
849  
850         if(re->result==NULL)
851                 return;
852         
853         initparts(re);
854         
855         /* assuming no new data gets added to dbase... */
856         R= *re;
857         
858         for(pa= re->parts.first; pa; pa= pa->next) {
859                 if(firsttile) {
860                         re->i.partsdone++;      /* was reset in initparts */
861                         firsttile--;
862                 }
863                 else {
864                         do_part_thread(pa);
865                         
866                         if(pa->result) {
867                                 if(!re->test_break()) {
868                                         re->display_draw(pa->result, NULL);
869                                         
870                                         re->i.partsdone++;
871                                 }
872                                 free_render_result(pa->result);
873                                 pa->result= NULL;
874                         }               
875                         if(re->test_break())
876                                 break;
877                 }
878         }
879         
880         re->i.lastframetime= PIL_check_seconds_timer()- re->i.starttime;
881         re->stats_draw(&re->i);
882
883         freeparts(re);
884 }
885
886 static RenderPart *find_next_part(Render *re)
887 {
888         RenderPart *pa, *best= NULL;
889         int centx=re->winx/2, centy=re->winy/2, tot=1;
890         int mindist, distx, disty;
891         
892         /* find center of rendered parts, image center counts for 1 too */
893         for(pa= re->parts.first; pa; pa= pa->next) {
894                 if(pa->ready) {
895                         centx+= (pa->disprect.xmin+pa->disprect.xmax)/2;
896                         centy+= (pa->disprect.ymin+pa->disprect.ymax)/2;
897                         tot++;
898                 }
899         }
900         centx/=tot;
901         centy/=tot;
902         
903         /* closest of the non-rendering parts */
904         mindist= re->winx*re->winy;
905         for(pa= re->parts.first; pa; pa= pa->next) {
906                 if(pa->ready==0 && pa->nr==0) {
907                         distx= centx - (pa->disprect.xmin+pa->disprect.xmax)/2;
908                         disty= centy - (pa->disprect.ymin+pa->disprect.ymax)/2;
909                         distx= (int)sqrt(distx*distx + disty*disty);
910                         if(distx<mindist) {
911                                 best= pa;
912                                 mindist= distx;
913                         }
914                 }
915         }
916         return best;
917 }
918
919 static void print_part_stats(Render *re, RenderPart *pa)
920 {
921         char str[64];
922         
923         sprintf(str, "Part %d-%d", pa->nr, re->i.totpart);
924         re->i.infostr= str;
925         re->stats_draw(&re->i);
926         re->i.infostr= NULL;
927 }
928
929 static void threaded_tile_processor(Render *re)
930 {
931         ListBase threads;
932         RenderPart *pa, *nextpa;
933         RenderResult *rr= re->result;
934         int maxthreads, rendering=1, counter= 1, drawtimer=0, hasdrawn;
935         
936         if(rr==NULL)
937                 return;
938         if(re->test_break())
939                 return;
940         
941         if(rr->exrhandle) {
942                 IMB_exrtile_begin_write(rr->exrhandle, "/tmp/render.exr", rr->rectx, rr->recty, rr->rectx/re->r.xparts, rr->recty/re->r.yparts);
943                 exrtile_lock = SDL_CreateMutex();
944         }
945         
946         if(re->r.mode & R_THREADS) maxthreads= 2;
947         else maxthreads= 1;
948         
949         initparts(re);
950         BLI_init_threads(&threads, do_part_thread, maxthreads);
951         
952         /* assuming no new data gets added to dbase... */
953         R= *re;
954         
955         /* set threadsafe break */
956         R.test_break= thread_break;
957         
958         /* timer loop demands to sleep when no parts are left */
959         nextpa= find_next_part(re);
960         
961         while(rendering) {
962                 
963                 if(nextpa && BLI_available_threads(&threads) && !re->test_break()) {
964                         drawtimer= 0;
965                         nextpa->nr= counter++;  /* for nicest part, and for stats */
966                         nextpa->thread= BLI_available_thread_index(&threads);   /* sample index */
967                         BLI_insert_thread(&threads, nextpa);
968
969                         nextpa= find_next_part(re);
970                 }
971                 else {
972                         PIL_sleep_ms(50);
973                         drawtimer++;
974                 }
975                 
976                 /* check for ready ones to display, and if we need to continue */
977                 rendering= 0;
978                 hasdrawn= 0;
979                 for(pa= re->parts.first; pa; pa= pa->next) {
980                         if(pa->ready) {
981                                 if(pa->result) {
982                                         BLI_remove_thread(&threads, pa);
983
984                                         re->display_draw(pa->result, NULL);
985                                         print_part_stats(re, pa);
986                                         
987                                         free_render_result(pa->result);
988                                         pa->result= NULL;
989                                         re->i.partsdone++;
990                                         hasdrawn= 1;
991                                 }
992                         }
993                         else {
994                                 rendering= 1;
995                                 if(pa->nr && pa->result && drawtimer>20) {
996                                         re->display_draw(pa->result, &pa->result->renrect);
997                                         hasdrawn= 1;
998                                 }
999                         }
1000                 }
1001                 if(hasdrawn)
1002                         drawtimer= 0;
1003
1004                 /* on break, wait for all slots to get freed */
1005                 if( (g_break=re->test_break()) && BLI_available_threads(&threads)==maxthreads)
1006                         rendering= 0;
1007                 
1008         }
1009         
1010         if(rr->exrhandle) {
1011                 if(exrtile_lock) SDL_DestroyMutex(exrtile_lock); 
1012                 IMB_exr_close(rr->exrhandle);
1013                 read_render_result(re);
1014         }
1015         
1016         /* unset threadsafety */
1017         g_break= 0;
1018         
1019         BLI_end_threads(&threads);
1020         freeparts(re);
1021 }
1022
1023 /* currently only called by preview renders */
1024 void RE_TileProcessor(Render *re, int firsttile)
1025 {
1026         if(0) 
1027                 threaded_tile_processor(re);
1028         else
1029                 render_tile_processor(re, firsttile);   
1030 }
1031
1032
1033 /* ************  This part uses API, for rendering Blender scenes ********** */
1034
1035 void render_one_frame(Render *re)
1036 {
1037         
1038 //      re->cfra= cfra; /* <- unused! */
1039         
1040         /* make render verts/faces/halos/lamps */
1041         if(render_result_needs_vector(re))
1042                 RE_Database_FromScene_Vectors(re, re->scene);
1043         else
1044            RE_Database_FromScene(re, re->scene, 1);
1045         
1046         threaded_tile_processor(re);
1047         //render_tile_processor(re, 0);
1048         
1049         /* free all render verts etc */
1050         RE_Database_Free(re);
1051 }
1052
1053 /* accumulates osa frames */
1054 static void do_render_blurred(Render *re, float frame)
1055 {
1056         
1057 }
1058
1059 /* interleaves 2 frames */
1060 static void do_render_fields(Render *re)
1061 {
1062         
1063 }
1064
1065 static void do_render_scene_node(Render *re, Scene *sce, int cfra)
1066 {
1067         Render *resc= RE_NewRender(sce->id.name+2);
1068         
1069         sce->r.cfra= cfra;
1070         
1071         /* makes render result etc */
1072         RE_InitState(resc, &sce->r, re->winx, re->winy, &re->disprect);
1073         
1074         /* this to enable this scene to create speed vectors */
1075         resc->r.scemode |= R_DOCOMP;
1076         
1077         /* now use renderdata and camera to set viewplane */
1078         if(sce->camera==NULL) return;
1079         RE_SetCamera(resc, sce->camera);
1080         
1081         /* still unsure entity this... */
1082         resc->scene= sce;
1083         
1084         /* ensure scene has depsgraph, base flags etc OK. Warning... also sets G.scene */
1085         set_scene_bg(sce);
1086
1087         /* copy callbacks */
1088         resc->display_draw= re->display_draw;
1089         resc->test_break= re->test_break;
1090         resc->stats_draw= re->stats_draw;
1091         
1092         if(resc->r.mode & R_FIELDS)
1093                 do_render_fields(resc);
1094         else if(resc->r.mode & R_MBLUR)
1095                 do_render_blurred(resc, resc->r.cfra);
1096         else
1097                 render_one_frame(resc);
1098
1099 }
1100
1101 static void ntree_render_scenes(Render *re)
1102 {
1103         bNode *node;
1104         int cfra= re->scene->r.cfra;
1105         
1106         if(re->scene->nodetree==NULL) return;
1107         
1108         /* check for render-result nodes using other scenes, we tag them LIB_DOIT */
1109         for(node= re->scene->nodetree->nodes.first; node; node= node->next) {
1110                 if(node->type==CMP_NODE_R_RESULT) {
1111                         if(node->id) {
1112                                 if(node->id != (ID *)re->scene)
1113                                         node->id->flag |= LIB_DOIT;
1114                                 else
1115                                         node->id->flag &= ~LIB_DOIT;
1116                         }
1117                 }
1118         }
1119         
1120         /* now foreach render-result node tagged we do a full render */
1121         /* results are stored in a way compisitor will find it */
1122         for(node= re->scene->nodetree->nodes.first; node; node= node->next) {
1123                 if(node->type==CMP_NODE_R_RESULT) {
1124                         if(node->id && node->id != (ID *)re->scene) {
1125                                 if(node->id->flag & LIB_DOIT) {
1126                                         do_render_scene_node(re, (Scene *)node->id, cfra);
1127                                         node->id->flag &= ~LIB_DOIT;
1128                                 }
1129                         }
1130                 }
1131         }
1132         
1133         /* still the global... */
1134         if(G.scene!=re->scene)
1135                 set_scene_bg(re->scene);
1136         
1137 }
1138
1139 /* helper call to detect if theres a render-result node */
1140 int composite_needs_render(Scene *sce)
1141 {
1142         bNodeTree *ntree= sce->nodetree;
1143         bNode *node;
1144         
1145         if(ntree==NULL) return 1;
1146         if(sce->use_nodes==0) return 1;
1147
1148         for(node= ntree->nodes.first; node; node= node->next) {
1149                 if(node->type==CMP_NODE_R_RESULT)
1150                         if(node->id==NULL || node->id!=&sce->id)
1151                                 return 1;
1152         }
1153         return 0;
1154 }
1155
1156 /* bad call... need to think over proper method still */
1157 static void render_composit_stats(char *str)
1158 {
1159         R.i.infostr= str;
1160         R.stats_draw(&R.i);
1161         R.i.infostr= NULL;
1162 }
1163
1164 static void do_render_final(Render *re)
1165 {
1166         /* we set start time here, for main Blender loops */
1167         re->i.starttime= PIL_check_seconds_timer();
1168
1169         if(re->r.scemode & R_DOSEQ) {
1170                 if (!re->result->rect32) {
1171                         re->result->rect32= MEM_callocT(sizeof(int)*re->rectx*re->recty, "do_render_final rectot");
1172                 }
1173                 if(!re->test_break()) 
1174                         do_render_seq(re->result, re->r.cfra);
1175         }
1176         else {
1177                 bNodeTree *ntree= re->scene->nodetree;
1178                 
1179                 if(composite_needs_render(re->scene)) {
1180                         /* save memory... free all cached images */
1181                         ntreeFreeCache(ntree);
1182                         
1183                         /* now use renderdata and camera to set viewplane */
1184                         RE_SetCamera(re, re->scene->camera);
1185                         
1186                         if(re->r.mode & R_FIELDS)
1187                                 do_render_fields(re);
1188                         else if(re->r.mode & R_MBLUR)
1189                                 do_render_blurred(re, re->scene->r.cfra);
1190                         else
1191                                 render_one_frame(re);
1192                 }
1193                 
1194                 if(!re->test_break() && ntree) {
1195                         ntreeCompositTagRender(ntree);
1196                         ntreeCompositTagAnimated(ntree);
1197                         
1198                         if(re->r.scemode & R_DOCOMP) {
1199                                 /* checks if there are render-result nodes that need scene */
1200                                 ntree_render_scenes(re);
1201                                 
1202                                 if(!re->test_break()) {
1203                                         ntree->stats_draw= render_composit_stats;
1204                                         /* in case it was never initialized */
1205                                         R.stats_draw= re->stats_draw;
1206                                         
1207                                         ntreeCompositExecTree(ntree, &re->r, G.background==0);
1208                                         ntree->stats_draw= NULL;
1209                                 }
1210                         }
1211                 }
1212         }
1213         
1214
1215         re->i.lastframetime= PIL_check_seconds_timer()- re->i.starttime;
1216         re->stats_draw(&re->i);
1217         
1218         re->display_draw(re->result, NULL);
1219 }
1220
1221
1222 static int is_rendering_allowed(Render *re)
1223 {
1224         
1225         /* forbidden combinations */
1226         if(re->r.mode & R_PANORAMA) {
1227                 if(re->r.mode & R_BORDER) {
1228                         re->error("No border supported for Panorama");
1229                         return 0;
1230                 }
1231                 if(re->r.yparts>1) {
1232                         re->error("No Y-Parts supported for Panorama");
1233                         return 0;
1234                 }
1235                 if(re->r.mode & R_ORTHO) {
1236                         re->error("No Ortho render possible for Panorama");
1237                         return 0;
1238                 }
1239         }
1240         
1241         if(re->r.mode & R_BORDER) {
1242                 if(re->r.border.xmax <= re->r.border.xmin || 
1243                    re->r.border.ymax <= re->r.border.ymin) {
1244                         re->error("No border area selected.");
1245                         return 0;
1246                 }
1247         }
1248         
1249         if(re->r.xparts*re->r.yparts>=2 && (re->r.mode & R_MOVIECROP) && (re->r.mode & R_BORDER)) {
1250                 re->error("Combination of border, crop and parts not allowed");
1251                 return 0;
1252         }
1253         
1254         if(re->r.yparts>1 && (re->r.mode & R_PANORAMA)) {
1255                 re->error("No Y-Parts supported for Panorama");
1256                 return 0;
1257         }
1258         
1259         /* check valid camera */
1260         if(re->scene->camera==NULL)
1261                 re->scene->camera= scene_find_camera(re->scene);
1262         if(re->scene->camera==NULL) {
1263                 re->error("No camera");
1264                 return 0;
1265         }
1266         
1267         
1268         return 1;
1269 }
1270
1271 /* evaluating scene options for general Blender render */
1272 static int render_initialize_from_scene(Render *re, Scene *scene)
1273 {
1274         int winx, winy;
1275         rcti disprect;
1276         
1277         /* r.xsch and r.ysch has the actual view window size
1278                 r.border is the clipping rect */
1279         
1280         /* calculate actual render result and display size */
1281         winx= (scene->r.size*scene->r.xsch)/100;
1282         winy= (scene->r.size*scene->r.ysch)/100;
1283         //      if(scene->r.mode & R_PANORAMA)
1284         //              winx*= scene->r.xparts;
1285         
1286         /* only in movie case we render smaller part */
1287         if(scene->r.mode & R_BORDER) {
1288                 disprect.xmin= scene->r.border.xmin*winx;
1289                 disprect.xmax= scene->r.border.xmax*winx;
1290                 
1291                 disprect.ymin= scene->r.border.ymin*winy;
1292                 disprect.ymax= scene->r.border.ymax*winy;
1293         }
1294         else {
1295                 disprect.xmin= disprect.ymin= 0;
1296                 disprect.xmax= winx;
1297                 disprect.ymax= winy;
1298         }
1299         
1300 /*      if(G.rt) re->flag |= R_FILEBUFFER; */
1301         RE_InitState(re, &scene->r, winx, winy, &disprect);
1302         re->flag &= ~R_FILEBUFFER;
1303         
1304         re->scene= scene;
1305         if(!is_rendering_allowed(re))
1306                 return 0;
1307         
1308         re->display_init(re->result);
1309         re->display_clear(re->result);
1310         
1311         return 1;
1312 }
1313
1314 /* general Blender frame render call */
1315 void RE_BlenderFrame(Render *re, Scene *scene, int frame)
1316 {
1317         /* ugly global still... is to prevent renderwin events and signal subsurfs etc to make full resol */
1318         /* is also set by caller renderwin.c */
1319         G.rendering= 1;
1320         
1321         if(render_initialize_from_scene(re, scene)) {
1322                 do_render_final(re);
1323                 
1324         }
1325 }
1326
1327 static void do_write_image_or_movie(Render *re, Scene *scene, bMovieHandle *mh)
1328 {
1329         char name[FILE_MAXDIR+FILE_MAXFILE];
1330         RenderResult rres;
1331         
1332         RE_GetResultImage(re, &rres);
1333
1334         /* write movie or image */
1335         if(BKE_imtype_is_movie(scene->r.imtype)) {
1336                 int dofree = 0;
1337                 /* note; the way it gets 32 bits rects is weak... */
1338                 if(rres.rect32==NULL) {
1339                         rres.rect32= MEM_mapallocT(sizeof(int)*rres.rectx*rres.recty, "temp 32 bits rect");
1340                         dofree = 1;
1341                 }
1342                 RE_ResultGet32(re, rres.rect32);
1343                 mh->append_movie(scene->r.cfra, rres.rect32, rres.rectx, rres.recty);
1344                 if(dofree) {
1345                         MEM_freeT(rres.rect32);
1346                 }
1347                 printf("Append frame %d", scene->r.cfra);
1348         } else {
1349                 ImBuf *ibuf= IMB_allocImBuf(rres.rectx, rres.recty, scene->r.planes, 0, 0);
1350                 int ok;
1351                 
1352                 BKE_makepicstring(name, (scene->r.cfra));
1353
1354                 /* if not exists, BKE_write_ibuf makes one */
1355                 ibuf->rect= rres.rect32;    
1356                 ibuf->rect_float= rres.rectf;
1357                 ibuf->zbuf_float= rres.rectz;
1358                 
1359                 /* float factor for random dither, imbuf takes care of it */
1360                 ibuf->dither= scene->r.dither_intensity;
1361
1362                 ok= BKE_write_ibuf(ibuf, name, scene->r.imtype, scene->r.subimtype, scene->r.quality);
1363                 
1364                 if(ok==0) {
1365                         printf("Render error: cannot save %s\n", name);
1366                         G.afbreek=1;
1367                         return;
1368                 }
1369                 else printf("Saved: %s", name);
1370                 
1371                 /* optional preview images for exr */
1372                 if(ok && scene->r.imtype==R_OPENEXR && (scene->r.subimtype & R_PREVIEW_JPG)) {
1373                         if(BLI_testextensie(name, ".exr")) 
1374                                 name[strlen(name)-4]= 0;
1375                         BKE_add_image_extension(name, R_JPEG90);
1376                         ibuf->depth= 24; 
1377                         BKE_write_ibuf(ibuf, name, R_JPEG90, scene->r.subimtype, scene->r.quality);
1378                         printf("Saved: %s", name);
1379                 }
1380                 
1381                 /* imbuf knows which rects are not part of ibuf */
1382                 IMB_freeImBuf(ibuf);    
1383         }
1384         
1385         BLI_timestr(re->i.lastframetime, name);
1386         printf(" Time: %s\n", name);
1387         fflush(stdout); /* needed for renderd !! (not anymore... (ton)) */
1388 }
1389
1390 /* saves images to disk */
1391 void RE_BlenderAnim(Render *re, Scene *scene, int sfra, int efra)
1392 {
1393         bMovieHandle *mh= BKE_get_movie_handle(scene->r.imtype);
1394         int cfrao= scene->r.cfra;
1395         
1396         /* ugly global still... is to prevent renderwin events and signal subsurfs etc to make full resol */
1397         /* is also set by caller renderwin.c */
1398         G.rendering= 1;
1399         
1400         if(!render_initialize_from_scene(re, scene))
1401            return;
1402         
1403         /* confusing... scene->r or re->r? make a decision once! */
1404         if(BKE_imtype_is_movie(scene->r.imtype))
1405                 mh->start_movie(&scene->r, re->rectx, re->recty);
1406
1407         if (mh->get_next_frame) {
1408                 while (!(G.afbreek == 1)) {
1409                         int nf = mh->get_next_frame();
1410                         if (nf >= 0 && nf >= scene->r.sfra && nf <= scene->r.efra) {
1411                                 scene->r.cfra = nf;
1412                                 re->r.cfra= scene->r.cfra; /* weak.... */
1413                 
1414                                 do_render_final(re);
1415
1416                                 if(re->test_break() == 0) {
1417                                         do_write_image_or_movie(re, scene, mh);
1418                                 }
1419                         }
1420                 }
1421         } else {
1422                 for(scene->r.cfra= sfra; 
1423                     scene->r.cfra<=efra; scene->r.cfra++) {
1424                         re->r.cfra= scene->r.cfra;         /* weak.... */
1425                 
1426                         do_render_final(re);
1427
1428                         if(re->test_break() == 0) {
1429                                 do_write_image_or_movie(re, scene, mh);
1430                         }
1431                 
1432                         if(G.afbreek==1) break;
1433                 }
1434         }
1435         
1436         /* end movie */
1437         if(BKE_imtype_is_movie(scene->r.imtype))
1438                 mh->end_movie();
1439
1440         scene->r.cfra= cfrao;
1441 }
1442
1443
1444