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