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