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