Merged changes in the trunk up to revision 41768.
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 /** \file blender/render/intern/source/pipeline.c
30  *  \ingroup render
31  */
32
33
34 #include <math.h>
35 #include <limits.h>
36 #include <string.h>
37 #include <stdlib.h>
38 #include <stddef.h>
39
40 #include "DNA_group_types.h"
41 #include "DNA_image_types.h"
42 #include "DNA_node_types.h"
43 #include "DNA_object_types.h"
44 #include "DNA_scene_types.h"
45 #include "DNA_sequence_types.h"
46 #include "DNA_userdef_types.h"
47
48 #include "MEM_guardedalloc.h"
49
50 #include "BKE_animsys.h"        /* <------ should this be here?, needed for sequencer update */
51 #include "BKE_camera.h"
52 #include "BKE_global.h"
53 #include "BKE_image.h"
54 #include "BKE_library.h"
55 #include "BKE_main.h"
56 #include "BKE_node.h"
57 #include "BKE_object.h"
58 #include "BKE_pointcache.h"
59 #include "BKE_report.h"
60 #include "BKE_scene.h"
61 #include "BKE_sequencer.h"
62 #include "BKE_utildefines.h"
63 #include "BKE_writeavi.h"       /* <------ should be replaced once with generic movie module */
64
65 #include "BLI_math.h"
66 #include "BLI_blenlib.h"
67 #include "BLI_rand.h"
68 #include "BLI_threads.h"
69 #include "BLI_callbacks.h"
70 #include "BLI_utildefines.h"
71
72 #include "PIL_time.h"
73 #include "IMB_imbuf.h"
74 #include "IMB_imbuf_types.h"
75
76 #include "intern/openexr/openexr_multi.h"
77
78 #include "RE_engine.h"
79 #include "RE_pipeline.h"
80
81 #include "FRS_freestyle.h"
82 #include "FRS_freestyle_config.h"
83
84 /* internal */
85 #include "render_types.h"
86 #include "renderpipeline.h"
87 #include "renderdatabase.h"
88 #include "rendercore.h"
89 #include "envmap.h"
90 #include "initrender.h"
91 #include "shadbuf.h"
92 #include "pixelblending.h"
93 #include "zbuf.h"
94
95
96 /* render flow
97
98 1) Initialize state
99 - state data, tables
100 - movie/image file init
101 - everything that doesn't change during animation
102
103 2) Initialize data
104 - camera, world, matrices
105 - make render verts, faces, halos, strands
106 - everything can change per frame/field
107
108 3) Render Processor
109 - multiple layers
110 - tiles, rect, baking
111 - layers/tiles optionally to disk or directly in Render Result
112
113 4) Composite Render Result
114 - also read external files etc
115
116 5) Image Files
117 - save file or append in movie
118
119 */
120
121
122 /* ********* globals ******** */
123
124 /* here we store all renders */
125 static struct {
126         ListBase renderlist;
127
128         /* commandline thread override */
129         int threads;
130 } RenderGlobal = {{NULL, NULL}, -1}; 
131
132 /* hardcopy of current render, used while rendering for speed */
133 Render R;
134
135 /* ********* alloc and free ******** */
136
137 static int do_write_image_or_movie(Render *re, Scene *scene, bMovieHandle *mh, const char *name_override);
138
139 static volatile int g_break= 0;
140 static int thread_break(void *UNUSED(arg))
141 {
142         return g_break;
143 }
144
145 /* default callbacks, set in each new render */
146 static void result_nothing(void *UNUSED(arg), RenderResult *UNUSED(rr)) {}
147 static void result_rcti_nothing(void *UNUSED(arg), RenderResult *UNUSED(rr), volatile struct rcti *UNUSED(rect)) {}
148 static void stats_nothing(void *UNUSED(arg), RenderStats *UNUSED(rs)) {}
149 static void float_nothing(void *UNUSED(arg), float UNUSED(val)) {}
150 static int default_break(void *UNUSED(arg)) {return G.afbreek == 1;}
151
152 static void stats_background(void *UNUSED(arg), RenderStats *rs)
153 {
154         uintptr_t mem_in_use, mmap_in_use, peak_memory;
155         float megs_used_memory, mmap_used_memory, megs_peak_memory;
156
157         mem_in_use= MEM_get_memory_in_use();
158         mmap_in_use= MEM_get_mapped_memory_in_use();
159         peak_memory = MEM_get_peak_memory();
160
161         megs_used_memory= (mem_in_use-mmap_in_use)/(1024.0*1024.0);
162         mmap_used_memory= (mmap_in_use)/(1024.0*1024.0);
163         megs_peak_memory = (peak_memory)/(1024.0*1024.0);
164
165         fprintf(stdout, "Fra:%d Mem:%.2fM (%.2fM, peak %.2fM) ", rs->cfra,
166                 megs_used_memory, mmap_used_memory, megs_peak_memory);
167
168         if(rs->curfield)
169                 fprintf(stdout, "Field %d ", rs->curfield);
170         if(rs->curblur)
171                 fprintf(stdout, "Blur %d ", rs->curblur);
172
173         if(rs->infostr) {
174                 fprintf(stdout, "| %s", rs->infostr);
175         }
176         else {
177                 if(rs->tothalo)
178                         fprintf(stdout, "Sce: %s Ve:%d Fa:%d Ha:%d La:%d", rs->scenename, rs->totvert, rs->totface, rs->tothalo, rs->totlamp);
179                 else
180                         fprintf(stdout, "Sce: %s Ve:%d Fa:%d La:%d", rs->scenename, rs->totvert, rs->totface, rs->totlamp);
181         }
182
183         BLI_exec_cb(G.main, NULL, BLI_CB_EVT_RENDER_STATS);
184
185         fputc('\n', stdout);
186         fflush(stdout);
187 }
188
189 void RE_FreeRenderResult(RenderResult *res)
190 {
191         if(res==NULL) return;
192
193         while(res->layers.first) {
194                 RenderLayer *rl= res->layers.first;
195                 
196                 if(rl->rectf) MEM_freeN(rl->rectf);
197                 /* acolrect and scolrect are optionally allocated in shade_tile, only free here since it can be used for drawing */
198                 if(rl->acolrect) MEM_freeN(rl->acolrect);
199                 if(rl->scolrect) MEM_freeN(rl->scolrect);
200                 
201                 while(rl->passes.first) {
202                         RenderPass *rpass= rl->passes.first;
203                         if(rpass->rect) MEM_freeN(rpass->rect);
204                         BLI_remlink(&rl->passes, rpass);
205                         MEM_freeN(rpass);
206                 }
207                 BLI_remlink(&res->layers, rl);
208                 MEM_freeN(rl);
209         }
210         
211         if(res->rect32)
212                 MEM_freeN(res->rect32);
213         if(res->rectz)
214                 MEM_freeN(res->rectz);
215         if(res->rectf)
216                 MEM_freeN(res->rectf);
217         if(res->text)
218                 MEM_freeN(res->text);
219         
220         MEM_freeN(res);
221 }
222
223 /* version that's compatible with fullsample buffers */
224 void free_render_result(ListBase *lb, RenderResult *rr)
225 {
226         RenderResult *rrnext;
227         
228         for(; rr; rr= rrnext) {
229                 rrnext= rr->next;
230                 
231                 if(lb && lb->first)
232                         BLI_remlink(lb, rr);
233                 
234                 RE_FreeRenderResult(rr);
235         }
236 }
237
238
239 /* all layers except the active one get temporally pushed away */
240 static void push_render_result(Render *re)
241 {
242         BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
243
244         /* officially pushed result should be NULL... error can happen with do_seq */
245         RE_FreeRenderResult(re->pushedresult);
246         
247         re->pushedresult= re->result;
248         re->result= NULL;
249
250         BLI_rw_mutex_unlock(&re->resultmutex);
251 }
252
253 /* if scemode is R_SINGLE_LAYER, at end of rendering, merge the both render results */
254 static void pop_render_result(Render *re)
255 {
256         if(re->result==NULL) {
257                 printf("pop render result error; no current result!\n");
258                 return;
259         }
260
261         if(re->pushedresult) {
262                 BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
263
264                 if(re->pushedresult->rectx==re->result->rectx && re->pushedresult->recty==re->result->recty) {
265                         /* find which layer in pushedresult should be replaced */
266                         SceneRenderLayer *srl;
267                         RenderLayer *rlpush;
268                         RenderLayer *rl= re->result->layers.first;
269                         int nr;
270                         
271                         /* render result should be empty after this */
272                         BLI_remlink(&re->result->layers, rl);
273                         
274                         /* reconstruct render result layers */
275                         for(nr=0, srl= re->scene->r.layers.first; srl; srl= srl->next, nr++) {
276                                 if(nr==re->r.actlay)
277                                         BLI_addtail(&re->result->layers, rl);
278                                 else {
279                                         rlpush= RE_GetRenderLayer(re->pushedresult, srl->name);
280                                         if(rlpush) {
281                                                 BLI_remlink(&re->pushedresult->layers, rlpush);
282                                                 BLI_addtail(&re->result->layers, rlpush);
283                                         }
284                                 }
285                         }
286                 }
287                 
288                 RE_FreeRenderResult(re->pushedresult);
289                 re->pushedresult= NULL;
290
291                 BLI_rw_mutex_unlock(&re->resultmutex);
292         }
293 }
294
295 /* NOTE: OpenEXR only supports 32 chars for layer+pass names
296    In blender we now use max 10 chars for pass, max 20 for layer */
297 static const char *get_pass_name(int passtype, int channel)
298 {
299         
300         if(passtype == SCE_PASS_COMBINED) {
301                 if(channel==-1) return "Combined";
302                 if(channel==0) return "Combined.R";
303                 if(channel==1) return "Combined.G";
304                 if(channel==2) return "Combined.B";
305                 return "Combined.A";
306         }
307         if(passtype == SCE_PASS_Z) {
308                 if(channel==-1) return "Depth";
309                 return "Depth.Z";
310         }
311         if(passtype == SCE_PASS_VECTOR) {
312                 if(channel==-1) return "Vector";
313                 if(channel==0) return "Vector.X";
314                 if(channel==1) return "Vector.Y";
315                 if(channel==2) return "Vector.Z";
316                 return "Vector.W";
317         }
318         if(passtype == SCE_PASS_NORMAL) {
319                 if(channel==-1) return "Normal";
320                 if(channel==0) return "Normal.X";
321                 if(channel==1) return "Normal.Y";
322                 return "Normal.Z";
323         }
324         if(passtype == SCE_PASS_UV) {
325                 if(channel==-1) return "UV";
326                 if(channel==0) return "UV.U";
327                 if(channel==1) return "UV.V";
328                 return "UV.A";
329         }
330         if(passtype == SCE_PASS_RGBA) {
331                 if(channel==-1) return "Color";
332                 if(channel==0) return "Color.R";
333                 if(channel==1) return "Color.G";
334                 if(channel==2) return "Color.B";
335                 return "Color.A";
336         }
337         if(passtype == SCE_PASS_EMIT) {
338                 if(channel==-1) return "Emit";
339                 if(channel==0) return "Emit.R";
340                 if(channel==1) return "Emit.G";
341                 return "Emit.B";
342         }
343         if(passtype == SCE_PASS_DIFFUSE) {
344                 if(channel==-1) return "Diffuse";
345                 if(channel==0) return "Diffuse.R";
346                 if(channel==1) return "Diffuse.G";
347                 return "Diffuse.B";
348         }
349         if(passtype == SCE_PASS_SPEC) {
350                 if(channel==-1) return "Spec";
351                 if(channel==0) return "Spec.R";
352                 if(channel==1) return "Spec.G";
353                 return "Spec.B";
354         }
355         if(passtype == SCE_PASS_SHADOW) {
356                 if(channel==-1) return "Shadow";
357                 if(channel==0) return "Shadow.R";
358                 if(channel==1) return "Shadow.G";
359                 return "Shadow.B";
360         }
361         if(passtype == SCE_PASS_AO) {
362                 if(channel==-1) return "AO";
363                 if(channel==0) return "AO.R";
364                 if(channel==1) return "AO.G";
365                 return "AO.B";
366         }
367         if(passtype == SCE_PASS_ENVIRONMENT) {
368                 if(channel==-1) return "Env";
369                 if(channel==0) return "Env.R";
370                 if(channel==1) return "Env.G";
371                 return "Env.B";
372         }
373         if(passtype == SCE_PASS_INDIRECT) {
374                 if(channel==-1) return "Indirect";
375                 if(channel==0) return "Indirect.R";
376                 if(channel==1) return "Indirect.G";
377                 return "Indirect.B";
378         }
379         if(passtype == SCE_PASS_REFLECT) {
380                 if(channel==-1) return "Reflect";
381                 if(channel==0) return "Reflect.R";
382                 if(channel==1) return "Reflect.G";
383                 return "Reflect.B";
384         }
385         if(passtype == SCE_PASS_REFRACT) {
386                 if(channel==-1) return "Refract";
387                 if(channel==0) return "Refract.R";
388                 if(channel==1) return "Refract.G";
389                 return "Refract.B";
390         }
391         if(passtype == SCE_PASS_INDEXOB) {
392                 if(channel==-1) return "IndexOB";
393                 return "IndexOB.X";
394         }
395         if(passtype == SCE_PASS_INDEXMA) {
396                 if(channel==-1) return "IndexMA";
397                 return "IndexMA.X";
398         }
399         if(passtype == SCE_PASS_MIST) {
400                 if(channel==-1) return "Mist";
401                 return "Mist.Z";
402         }
403         if(passtype == SCE_PASS_RAYHITS)
404         {
405                 if(channel==-1) return "Rayhits";
406                 if(channel==0) return "Rayhits.R";
407                 if(channel==1) return "Rayhits.G";
408                 return "Rayhits.B";
409         }
410         return "Unknown";
411 }
412
413 static int passtype_from_name(const char *str)
414 {
415         
416         if(strcmp(str, "Combined")==0)
417                 return SCE_PASS_COMBINED;
418
419         if(strcmp(str, "Depth")==0)
420                 return SCE_PASS_Z;
421
422         if(strcmp(str, "Vector")==0)
423                 return SCE_PASS_VECTOR;
424
425         if(strcmp(str, "Normal")==0)
426                 return SCE_PASS_NORMAL;
427
428         if(strcmp(str, "UV")==0)
429                 return SCE_PASS_UV;
430
431         if(strcmp(str, "Color")==0)
432                 return SCE_PASS_RGBA;
433
434         if(strcmp(str, "Emit")==0)
435                 return SCE_PASS_EMIT;
436
437         if(strcmp(str, "Diffuse")==0)
438                 return SCE_PASS_DIFFUSE;
439
440         if(strcmp(str, "Spec")==0)
441                 return SCE_PASS_SPEC;
442
443         if(strcmp(str, "Shadow")==0)
444                 return SCE_PASS_SHADOW;
445         
446         if(strcmp(str, "AO")==0)
447                 return SCE_PASS_AO;
448
449         if(strcmp(str, "Env")==0)
450                 return SCE_PASS_ENVIRONMENT;
451
452         if(strcmp(str, "Indirect")==0)
453                 return SCE_PASS_INDIRECT;
454
455         if(strcmp(str, "Reflect")==0)
456                 return SCE_PASS_REFLECT;
457
458         if(strcmp(str, "Refract")==0)
459                 return SCE_PASS_REFRACT;
460
461         if(strcmp(str, "IndexOB")==0)
462                 return SCE_PASS_INDEXOB;
463
464         if(strcmp(str, "IndexMA")==0)
465                 return SCE_PASS_INDEXMA;
466
467         if(strcmp(str, "Mist")==0)
468                 return SCE_PASS_MIST;
469         
470         if(strcmp(str, "RayHits")==0)
471                 return SCE_PASS_RAYHITS;
472         return 0;
473 }
474
475 static void scene_unique_exr_name(Scene *scene, char *str, int sample)
476 {
477         char di[FILE_MAX], name[FILE_MAXFILE+MAX_ID_NAME+100], fi[FILE_MAXFILE];
478         
479         BLI_strncpy(di, G.main->name, FILE_MAX);
480         BLI_splitdirstring(di, fi);
481         
482         if(sample==0)
483                 BLI_snprintf(name, sizeof(name), "%s_%s.exr", fi, scene->id.name+2);
484         else
485                 BLI_snprintf(name, sizeof(name), "%s_%s%d.exr", fi, scene->id.name+2, sample);
486
487         BLI_make_file_string("/", str, BLI_temporary_dir(), name);
488 }
489
490 static void render_unique_exr_name(Render *re, char *str, int sample)
491 {
492         scene_unique_exr_name(re->scene, str, sample);
493 }
494
495 static void render_layer_add_pass(RenderResult *rr, RenderLayer *rl, int channels, int passtype)
496 {
497         const char *typestr= get_pass_name(passtype, 0);
498         RenderPass *rpass= MEM_callocN(sizeof(RenderPass), typestr);
499         int rectsize= rr->rectx*rr->recty*channels;
500         
501         BLI_addtail(&rl->passes, rpass);
502         rpass->passtype= passtype;
503         rpass->channels= channels;
504         rpass->rectx= rl->rectx;
505         rpass->recty= rl->recty;
506         
507         if(rr->exrhandle) {
508                 int a;
509                 for(a=0; a<channels; a++)
510                         IMB_exr_add_channel(rr->exrhandle, rl->name, get_pass_name(passtype, a), 0, 0, NULL);
511         }
512         else {
513                 float *rect;
514                 int x;
515                 
516                 rpass->rect= MEM_mapallocN(sizeof(float)*rectsize, typestr);
517                 
518                 if(passtype==SCE_PASS_VECTOR) {
519                         /* initialize to max speed */
520                         rect= rpass->rect;
521                         for(x= rectsize-1; x>=0; x--)
522                                 rect[x]= PASS_VECTOR_MAX;
523                 }
524                 else if(passtype==SCE_PASS_Z) {
525                         rect= rpass->rect;
526                         for(x= rectsize-1; x>=0; x--)
527                                 rect[x]= 10e10;
528                 }
529         }
530 }
531
532 float *RE_RenderLayerGetPass(RenderLayer *rl, int passtype)
533 {
534         RenderPass *rpass;
535         
536         for(rpass=rl->passes.first; rpass; rpass= rpass->next)
537                 if(rpass->passtype== passtype)
538                         return rpass->rect;
539         return NULL;
540 }
541
542 RenderLayer *RE_GetRenderLayer(RenderResult *rr, const char *name)
543 {
544         RenderLayer *rl;
545         
546         if(rr==NULL) return NULL;
547         
548         for(rl= rr->layers.first; rl; rl= rl->next)
549                 if(strncmp(rl->name, name, RE_MAXNAME)==0)
550                         return rl;
551         return NULL;
552 }
553
554 /* called by main render as well for parts */
555 /* will read info from Render *re to define layers */
556 /* called in threads */
557 /* re->winx,winy is coordinate space of entire image, partrct the part within */
558 RenderResult *new_render_result(Render *re, rcti *partrct, int crop, int savebuffers)
559 {
560         RenderResult *rr;
561         RenderLayer *rl;
562         SceneRenderLayer *srl;
563         int rectx, recty, nr;
564         
565         rectx= partrct->xmax - partrct->xmin;
566         recty= partrct->ymax - partrct->ymin;
567         
568         if(rectx<=0 || recty<=0)
569                 return NULL;
570         
571         rr= MEM_callocN(sizeof(RenderResult), "new render result");
572         rr->rectx= rectx;
573         rr->recty= recty;
574         rr->renrect.xmin= 0; rr->renrect.xmax= rectx-2*crop;
575         /* crop is one or two extra pixels rendered for filtering, is used for merging and display too */
576         rr->crop= crop;
577         
578         /* tilerect is relative coordinates within render disprect. do not subtract crop yet */
579         rr->tilerect.xmin= partrct->xmin - re->disprect.xmin;
580         rr->tilerect.xmax= partrct->xmax - re->disprect.xmax;
581         rr->tilerect.ymin= partrct->ymin - re->disprect.ymin;
582         rr->tilerect.ymax= partrct->ymax - re->disprect.ymax;
583         
584         if(savebuffers) {
585                 rr->exrhandle= IMB_exr_get_handle();
586         }
587         
588         /* check renderdata for amount of layers */
589         for(nr=0, srl= re->r.layers.first; srl; srl= srl->next, nr++) {
590                 
591                 if((re->r.scemode & R_SINGLE_LAYER) && nr!=re->r.actlay)
592                         continue;
593                 if(srl->layflag & SCE_LAY_DISABLE)
594                         continue;
595                 
596                 rl= MEM_callocN(sizeof(RenderLayer), "new render layer");
597                 BLI_addtail(&rr->layers, rl);
598                 
599                 BLI_strncpy(rl->name, srl->name, sizeof(rl->name));
600                 rl->lay= srl->lay;
601                 rl->lay_zmask= srl->lay_zmask;
602                 rl->layflag= srl->layflag;
603                 rl->passflag= srl->passflag; // for debugging: srl->passflag|SCE_PASS_RAYHITS;
604                 rl->pass_xor= srl->pass_xor;
605                 rl->light_override= srl->light_override;
606                 rl->mat_override= srl->mat_override;
607                 rl->rectx= rectx;
608                 rl->recty= recty;
609                 
610                 if(rr->exrhandle) {
611                         IMB_exr_add_channel(rr->exrhandle, rl->name, "Combined.R", 0, 0, NULL);
612                         IMB_exr_add_channel(rr->exrhandle, rl->name, "Combined.G", 0, 0, NULL);
613                         IMB_exr_add_channel(rr->exrhandle, rl->name, "Combined.B", 0, 0, NULL);
614                         IMB_exr_add_channel(rr->exrhandle, rl->name, "Combined.A", 0, 0, NULL);
615                 }
616                 else
617                         rl->rectf= MEM_mapallocN(rectx*recty*sizeof(float)*4, "Combined rgba");
618                 
619                 if(srl->passflag  & SCE_PASS_Z)
620                         render_layer_add_pass(rr, rl, 1, SCE_PASS_Z);
621                 if(srl->passflag  & SCE_PASS_VECTOR)
622                         render_layer_add_pass(rr, rl, 4, SCE_PASS_VECTOR);
623                 if(srl->passflag  & SCE_PASS_NORMAL)
624                         render_layer_add_pass(rr, rl, 3, SCE_PASS_NORMAL);
625                 if(srl->passflag  & SCE_PASS_UV) 
626                         render_layer_add_pass(rr, rl, 3, SCE_PASS_UV);
627                 if(srl->passflag  & SCE_PASS_RGBA)
628                         render_layer_add_pass(rr, rl, 4, SCE_PASS_RGBA);
629                 if(srl->passflag  & SCE_PASS_EMIT)
630                         render_layer_add_pass(rr, rl, 3, SCE_PASS_EMIT);
631                 if(srl->passflag  & SCE_PASS_DIFFUSE)
632                         render_layer_add_pass(rr, rl, 3, SCE_PASS_DIFFUSE);
633                 if(srl->passflag  & SCE_PASS_SPEC)
634                         render_layer_add_pass(rr, rl, 3, SCE_PASS_SPEC);
635                 if(srl->passflag  & SCE_PASS_AO)
636                         render_layer_add_pass(rr, rl, 3, SCE_PASS_AO);
637                 if(srl->passflag  & SCE_PASS_ENVIRONMENT)
638                         render_layer_add_pass(rr, rl, 3, SCE_PASS_ENVIRONMENT);
639                 if(srl->passflag  & SCE_PASS_INDIRECT)
640                         render_layer_add_pass(rr, rl, 3, SCE_PASS_INDIRECT);
641                 if(srl->passflag  & SCE_PASS_SHADOW)
642                         render_layer_add_pass(rr, rl, 3, SCE_PASS_SHADOW);
643                 if(srl->passflag  & SCE_PASS_REFLECT)
644                         render_layer_add_pass(rr, rl, 3, SCE_PASS_REFLECT);
645                 if(srl->passflag  & SCE_PASS_REFRACT)
646                         render_layer_add_pass(rr, rl, 3, SCE_PASS_REFRACT);
647                 if(srl->passflag  & SCE_PASS_INDEXOB)
648                         render_layer_add_pass(rr, rl, 1, SCE_PASS_INDEXOB);
649                 if(srl->passflag  & SCE_PASS_INDEXMA)
650                         render_layer_add_pass(rr, rl, 1, SCE_PASS_INDEXMA);
651                 if(srl->passflag  & SCE_PASS_MIST)
652                         render_layer_add_pass(rr, rl, 1, SCE_PASS_MIST);
653                 if(rl->passflag & SCE_PASS_RAYHITS)
654                         render_layer_add_pass(rr, rl, 4, SCE_PASS_RAYHITS);
655                 
656         }
657         /* sss, previewrender and envmap don't do layers, so we make a default one */
658         if(rr->layers.first==NULL) {
659                 rl= MEM_callocN(sizeof(RenderLayer), "new render layer");
660                 BLI_addtail(&rr->layers, rl);
661                 
662                 rl->rectx= rectx;
663                 rl->recty= recty;
664
665                 /* duplicate code... */
666                 if(rr->exrhandle) {
667                         IMB_exr_add_channel(rr->exrhandle, rl->name, "Combined.R", 0, 0, NULL);
668                         IMB_exr_add_channel(rr->exrhandle, rl->name, "Combined.G", 0, 0, NULL);
669                         IMB_exr_add_channel(rr->exrhandle, rl->name, "Combined.B", 0, 0, NULL);
670                         IMB_exr_add_channel(rr->exrhandle, rl->name, "Combined.A", 0, 0, NULL);
671                 }
672                 else
673                         rl->rectf= MEM_mapallocN(rectx*recty*sizeof(float)*4, "Combined rgba");
674                 
675                 /* note, this has to be in sync with scene.c */
676                 rl->lay= (1<<20) -1;
677                 rl->layflag= 0x7FFF;    /* solid ztra halo strand */
678                 rl->passflag= SCE_PASS_COMBINED;
679                 FRS_add_freestyle_config( srl );
680                 
681                 re->r.actlay= 0;
682         }
683         
684         /* border render; calculate offset for use in compositor. compo is centralized coords */
685         rr->xof= re->disprect.xmin + (re->disprect.xmax - re->disprect.xmin)/2 - re->winx/2;
686         rr->yof= re->disprect.ymin + (re->disprect.ymax - re->disprect.ymin)/2 - re->winy/2;
687         
688         return rr;
689 }
690
691 static int render_scene_needs_vector(Render *re)
692 {
693         SceneRenderLayer *srl;
694         
695         for(srl= re->scene->r.layers.first; srl; srl= srl->next)
696                 if(!(srl->layflag & SCE_LAY_DISABLE))
697                         if(srl->passflag & SCE_PASS_VECTOR)
698                                 return 1;
699
700         return 0;
701 }
702
703 static void do_merge_tile(RenderResult *rr, RenderResult *rrpart, float *target, float *tile, int pixsize)
704 {
705         int y, ofs, copylen, tilex, tiley;
706         
707         copylen= tilex= rrpart->rectx;
708         tiley= rrpart->recty;
709         
710         if(rrpart->crop) {      /* filters add pixel extra */
711                 tile+= pixsize*(rrpart->crop + rrpart->crop*tilex);
712                 
713                 copylen= tilex - 2*rrpart->crop;
714                 tiley -= 2*rrpart->crop;
715                 
716                 ofs= (rrpart->tilerect.ymin + rrpart->crop)*rr->rectx + (rrpart->tilerect.xmin+rrpart->crop);
717                 target+= pixsize*ofs;
718         }
719         else {
720                 ofs= (rrpart->tilerect.ymin*rr->rectx + rrpart->tilerect.xmin);
721                 target+= pixsize*ofs;
722         }
723
724         copylen *= sizeof(float)*pixsize;
725         tilex *= pixsize;
726         ofs= pixsize*rr->rectx;
727
728         for(y=0; y<tiley; y++) {
729                 memcpy(target, tile, copylen);
730                 target+= ofs;
731                 tile+= tilex;
732         }
733 }
734
735 /* used when rendering to a full buffer, or when reading the exr part-layer-pass file */
736 /* no test happens here if it fits... we also assume layers are in sync */
737 /* is used within threads */
738 void merge_render_result(RenderResult *rr, RenderResult *rrpart)
739 {
740         RenderLayer *rl, *rlp;
741         RenderPass *rpass, *rpassp;
742         
743         for(rl= rr->layers.first, rlp= rrpart->layers.first; rl && rlp; rl= rl->next, rlp= rlp->next) {
744                 
745                 /* combined */
746                 if(rl->rectf && rlp->rectf)
747                         do_merge_tile(rr, rrpart, rl->rectf, rlp->rectf, 4);
748                 
749                 /* passes are allocated in sync */
750                 for(rpass= rl->passes.first, rpassp= rlp->passes.first; rpass && rpassp; rpass= rpass->next, rpassp= rpassp->next) {
751                         do_merge_tile(rr, rrpart, rpass->rect, rpassp->rect, rpass->channels);
752                 }
753         }
754 }
755
756
757 static void save_render_result_tile(RenderResult *rr, RenderResult *rrpart)
758 {
759         RenderLayer *rlp;
760         RenderPass *rpassp;
761         int offs, partx, party;
762         
763         BLI_lock_thread(LOCK_IMAGE);
764         
765         for(rlp= rrpart->layers.first; rlp; rlp= rlp->next) {
766                 
767                 if(rrpart->crop) {      /* filters add pixel extra */
768                         offs= (rrpart->crop + rrpart->crop*rrpart->rectx);
769                 }
770                 else {
771                         offs= 0;
772                 }
773                 
774                 /* combined */
775                 if(rlp->rectf) {
776                         int a, xstride= 4;
777                         for(a=0; a<xstride; a++)
778                                 IMB_exr_set_channel(rr->exrhandle, rlp->name, get_pass_name(SCE_PASS_COMBINED, a), 
779                                                                 xstride, xstride*rrpart->rectx, rlp->rectf+a + xstride*offs);
780                 }
781                 
782                 /* passes are allocated in sync */
783                 for(rpassp= rlp->passes.first; rpassp; rpassp= rpassp->next) {
784                         int a, xstride= rpassp->channels;
785                         for(a=0; a<xstride; a++)
786                                 IMB_exr_set_channel(rr->exrhandle, rlp->name, get_pass_name(rpassp->passtype, a), 
787                                                                         xstride, xstride*rrpart->rectx, rpassp->rect+a + xstride*offs);
788                 }
789                 
790         }
791
792         party= rrpart->tilerect.ymin + rrpart->crop;
793         partx= rrpart->tilerect.xmin + rrpart->crop;
794         IMB_exrtile_write_channels(rr->exrhandle, partx, party, 0);
795
796         BLI_unlock_thread(LOCK_IMAGE);
797
798 }
799
800 static void save_empty_result_tiles(Render *re)
801 {
802         RenderPart *pa;
803         RenderResult *rr;
804         
805         for(rr= re->result; rr; rr= rr->next) {
806                 IMB_exrtile_clear_channels(rr->exrhandle);
807                 
808                 for(pa= re->parts.first; pa; pa= pa->next) {
809                         if(pa->ready==0) {
810                                 int party= pa->disprect.ymin - re->disprect.ymin + pa->crop;
811                                 int partx= pa->disprect.xmin - re->disprect.xmin + pa->crop;
812                                 IMB_exrtile_write_channels(rr->exrhandle, partx, party, 0);
813                         }
814                 }
815         }
816 }
817
818
819 /* for passes read from files, these have names stored */
820 static char *make_pass_name(RenderPass *rpass, int chan)
821 {
822         static char name[16];
823         int len;
824         
825         BLI_strncpy(name, rpass->name, EXR_PASS_MAXNAME);
826         len= strlen(name);
827         name[len]= '.';
828         name[len+1]= rpass->chan_id[chan];
829         name[len+2]= 0;
830
831         return name;
832 }
833
834 /* filename already made absolute */
835 /* called from within UI, saves both rendered result as a file-read result */
836 int RE_WriteRenderResult(ReportList *reports, RenderResult *rr, const char *filename, int compress)
837 {
838         RenderLayer *rl;
839         RenderPass *rpass;
840         void *exrhandle= IMB_exr_get_handle();
841         int success;
842
843         BLI_make_existing_file(filename);
844         
845         /* composite result */
846         if(rr->rectf) {
847                 IMB_exr_add_channel(exrhandle, "Composite", "Combined.R", 4, 4*rr->rectx, rr->rectf);
848                 IMB_exr_add_channel(exrhandle, "Composite", "Combined.G", 4, 4*rr->rectx, rr->rectf+1);
849                 IMB_exr_add_channel(exrhandle, "Composite", "Combined.B", 4, 4*rr->rectx, rr->rectf+2);
850                 IMB_exr_add_channel(exrhandle, "Composite", "Combined.A", 4, 4*rr->rectx, rr->rectf+3);
851         }
852         
853         /* add layers/passes and assign channels */
854         for(rl= rr->layers.first; rl; rl= rl->next) {
855                 
856                 /* combined */
857                 if(rl->rectf) {
858                         int a, xstride= 4;
859                         for(a=0; a<xstride; a++)
860                                 IMB_exr_add_channel(exrhandle, rl->name, get_pass_name(SCE_PASS_COMBINED, a), 
861                                                                         xstride, xstride*rr->rectx, rl->rectf+a);
862                 }
863                 
864                 /* passes are allocated in sync */
865                 for(rpass= rl->passes.first; rpass; rpass= rpass->next) {
866                         int a, xstride= rpass->channels;
867                         for(a=0; a<xstride; a++) {
868                                 if(rpass->passtype)
869                                         IMB_exr_add_channel(exrhandle, rl->name, get_pass_name(rpass->passtype, a), 
870                                                                                 xstride, xstride*rr->rectx, rpass->rect+a);
871                                 else
872                                         IMB_exr_add_channel(exrhandle, rl->name, make_pass_name(rpass, a), 
873                                                                                 xstride, xstride*rr->rectx, rpass->rect+a);
874                         }
875                 }
876         }
877
878         /* when the filename has no permissions, this can fail */
879         if(IMB_exr_begin_write(exrhandle, filename, rr->rectx, rr->recty, compress)) {
880                 IMB_exr_write_channels(exrhandle);
881                 success= TRUE;
882         }
883         else {
884                 /* TODO, get the error from openexr's exception */
885                 BKE_report(reports, RPT_ERROR, "Error Writing Render Result, see console");
886                 success= FALSE;
887         }
888         IMB_exr_close(exrhandle);
889
890         return success;
891 }
892
893 /* callbacks for RE_MultilayerConvert */
894 static void *ml_addlayer_cb(void *base, char *str)
895 {
896         RenderResult *rr= base;
897         RenderLayer *rl;
898         
899         rl= MEM_callocN(sizeof(RenderLayer), "new render layer");
900         BLI_addtail(&rr->layers, rl);
901         
902         BLI_strncpy(rl->name, str, EXR_LAY_MAXNAME);
903         return rl;
904 }
905 static void ml_addpass_cb(void *UNUSED(base), void *lay, char *str, float *rect, int totchan, char *chan_id)
906 {
907         RenderLayer *rl= lay;   
908         RenderPass *rpass= MEM_callocN(sizeof(RenderPass), "loaded pass");
909         int a;
910         
911         BLI_addtail(&rl->passes, rpass);
912         rpass->channels= totchan;
913
914         rpass->passtype= passtype_from_name(str);
915         if(rpass->passtype==0) printf("unknown pass %s\n", str);
916         rl->passflag |= rpass->passtype;
917         
918         BLI_strncpy(rpass->name, str, EXR_PASS_MAXNAME);
919         /* channel id chars */
920         for(a=0; a<totchan; a++)
921                 rpass->chan_id[a]= chan_id[a];
922         
923         rpass->rect= rect;
924 }
925
926 /* from imbuf, if a handle was returned we convert this to render result */
927 RenderResult *RE_MultilayerConvert(void *exrhandle, int rectx, int recty)
928 {
929         RenderResult *rr= MEM_callocN(sizeof(RenderResult), "loaded render result");
930         RenderLayer *rl;
931         RenderPass *rpass;
932         
933         rr->rectx= rectx;
934         rr->recty= recty;
935         
936         IMB_exr_multilayer_convert(exrhandle, rr, ml_addlayer_cb, ml_addpass_cb);
937
938         for(rl=rr->layers.first; rl; rl=rl->next) {
939                 rl->rectx= rectx;
940                 rl->recty= recty;
941
942                 for(rpass=rl->passes.first; rpass; rpass=rpass->next) {
943                         rpass->rectx= rectx;
944                         rpass->recty= recty;
945                 }
946         }
947         
948         return rr;
949 }
950
951 /* called in end of render, to add names to passes... for UI only */
952 static void renderresult_add_names(RenderResult *rr)
953 {
954         RenderLayer *rl;
955         RenderPass *rpass;
956         
957         for(rl= rr->layers.first; rl; rl= rl->next)
958                 for(rpass= rl->passes.first; rpass; rpass= rpass->next)
959                         BLI_strncpy(rpass->name, get_pass_name(rpass->passtype, -1), sizeof(rpass->name));
960 }
961
962 /* called for reading temp files, and for external engines */
963 static int read_render_result_from_file(const char *filename, RenderResult *rr)
964 {
965         RenderLayer *rl;
966         RenderPass *rpass;
967         void *exrhandle= IMB_exr_get_handle();
968         int rectx, recty;
969
970         if(IMB_exr_begin_read(exrhandle, filename, &rectx, &recty)==0) {
971                 printf("failed being read %s\n", filename);
972                 IMB_exr_close(exrhandle);
973                 return 0;
974         }
975
976         if(rr == NULL || rectx!=rr->rectx || recty!=rr->recty) {
977                 if(rr)
978                         printf("error in reading render result: dimensions don't match\n");
979                 else
980                         printf("error in reading render result: NULL result pointer\n");
981                 IMB_exr_close(exrhandle);
982                 return 0;
983         }
984         else {
985                 for(rl= rr->layers.first; rl; rl= rl->next) {
986                         
987                         /* combined */
988                         if(rl->rectf) {
989                                 int a, xstride= 4;
990                                 for(a=0; a<xstride; a++)
991                                         IMB_exr_set_channel(exrhandle, rl->name, get_pass_name(SCE_PASS_COMBINED, a), 
992                                                                                 xstride, xstride*rectx, rl->rectf+a);
993                         }
994                         
995                         /* passes are allocated in sync */
996                         for(rpass= rl->passes.first; rpass; rpass= rpass->next) {
997                                 int a, xstride= rpass->channels;
998                                 for(a=0; a<xstride; a++)
999                                         IMB_exr_set_channel(exrhandle, rl->name, get_pass_name(rpass->passtype, a), 
1000                                                                                 xstride, xstride*rectx, rpass->rect+a);
1001                         }
1002                         
1003                 }
1004                 IMB_exr_read_channels(exrhandle);
1005                 renderresult_add_names(rr);
1006         }
1007
1008         IMB_exr_close(exrhandle);
1009
1010         return 1;
1011 }
1012
1013 /* only for temp buffer files, makes exact copy of render result */
1014 static int read_render_result(Render *re, int sample)
1015 {
1016         char str[FILE_MAX];
1017         int success;
1018
1019         BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
1020
1021         RE_FreeRenderResult(re->result);
1022         re->result= new_render_result(re, &re->disprect, 0, RR_USEMEM);
1023
1024         render_unique_exr_name(re, str, sample);
1025         printf("read exr tmp file: %s\n", str);
1026
1027         if(read_render_result_from_file(str, re->result)) {
1028                 success= TRUE;
1029         }
1030         else {
1031                 printf("cannot read: %s\n", str);
1032                 success= FALSE;
1033
1034         }
1035
1036         BLI_rw_mutex_unlock(&re->resultmutex);
1037
1038         return success;
1039 }
1040
1041 /* *************************************************** */
1042
1043 Render *RE_GetRender(const char *name)
1044 {
1045         Render *re;
1046
1047         /* search for existing renders */
1048         for(re= RenderGlobal.renderlist.first; re; re= re->next)
1049                 if(strncmp(re->name, name, RE_MAXNAME)==0)
1050                         break;
1051
1052         return re;
1053 }
1054
1055 /* if you want to know exactly what has been done */
1056 RenderResult *RE_AcquireResultRead(Render *re)
1057 {
1058         if(re) {
1059                 BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_READ);
1060                 return re->result;
1061         }
1062
1063         return NULL;
1064 }
1065
1066 RenderResult *RE_AcquireResultWrite(Render *re)
1067 {
1068         if(re) {
1069                 BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
1070                 return re->result;
1071         }
1072
1073         return NULL;
1074 }
1075
1076 void RE_SwapResult(Render *re, RenderResult **rr)
1077 {
1078         /* for keeping render buffers */
1079         if(re) {
1080                 SWAP(RenderResult*, re->result, *rr);
1081         }
1082 }
1083
1084
1085 void RE_ReleaseResult(Render *re)
1086 {
1087         if(re)
1088                 BLI_rw_mutex_unlock(&re->resultmutex);
1089 }
1090
1091 /* displist.c util.... */
1092 Scene *RE_GetScene(Render *re)
1093 {
1094         if(re)
1095                 return re->scene;
1096         return NULL;
1097 }
1098
1099 RenderLayer *render_get_active_layer(Render *re, RenderResult *rr)
1100 {
1101         RenderLayer *rl= BLI_findlink(&rr->layers, re->r.actlay);
1102         
1103         if(rl) 
1104                 return rl;
1105         else 
1106                 return rr->layers.first;
1107 }
1108
1109
1110 /* fill provided result struct with what's currently active or done */
1111 void RE_AcquireResultImage(Render *re, RenderResult *rr)
1112 {
1113         memset(rr, 0, sizeof(RenderResult));
1114
1115         if(re) {
1116                 BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_READ);
1117
1118                 if(re->result) {
1119                         RenderLayer *rl;
1120                         
1121                         rr->rectx= re->result->rectx;
1122                         rr->recty= re->result->recty;
1123                         
1124                         rr->rectf= re->result->rectf;
1125                         rr->rectz= re->result->rectz;
1126                         rr->rect32= re->result->rect32;
1127                         
1128                         /* active layer */
1129                         rl= render_get_active_layer(re, re->result);
1130
1131                         if(rl) {
1132                                 if(rr->rectf==NULL)
1133                                         rr->rectf= rl->rectf;
1134                                 if(rr->rectz==NULL)
1135                                         rr->rectz= RE_RenderLayerGetPass(rl, SCE_PASS_Z);       
1136                         }
1137
1138                         rr->have_combined= (re->result->rectf != NULL);
1139                         rr->layers= re->result->layers;
1140                 }
1141         }
1142 }
1143
1144 void RE_ReleaseResultImage(Render *re)
1145 {
1146         if(re)
1147                 BLI_rw_mutex_unlock(&re->resultmutex);
1148 }
1149
1150 /* caller is responsible for allocating rect in correct size! */
1151 void RE_ResultGet32(Render *re, unsigned int *rect)
1152 {
1153         RenderResult rres;
1154         
1155         RE_AcquireResultImage(re, &rres);
1156
1157         if(rres.rect32) 
1158                 memcpy(rect, rres.rect32, sizeof(int)*rres.rectx*rres.recty);
1159         else if(rres.rectf) {
1160                 float *fp= rres.rectf;
1161                 int tot= rres.rectx*rres.recty;
1162                 char *cp= (char *)rect;
1163                 
1164                 if (re->r.color_mgt_flag & R_COLOR_MANAGEMENT) {
1165                         /* Finally convert back to sRGB rendered image */ 
1166                         for(;tot>0; tot--, cp+=4, fp+=4) {
1167                                 cp[0] = FTOCHAR(linearrgb_to_srgb(fp[0]));
1168                                 cp[1] = FTOCHAR(linearrgb_to_srgb(fp[1]));
1169                                 cp[2] = FTOCHAR(linearrgb_to_srgb(fp[2]));
1170                                 cp[3] = FTOCHAR(fp[3]);
1171                         }
1172                 }
1173                 else {
1174                         /* Color management is off : no conversion necessary */
1175                         for(;tot>0; tot--, cp+=4, fp+=4) {
1176                                 cp[0] = FTOCHAR(fp[0]);
1177                                 cp[1] = FTOCHAR(fp[1]);
1178                                 cp[2] = FTOCHAR(fp[2]);
1179                                 cp[3] = FTOCHAR(fp[3]);
1180                         }
1181                 }
1182
1183         }
1184         else
1185                 /* else fill with black */
1186                 memset(rect, 0, sizeof(int)*re->rectx*re->recty);
1187
1188         RE_ReleaseResultImage(re);
1189 }
1190
1191 RenderStats *RE_GetStats(Render *re)
1192 {
1193         return &re->i;
1194 }
1195
1196 Render *RE_NewRender(const char *name)
1197 {
1198         Render *re;
1199
1200         /* only one render per name exists */
1201         re= RE_GetRender(name);
1202         if(re==NULL) {
1203                 
1204                 /* new render data struct */
1205                 re= MEM_callocN(sizeof(Render), "new render");
1206                 BLI_addtail(&RenderGlobal.renderlist, re);
1207                 BLI_strncpy(re->name, name, RE_MAXNAME);
1208                 BLI_rw_mutex_init(&re->resultmutex);
1209         }
1210         
1211         RE_InitRenderCB(re);
1212
1213         /* init some variables */
1214         re->ycor= 1.0f;
1215         
1216         return re;
1217 }
1218
1219 /* called for new renders and when finishing rendering so
1220  * we calways have valid callbacks on a render */
1221 void RE_InitRenderCB(Render *re)
1222 {
1223         /* set default empty callbacks */
1224         re->display_init= result_nothing;
1225         re->display_clear= result_nothing;
1226         re->display_draw= result_rcti_nothing;
1227         re->progress= float_nothing;
1228         re->test_break= default_break;
1229         if(G.background)
1230                 re->stats_draw= stats_background;
1231         else
1232                 re->stats_draw= stats_nothing;
1233         /* clear callback handles */
1234         re->dih= re->dch= re->ddh= re->sdh= re->prh= re->tbh= NULL;
1235 }
1236
1237 /* only call this while you know it will remove the link too */
1238 void RE_FreeRender(Render *re)
1239 {
1240         BLI_rw_mutex_end(&re->resultmutex);
1241         
1242         free_renderdata_tables(re);
1243         free_sample_tables(re);
1244         
1245         RE_FreeRenderResult(re->result);
1246         RE_FreeRenderResult(re->pushedresult);
1247         
1248         BLI_remlink(&RenderGlobal.renderlist, re);
1249         MEM_freeN(re);
1250 }
1251
1252 /* exit blender */
1253 void RE_FreeAllRender(void)
1254 {
1255         while(RenderGlobal.renderlist.first) {
1256                 RE_FreeRender(RenderGlobal.renderlist.first);
1257         }
1258 }
1259
1260 /* ********* initialize state ******** */
1261
1262
1263 /* what doesn't change during entire render sequence */
1264 /* disprect is optional, if NULL it assumes full window render */
1265 void RE_InitState(Render *re, Render *source, RenderData *rd, SceneRenderLayer *srl, int winx, int winy, rcti *disprect)
1266 {
1267         re->ok= TRUE;   /* maybe flag */
1268         
1269         re->i.starttime= PIL_check_seconds_timer();
1270         re->r= *rd;             /* hardcopy */
1271         
1272         re->winx= winx;
1273         re->winy= winy;
1274         if(disprect) {
1275                 re->disprect= *disprect;
1276                 re->rectx= disprect->xmax-disprect->xmin;
1277                 re->recty= disprect->ymax-disprect->ymin;
1278         }
1279         else {
1280                 re->disprect.xmin= re->disprect.ymin= 0;
1281                 re->disprect.xmax= winx;
1282                 re->disprect.ymax= winy;
1283                 re->rectx= winx;
1284                 re->recty= winy;
1285         }
1286         
1287         if(re->rectx < 2 || re->recty < 2 || (BKE_imtype_is_movie(rd->imtype) &&
1288                                                                                   (re->rectx < 16 || re->recty < 16) )) {
1289                 BKE_report(re->reports, RPT_ERROR, "Image too small");
1290                 re->ok= 0;
1291                 return;
1292         }
1293
1294         if((re->r.mode & (R_OSA))==0)
1295                 re->r.scemode &= ~R_FULL_SAMPLE;
1296
1297 #ifdef WITH_OPENEXR
1298         if(re->r.scemode & R_FULL_SAMPLE)
1299                 re->r.scemode |= R_EXR_TILE_FILE;       /* enable automatic */
1300
1301         /* Until use_border is made compatible with save_buffers/full_sample, render without the later instead of not rendering at all.*/
1302         if(re->r.mode & R_BORDER) 
1303         {
1304                 re->r.scemode &= ~(R_EXR_TILE_FILE|R_FULL_SAMPLE);
1305         }
1306
1307 #else
1308         /* can't do this without openexr support */
1309         re->r.scemode &= ~(R_EXR_TILE_FILE|R_FULL_SAMPLE);
1310 #endif
1311         
1312         /* fullsample wants uniform osa levels */
1313         if(source && (re->r.scemode & R_FULL_SAMPLE)) {
1314                 /* but, if source has no full sample we disable it */
1315                 if((source->r.scemode & R_FULL_SAMPLE)==0)
1316                         re->r.scemode &= ~R_FULL_SAMPLE;
1317                 else
1318                         re->r.osa= re->osa= source->osa;
1319         }
1320         else {
1321                 /* check state variables, osa? */
1322                 if(re->r.mode & (R_OSA)) {
1323                         re->osa= re->r.osa;
1324                         if(re->osa>16) re->osa= 16;
1325                 }
1326                 else re->osa= 0;
1327         }
1328         
1329         if (srl) {
1330                 int index = BLI_findindex(&re->r.layers, srl);
1331                 if (index != -1) {
1332                         re->r.actlay = index;
1333                         re->r.scemode |= R_SINGLE_LAYER;
1334                 }
1335         }
1336                 
1337         /* always call, checks for gamma, gamma tables and jitter too */
1338         make_sample_tables(re); 
1339         
1340         /* if preview render, we try to keep old result */
1341         BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
1342
1343         if(re->r.scemode & R_PREVIEWBUTS) {
1344                 if(re->result && re->result->rectx==re->rectx && re->result->recty==re->recty);
1345                 else {
1346                         RE_FreeRenderResult(re->result);
1347                         re->result= NULL;
1348                 }
1349         }
1350         else {
1351                 
1352                 /* make empty render result, so display callbacks can initialize */
1353                 RE_FreeRenderResult(re->result);
1354                 re->result= MEM_callocN(sizeof(RenderResult), "new render result");
1355                 re->result->rectx= re->rectx;
1356                 re->result->recty= re->recty;
1357         }
1358
1359         BLI_rw_mutex_unlock(&re->resultmutex);
1360         
1361         /* we clip faces with a minimum of 2 pixel boundary outside of image border. see zbuf.c */
1362         re->clipcrop= 1.0f + 2.0f/(float)(re->winx>re->winy?re->winy:re->winx);
1363         
1364         re->mblur_offs = re->field_offs = 0.f;
1365         
1366         RE_init_threadcount(re);
1367 }
1368
1369 /* part of external api, not called for regular render pipeline */
1370 void RE_SetDispRect (struct Render *re, rcti *disprect)
1371 {
1372         re->disprect= *disprect;
1373         re->rectx= disprect->xmax-disprect->xmin;
1374         re->recty= disprect->ymax-disprect->ymin;
1375         
1376         /* initialize render result */
1377         BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
1378
1379         RE_FreeRenderResult(re->result);
1380         re->result= new_render_result(re, &re->disprect, 0, RR_USEMEM);
1381
1382         BLI_rw_mutex_unlock(&re->resultmutex);
1383 }
1384
1385 void RE_SetWindow(Render *re, rctf *viewplane, float clipsta, float clipend)
1386 {
1387         /* re->ok flag? */
1388         
1389         re->viewplane= *viewplane;
1390         re->clipsta= clipsta;
1391         re->clipend= clipend;
1392         re->r.mode &= ~R_ORTHO;
1393
1394         perspective_m4( re->winmat,re->viewplane.xmin, re->viewplane.xmax, re->viewplane.ymin, re->viewplane.ymax, re->clipsta, re->clipend);
1395         
1396 }
1397
1398 void RE_SetOrtho(Render *re, rctf *viewplane, float clipsta, float clipend)
1399 {
1400         /* re->ok flag? */
1401         
1402         re->viewplane= *viewplane;
1403         re->clipsta= clipsta;
1404         re->clipend= clipend;
1405         re->r.mode |= R_ORTHO;
1406
1407         orthographic_m4( re->winmat,re->viewplane.xmin, re->viewplane.xmax, re->viewplane.ymin, re->viewplane.ymax, re->clipsta, re->clipend);
1408 }
1409
1410 void RE_SetView(Render *re, float mat[][4])
1411 {
1412         /* re->ok flag? */
1413         copy_m4_m4(re->viewmat, mat);
1414         invert_m4_m4(re->viewinv, re->viewmat);
1415 }
1416
1417 /* image and movie output has to move to either imbuf or kernel */
1418 void RE_display_init_cb(Render *re, void *handle, void (*f)(void *handle, RenderResult *rr))
1419 {
1420         re->display_init= f;
1421         re->dih= handle;
1422 }
1423 void RE_display_clear_cb(Render *re, void *handle, void (*f)(void *handle, RenderResult *rr))
1424 {
1425         re->display_clear= f;
1426         re->dch= handle;
1427 }
1428 void RE_display_draw_cb(Render *re, void *handle, void (*f)(void *handle, RenderResult *rr, volatile rcti *rect))
1429 {
1430         re->display_draw= f;
1431         re->ddh= handle;
1432 }
1433 void RE_stats_draw_cb(Render *re, void *handle, void (*f)(void *handle, RenderStats *rs))
1434 {
1435         re->stats_draw= f;
1436         re->sdh= handle;
1437 }
1438 void RE_progress_cb(Render *re, void *handle, void (*f)(void *handle, float))
1439 {
1440         re->progress= f;
1441         re->prh= handle;
1442 }
1443
1444 void RE_draw_lock_cb(Render *re, void *handle, void (*f)(void *handle, int i))
1445 {
1446         re->draw_lock= f;
1447         re->tbh= handle;
1448 }
1449
1450 void RE_test_break_cb(Render *re, void *handle, int (*f)(void *handle))
1451 {
1452         re->test_break= f;
1453         re->tbh= handle;
1454 }
1455
1456
1457 /* ********* add object data (later) ******** */
1458
1459 /* object is considered fully prepared on correct time etc */
1460 /* includes lights */
1461 #if 0
1462 void RE_AddObject(Render *UNUSED(re), Object *UNUSED(ob))
1463 {
1464         
1465 }
1466 #endif
1467
1468 /* *************************************** */
1469
1470 static int render_display_draw_enabled(Render *re)
1471 {
1472         /* don't show preprocess for previewrender sss */
1473         if(re->sss_points)
1474                 return !(re->r.scemode & R_PREVIEWBUTS);
1475         else
1476                 return 1;
1477 }
1478
1479 /* allocate osa new results for samples */
1480 static RenderResult *new_full_sample_buffers(Render *re, ListBase *lb, rcti *partrct, int crop)
1481 {
1482         int a;
1483         
1484         if(re->osa==0)
1485                 return new_render_result(re, partrct, crop, RR_USEMEM);
1486         
1487         for(a=0; a<re->osa; a++) {
1488                 RenderResult *rr= new_render_result(re, partrct, crop, RR_USEMEM);
1489                 BLI_addtail(lb, rr);
1490                 rr->sample_nr= a;
1491         }
1492         
1493         return lb->first;
1494 }
1495
1496
1497 /* the main thread call, renders an entire part */
1498 static void *do_part_thread(void *pa_v)
1499 {
1500         RenderPart *pa= pa_v;
1501         
1502         /* need to return nicely all parts on esc */
1503         if(R.test_break(R.tbh)==0) {
1504                 
1505                 if(!R.sss_points && (R.r.scemode & R_FULL_SAMPLE))
1506                         pa->result= new_full_sample_buffers(&R, &pa->fullresult, &pa->disprect, pa->crop);
1507                 else
1508                         pa->result= new_render_result(&R, &pa->disprect, pa->crop, RR_USEMEM);
1509
1510                 if(R.sss_points)
1511                         zbufshade_sss_tile(pa);
1512                 else if(R.osa)
1513                         zbufshadeDA_tile(pa);
1514                 else
1515                         zbufshade_tile(pa);
1516                 
1517                 /* merge too on break! */
1518                 if(R.result->exrhandle) {
1519                         RenderResult *rr, *rrpart;
1520                         
1521                         for(rr= R.result, rrpart= pa->result; rr && rrpart; rr= rr->next, rrpart= rrpart->next)
1522                                 save_render_result_tile(rr, rrpart);
1523                         
1524                 }
1525                 else if(render_display_draw_enabled(&R)) {
1526                         /* on break, don't merge in result for preview renders, looks nicer */
1527                         if(R.test_break(R.tbh) && (R.r.scemode & R_PREVIEWBUTS));
1528                         else merge_render_result(R.result, pa->result);
1529                 }
1530         }
1531         
1532         pa->ready= 1;
1533         
1534         return NULL;
1535 }
1536
1537 /* calculus for how much 1 pixel rendered should rotate the 3d geometry */
1538 /* is not that simple, needs to be corrected for errors of larger viewplane sizes */
1539 /* called in initrender.c, initparts() and convertblender.c, for speedvectors */
1540 float panorama_pixel_rot(Render *re)
1541 {
1542         float psize, phi, xfac;
1543         float borderfac= (float)(re->disprect.xmax - re->disprect.xmin) / (float)re->winx;
1544         
1545         /* size of 1 pixel mapped to viewplane coords */
1546         psize= (re->viewplane.xmax-re->viewplane.xmin)/(float)(re->winx);
1547         /* angle of a pixel */
1548         phi= atan(psize/re->clipsta);
1549         
1550         /* correction factor for viewplane shifting, first calculate how much the viewplane angle is */
1551         xfac= borderfac*((re->viewplane.xmax-re->viewplane.xmin))/(float)re->xparts;
1552         xfac= atan(0.5f*xfac/re->clipsta); 
1553         /* and how much the same viewplane angle is wrapped */
1554         psize= 0.5f*phi*((float)re->partx);
1555         
1556         /* the ratio applied to final per-pixel angle */
1557         phi*= xfac/psize;
1558         
1559         return phi;
1560 }
1561
1562 /* call when all parts stopped rendering, to find the next Y slice */
1563 /* if slice found, it rotates the dbase */
1564 static RenderPart *find_next_pano_slice(Render *re, int *minx, rctf *viewplane)
1565 {
1566         RenderPart *pa, *best= NULL;
1567         
1568         *minx= re->winx;
1569         
1570         /* most left part of the non-rendering parts */
1571         for(pa= re->parts.first; pa; pa= pa->next) {
1572                 if(pa->ready==0 && pa->nr==0) {
1573                         if(pa->disprect.xmin < *minx) {
1574                                 best= pa;
1575                                 *minx= pa->disprect.xmin;
1576                         }
1577                 }
1578         }
1579                         
1580         if(best) {
1581                 float phi= panorama_pixel_rot(re);
1582
1583                 R.panodxp= (re->winx - (best->disprect.xmin + best->disprect.xmax) )/2;
1584                 R.panodxv= ((viewplane->xmax-viewplane->xmin)*R.panodxp)/(float)(re->winx);
1585                 
1586                 /* shift viewplane */
1587                 R.viewplane.xmin = viewplane->xmin + R.panodxv;
1588                 R.viewplane.xmax = viewplane->xmax + R.panodxv;
1589                 RE_SetWindow(re, &R.viewplane, R.clipsta, R.clipend);
1590                 copy_m4_m4(R.winmat, re->winmat);
1591                 
1592                 /* rotate database according to part coordinates */
1593                 project_renderdata(re, projectverto, 1, -R.panodxp*phi, 1);
1594                 R.panosi= sin(R.panodxp*phi);
1595                 R.panoco= cos(R.panodxp*phi);
1596         }
1597         return best;
1598 }
1599
1600 static RenderPart *find_next_part(Render *re, int minx)
1601 {
1602         RenderPart *pa, *best= NULL;
1603
1604         /* long long int's needed because of overflow [#24414] */
1605         long long int centx=re->winx/2, centy=re->winy/2, tot=1;
1606         long long int mindist= (long long int)re->winx * (long long int)re->winy;
1607         
1608         /* find center of rendered parts, image center counts for 1 too */
1609         for(pa= re->parts.first; pa; pa= pa->next) {
1610                 if(pa->ready) {
1611                         centx+= (pa->disprect.xmin+pa->disprect.xmax)/2;
1612                         centy+= (pa->disprect.ymin+pa->disprect.ymax)/2;
1613                         tot++;
1614                 }
1615         }
1616         centx/=tot;
1617         centy/=tot;
1618         
1619         /* closest of the non-rendering parts */
1620         for(pa= re->parts.first; pa; pa= pa->next) {
1621                 if(pa->ready==0 && pa->nr==0) {
1622                         long long int distx= centx - (pa->disprect.xmin+pa->disprect.xmax)/2;
1623                         long long int disty= centy - (pa->disprect.ymin+pa->disprect.ymax)/2;
1624                         distx= (long long int)sqrt(distx*distx + disty*disty);
1625                         if(distx<mindist) {
1626                                 if(re->r.mode & R_PANORAMA) {
1627                                         if(pa->disprect.xmin==minx) {
1628                                                 best= pa;
1629                                                 mindist= distx;
1630                                         }
1631                                 }
1632                                 else {
1633                                         best= pa;
1634                                         mindist= distx;
1635                                 }
1636                         }
1637                 }
1638         }
1639         return best;
1640 }
1641
1642 static void print_part_stats(Render *re, RenderPart *pa)
1643 {
1644         char str[64];
1645         
1646         BLI_snprintf(str, sizeof(str), "%s, Part %d-%d", re->scene->id.name+2, pa->nr, re->i.totpart);
1647         re->i.infostr= str;
1648         re->stats_draw(re->sdh, &re->i);
1649         re->i.infostr= NULL;
1650 }
1651
1652 /* make osa new results for samples */
1653 static RenderResult *new_full_sample_buffers_exr(Render *re)
1654 {
1655         int a;
1656         
1657         for(a=0; a<re->osa; a++) {
1658                 RenderResult *rr= new_render_result(re, &re->disprect, 0, 1);
1659                 BLI_addtail(&re->fullresult, rr);
1660                 rr->sample_nr= a;
1661         }
1662         
1663         return re->fullresult.first;
1664 }
1665
1666 static void threaded_tile_processor(Render *re)
1667 {
1668         ListBase threads;
1669         RenderPart *pa, *nextpa;
1670         rctf viewplane= re->viewplane;
1671         int rendering=1, counter= 1, drawtimer=0, hasdrawn, minx=0;
1672         
1673         BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
1674
1675         /* first step; free the entire render result, make new, and/or prepare exr buffer saving */
1676         if(re->result==NULL || !(re->r.scemode & R_PREVIEWBUTS)) {
1677                 RE_FreeRenderResult(re->result);
1678         
1679                 if(re->sss_points && render_display_draw_enabled(re))
1680                         re->result= new_render_result(re, &re->disprect, 0, 0);
1681                 else if(re->r.scemode & R_FULL_SAMPLE)
1682                         re->result= new_full_sample_buffers_exr(re);
1683                 else
1684                         re->result= new_render_result(re, &re->disprect, 0, re->r.scemode & (R_EXR_TILE_FILE|R_FULL_SAMPLE));
1685         }
1686
1687         BLI_rw_mutex_unlock(&re->resultmutex);
1688         
1689         if(re->result==NULL)
1690                 return;
1691         
1692         /* warning; no return here without closing exr file */
1693         
1694         initparts(re);
1695
1696         if(re->result->exrhandle) {
1697                 RenderResult *rr;
1698                 char str[FILE_MAX];
1699                 
1700                 for(rr= re->result; rr; rr= rr->next) {
1701                         render_unique_exr_name(re, str, rr->sample_nr);
1702                 
1703                         printf("write exr tmp file, %dx%d, %s\n", rr->rectx, rr->recty, str);
1704                         IMB_exrtile_begin_write(rr->exrhandle, str, 0, rr->rectx, rr->recty, re->partx, re->party);
1705                 }
1706         }
1707         
1708         BLI_init_threads(&threads, do_part_thread, re->r.threads);
1709         
1710         /* assuming no new data gets added to dbase... */
1711         R= *re;
1712         
1713         /* set threadsafe break */
1714         R.test_break= thread_break;
1715         
1716         /* timer loop demands to sleep when no parts are left, so we enter loop with a part */
1717         if(re->r.mode & R_PANORAMA)
1718                 nextpa= find_next_pano_slice(re, &minx, &viewplane);
1719         else
1720                 nextpa= find_next_part(re, 0);
1721         
1722         while(rendering) {
1723                 
1724                 if(re->test_break(re->tbh))
1725                         PIL_sleep_ms(50);
1726                 else if(nextpa && BLI_available_threads(&threads)) {
1727                         drawtimer= 0;
1728                         nextpa->nr= counter++;  /* for nicest part, and for stats */
1729                         nextpa->thread= BLI_available_thread_index(&threads);   /* sample index */
1730                         BLI_insert_thread(&threads, nextpa);
1731
1732                         nextpa= find_next_part(re, minx);
1733                 }
1734                 else if(re->r.mode & R_PANORAMA) {
1735                         if(nextpa==NULL && BLI_available_threads(&threads)==re->r.threads)
1736                                 nextpa= find_next_pano_slice(re, &minx, &viewplane);
1737                         else {
1738                                 PIL_sleep_ms(50);
1739                                 drawtimer++;
1740                         }
1741                 }
1742                 else {
1743                         PIL_sleep_ms(50);
1744                         drawtimer++;
1745                 }
1746                 
1747                 /* check for ready ones to display, and if we need to continue */
1748                 rendering= 0;
1749                 hasdrawn= 0;
1750                 for(pa= re->parts.first; pa; pa= pa->next) {
1751                         if(pa->ready) {
1752                                 
1753                                 BLI_remove_thread(&threads, pa);
1754                                 
1755                                 if(pa->result) {
1756                                         if(render_display_draw_enabled(re))
1757                                                 re->display_draw(re->ddh, pa->result, NULL);
1758                                         print_part_stats(re, pa);
1759                                         
1760                                         free_render_result(&pa->fullresult, pa->result);
1761                                         pa->result= NULL;
1762                                         re->i.partsdone++;
1763                                         re->progress(re->prh, re->i.partsdone / (float)re->i.totpart);
1764                                         hasdrawn= 1;
1765                                 }
1766                         }
1767                         else {
1768                                 rendering= 1;
1769                                 if(pa->nr && pa->result && drawtimer>20) {
1770                                         if(render_display_draw_enabled(re))
1771                                                 re->display_draw(re->ddh, pa->result, &pa->result->renrect);
1772                                         hasdrawn= 1;
1773                                 }
1774                         }
1775                 }
1776                 if(hasdrawn)
1777                         drawtimer= 0;
1778
1779                 /* on break, wait for all slots to get freed */
1780                 if( (g_break=re->test_break(re->tbh)) && BLI_available_threads(&threads)==re->r.threads)
1781                         rendering= 0;
1782                 
1783         }
1784         
1785         if(re->result->exrhandle) {
1786                 RenderResult *rr;
1787
1788                 BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
1789                 save_empty_result_tiles(re);
1790                 
1791                 for(rr= re->result; rr; rr= rr->next) {
1792                         IMB_exr_close(rr->exrhandle);
1793                         rr->exrhandle= NULL;
1794                 }
1795                 
1796                 free_render_result(&re->fullresult, re->result);
1797                 re->result= NULL;
1798
1799                 BLI_rw_mutex_unlock(&re->resultmutex);
1800                 
1801                 read_render_result(re, 0);
1802         }
1803         
1804         /* unset threadsafety */
1805         g_break= 0;
1806         
1807         BLI_end_threads(&threads);
1808         freeparts(re);
1809         re->viewplane= viewplane; /* restore viewplane, modified by pano render */
1810 }
1811
1812 /* currently only called by preview renders and envmap */
1813 void RE_TileProcessor(Render *re)
1814 {
1815         threaded_tile_processor(re);
1816 }
1817
1818 /* ************  This part uses API, for rendering Blender scenes ********** */
1819
1820 static void add_freestyle(Render *re);
1821
1822 static void do_render_3d(Render *re)
1823 {
1824         /* try external */
1825         if(RE_engine_render(re, 0))
1826                 return;
1827
1828         /* internal */
1829         
1830 //      re->cfra= cfra; /* <- unused! */
1831         re->scene->r.subframe = re->mblur_offs + re->field_offs;
1832         
1833         /* lock drawing in UI during data phase */
1834         if(re->draw_lock)
1835                 re->draw_lock(re->dlh, 1);
1836         
1837         /* make render verts/faces/halos/lamps */
1838         if(render_scene_needs_vector(re))
1839                 RE_Database_FromScene_Vectors(re, re->main, re->scene, re->lay);
1840         else
1841                 RE_Database_FromScene(re, re->main, re->scene, re->lay, 1);
1842         
1843         /* clear UI drawing locks */
1844         if(re->draw_lock)
1845                 re->draw_lock(re->dlh, 0);
1846         
1847         threaded_tile_processor(re);
1848         
1849         /* do left-over 3d post effects (flares) */
1850         if(re->flag & R_HALO)
1851                 if(!re->test_break(re->tbh))
1852                         add_halo_flare(re);
1853         
1854         /* Freestyle  */
1855         if( re->r.mode & R_EDGE_FRS && re->r.renderer==R_INTERN)
1856                 if(!re->test_break(re->tbh))
1857                         add_freestyle(re);
1858                 
1859         /* free all render verts etc */
1860         RE_Database_Free(re);
1861         
1862         re->scene->r.subframe = 0.f;
1863 }
1864
1865 /* called by blur loop, accumulate RGBA key alpha */
1866 static void addblur_rect_key(RenderResult *rr, float *rectf, float *rectf1, float blurfac)
1867 {
1868         float mfac= 1.0f - blurfac;
1869         int a, b, stride= 4*rr->rectx;
1870         int len= stride*sizeof(float);
1871         
1872         for(a=0; a<rr->recty; a++) {
1873                 if(blurfac==1.0f) {
1874                         memcpy(rectf, rectf1, len);
1875                 }
1876                 else {
1877                         float *rf= rectf, *rf1= rectf1;
1878                         
1879                         for( b= rr->rectx; b>0; b--, rf+=4, rf1+=4) {
1880                                 if(rf1[3]<0.01f)
1881                                         rf[3]= mfac*rf[3];
1882                                 else if(rf[3]<0.01f) {
1883                                         rf[0]= rf1[0];
1884                                         rf[1]= rf1[1];
1885                                         rf[2]= rf1[2];
1886                                         rf[3]= blurfac*rf1[3];
1887                                 }
1888                                 else {
1889                                         rf[0]= mfac*rf[0] + blurfac*rf1[0];
1890                                         rf[1]= mfac*rf[1] + blurfac*rf1[1];
1891                                         rf[2]= mfac*rf[2] + blurfac*rf1[2];
1892                                         rf[3]= mfac*rf[3] + blurfac*rf1[3];
1893                                 }                               
1894                         }
1895                 }
1896                 rectf+= stride;
1897                 rectf1+= stride;
1898         }
1899 }
1900
1901 /* called by blur loop, accumulate renderlayers */
1902 static void addblur_rect(RenderResult *rr, float *rectf, float *rectf1, float blurfac, int channels)
1903 {
1904         float mfac= 1.0f - blurfac;
1905         int a, b, stride= channels*rr->rectx;
1906         int len= stride*sizeof(float);
1907         
1908         for(a=0; a<rr->recty; a++) {
1909                 if(blurfac==1.0f) {
1910                         memcpy(rectf, rectf1, len);
1911                 }
1912                 else {
1913                         float *rf= rectf, *rf1= rectf1;
1914                         
1915                         for( b= rr->rectx*channels; b>0; b--, rf++, rf1++) {
1916                                 rf[0]= mfac*rf[0] + blurfac*rf1[0];
1917                         }
1918                 }
1919                 rectf+= stride;
1920                 rectf1+= stride;
1921         }
1922 }
1923
1924
1925 /* called by blur loop, accumulate renderlayers */
1926 static void merge_renderresult_blur(RenderResult *rr, RenderResult *brr, float blurfac, int key_alpha)
1927 {
1928         RenderLayer *rl, *rl1;
1929         RenderPass *rpass, *rpass1;
1930         
1931         rl1= brr->layers.first;
1932         for(rl= rr->layers.first; rl && rl1; rl= rl->next, rl1= rl1->next) {
1933                 
1934                 /* combined */
1935                 if(rl->rectf && rl1->rectf) {
1936                         if(key_alpha)
1937                                 addblur_rect_key(rr, rl->rectf, rl1->rectf, blurfac);
1938                         else
1939                                 addblur_rect(rr, rl->rectf, rl1->rectf, blurfac, 4);
1940                 }
1941                 
1942                 /* passes are allocated in sync */
1943                 rpass1= rl1->passes.first;
1944                 for(rpass= rl->passes.first; rpass && rpass1; rpass= rpass->next, rpass1= rpass1->next) {
1945                         addblur_rect(rr, rpass->rect, rpass1->rect, blurfac, rpass->channels);
1946                 }
1947         }
1948 }
1949
1950 /* main blur loop, can be called by fields too */
1951 static void do_render_blur_3d(Render *re)
1952 {
1953         RenderResult *rres;
1954         float blurfac;
1955         int blur= re->r.mblur_samples;
1956         
1957         /* create accumulation render result */
1958         rres= new_render_result(re, &re->disprect, 0, RR_USEMEM);
1959         
1960         /* do the blur steps */
1961         while(blur--) {
1962                 re->mblur_offs = re->r.blurfac*((float)(re->r.mblur_samples-blur))/(float)re->r.mblur_samples;
1963                 
1964                 re->i.curblur= re->r.mblur_samples-blur;        /* stats */
1965                 
1966                 do_render_3d(re);
1967                 
1968                 blurfac= 1.0f/(float)(re->r.mblur_samples-blur);
1969                 
1970                 merge_renderresult_blur(rres, re->result, blurfac, re->r.alphamode & R_ALPHAKEY);
1971                 if(re->test_break(re->tbh)) break;
1972         }
1973         
1974         /* swap results */
1975         BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
1976         RE_FreeRenderResult(re->result);
1977         re->result= rres;
1978         BLI_rw_mutex_unlock(&re->resultmutex);
1979         
1980         re->mblur_offs = 0.0f;
1981         re->i.curblur= 0;       /* stats */
1982         
1983         /* weak... the display callback wants an active renderlayer pointer... */
1984         re->result->renlay= render_get_active_layer(re, re->result);
1985         re->display_draw(re->ddh, re->result, NULL);    
1986 }
1987
1988
1989 /* function assumes rectf1 and rectf2 to be half size of rectf */
1990 static void interleave_rect(RenderResult *rr, float *rectf, float *rectf1, float *rectf2, int channels)
1991 {
1992         int a, stride= channels*rr->rectx;
1993         int len= stride*sizeof(float);
1994         
1995         for(a=0; a<rr->recty; a+=2) {
1996                 memcpy(rectf, rectf1, len);
1997                 rectf+= stride;
1998                 rectf1+= stride;
1999                 memcpy(rectf, rectf2, len);
2000                 rectf+= stride;
2001                 rectf2+= stride;
2002         }
2003 }
2004
2005 /* merge render results of 2 fields */
2006 static void merge_renderresult_fields(RenderResult *rr, RenderResult *rr1, RenderResult *rr2)
2007 {
2008         RenderLayer *rl, *rl1, *rl2;
2009         RenderPass *rpass, *rpass1, *rpass2;
2010         
2011         rl1= rr1->layers.first;
2012         rl2= rr2->layers.first;
2013         for(rl= rr->layers.first; rl && rl1 && rl2; rl= rl->next, rl1= rl1->next, rl2= rl2->next) {
2014                 
2015                 /* combined */
2016                 if(rl->rectf && rl1->rectf && rl2->rectf)
2017                         interleave_rect(rr, rl->rectf, rl1->rectf, rl2->rectf, 4);
2018                 
2019                 /* passes are allocated in sync */
2020                 rpass1= rl1->passes.first;
2021                 rpass2= rl2->passes.first;
2022                 for(rpass= rl->passes.first; rpass && rpass1 && rpass2; rpass= rpass->next, rpass1= rpass1->next, rpass2= rpass2->next) {
2023                         interleave_rect(rr, rpass->rect, rpass1->rect, rpass2->rect, rpass->channels);
2024                 }
2025         }
2026 }
2027
2028
2029 /* interleaves 2 frames */
2030 static void do_render_fields_3d(Render *re)
2031 {
2032         Object *camera= RE_GetCamera(re);
2033         RenderResult *rr1, *rr2= NULL;
2034         
2035         /* no render result was created, we can safely halve render y */
2036         re->winy /= 2;
2037         re->recty /= 2;
2038         re->disprect.ymin /= 2;
2039         re->disprect.ymax /= 2;
2040         
2041         re->i.curfield= 1;      /* stats */
2042         
2043         /* first field, we have to call camera routine for correct aspect and subpixel offset */
2044         RE_SetCamera(re, camera);
2045         if(re->r.mode & R_MBLUR && (re->r.scemode & R_FULL_SAMPLE)==0)
2046                 do_render_blur_3d(re);
2047         else
2048                 do_render_3d(re);
2049
2050         BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
2051         rr1= re->result;
2052         re->result= NULL;
2053         BLI_rw_mutex_unlock(&re->resultmutex);
2054         
2055         /* second field */
2056         if(!re->test_break(re->tbh)) {
2057                 
2058                 re->i.curfield= 2;      /* stats */
2059                 
2060                 re->flag |= R_SEC_FIELD;
2061                 if((re->r.mode & R_FIELDSTILL)==0) {
2062                         re->field_offs = 0.5f;
2063                 }
2064                 RE_SetCamera(re, camera);
2065                 if(re->r.mode & R_MBLUR && (re->r.scemode & R_FULL_SAMPLE)==0)
2066                         do_render_blur_3d(re);
2067                 else
2068                         do_render_3d(re);
2069                 re->flag &= ~R_SEC_FIELD;
2070                 
2071                 re->field_offs = 0.0f;
2072                 
2073                 rr2= re->result;
2074         }
2075         
2076         /* allocate original height new buffers */
2077         re->winy *= 2;
2078         re->recty *= 2;
2079         re->disprect.ymin *= 2;
2080         re->disprect.ymax *= 2;
2081
2082         BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
2083         re->result= new_render_result(re, &re->disprect, 0, RR_USEMEM);
2084
2085         if(rr2) {
2086                 if(re->r.mode & R_ODDFIELD)
2087                         merge_renderresult_fields(re->result, rr2, rr1);
2088                 else
2089                         merge_renderresult_fields(re->result, rr1, rr2);
2090                 
2091                 RE_FreeRenderResult(rr2);
2092         }
2093
2094         RE_FreeRenderResult(rr1);
2095         
2096         re->i.curfield= 0;      /* stats */
2097         
2098         /* weak... the display callback wants an active renderlayer pointer... */
2099         re->result->renlay= render_get_active_layer(re, re->result);
2100
2101         BLI_rw_mutex_unlock(&re->resultmutex);
2102
2103         re->display_draw(re->ddh, re->result, NULL);
2104 }
2105
2106 /* main render routine, no compositing */
2107 static void do_render_fields_blur_3d(Render *re)
2108 {
2109         Object *camera= RE_GetCamera(re);
2110         /* also check for camera here */
2111         if(camera == NULL) {
2112                 printf("ERROR: Cannot render, no camera\n");
2113                 G.afbreek= 1;
2114                 return;
2115         }
2116
2117         /* now use renderdata and camera to set viewplane */
2118         RE_SetCamera(re, camera);
2119         
2120         if(re->r.mode & R_FIELDS)
2121                 do_render_fields_3d(re);
2122         else if(re->r.mode & R_MBLUR && (re->r.scemode & R_FULL_SAMPLE)==0)
2123                 do_render_blur_3d(re);
2124         else
2125                 do_render_3d(re);
2126         
2127         /* when border render, check if we have to insert it in black */
2128         if(re->result) {
2129                 if(re->r.mode & R_BORDER) {
2130                         if((re->r.mode & R_CROP)==0) {
2131                                 RenderResult *rres;
2132                                 
2133                                 BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
2134
2135                                 /* sub-rect for merge call later on */
2136                                 re->result->tilerect= re->disprect;
2137                                 
2138                                 /* this copying sequence could become function? */
2139                                 /* weak is: it chances disprect from border */
2140                                 re->disprect.xmin= re->disprect.ymin= 0;
2141                                 re->disprect.xmax= re->winx;
2142                                 re->disprect.ymax= re->winy;
2143                                 re->rectx= re->winx;
2144                                 re->recty= re->winy;
2145                                 
2146                                 rres= new_render_result(re, &re->disprect, 0, RR_USEMEM);
2147                                 
2148                                 merge_render_result(rres, re->result);
2149                                 RE_FreeRenderResult(re->result);
2150                                 re->result= rres;
2151                                 
2152                                 /* weak... the display callback wants an active renderlayer pointer... */
2153                                 re->result->renlay= render_get_active_layer(re, re->result);
2154                                 
2155                                 BLI_rw_mutex_unlock(&re->resultmutex);
2156                 
2157                                 re->display_init(re->dih, re->result);
2158                                 re->display_draw(re->ddh, re->result, NULL);
2159                         }
2160                         else {
2161                                 /* set offset (again) for use in compositor, disprect was manipulated. */
2162                                 re->result->xof= 0;
2163                                 re->result->yof= 0;
2164                         }
2165                 }
2166         }
2167 }
2168
2169
2170 /* within context of current Render *re, render another scene.
2171    it uses current render image size and disprect, but doesn't execute composite
2172 */
2173 static void render_scene(Render *re, Scene *sce, int cfra)
2174 {
2175         Render *resc= RE_NewRender(sce->id.name);
2176         int winx= re->winx, winy= re->winy;
2177         
2178         sce->r.cfra= cfra;
2179
2180         scene_camera_switch_update(sce);
2181
2182         /* exception: scene uses own size (unfinished code) */
2183         if(0) {
2184                 winx= (sce->r.size*sce->r.xsch)/100;
2185                 winy= (sce->r.size*sce->r.ysch)/100;
2186         }
2187         
2188         /* initial setup */
2189         RE_InitState(resc, re, &sce->r, NULL, winx, winy, &re->disprect);
2190         
2191         /* still unsure entity this... */
2192         resc->main= re->main;
2193         resc->scene= sce;
2194         resc->lay= sce->lay;
2195         
2196         /* ensure scene has depsgraph, base flags etc OK */
2197         set_scene_bg(re->main, sce);
2198
2199         /* copy callbacks */
2200         resc->display_draw= re->display_draw;
2201         resc->ddh= re->ddh;
2202         resc->test_break= re->test_break;
2203         resc->tbh= re->tbh;
2204         resc->stats_draw= re->stats_draw;
2205         resc->sdh= re->sdh;
2206         
2207         do_render_fields_blur_3d(resc);
2208 }
2209
2210 /* helper call to detect if this scene needs a render, or if there's a any render layer to render */
2211 static int composite_needs_render(Scene *sce, int this_scene)
2212 {
2213         bNodeTree *ntree= sce->nodetree;
2214         bNode *node;
2215         
2216         if(ntree==NULL) return 1;
2217         if(sce->use_nodes==0) return 1;
2218         if((sce->r.scemode & R_DOCOMP)==0) return 1;
2219         
2220         for(node= ntree->nodes.first; node; node= node->next) {
2221                 if(node->type==CMP_NODE_R_LAYERS)
2222                         if(this_scene==0 || node->id==NULL || node->id==&sce->id)
2223                                 return 1;
2224         }
2225         return 0;
2226 }
2227
2228 static void tag_scenes_for_render(Render *re)
2229 {
2230         bNode *node;
2231         Scene *sce;
2232         
2233         for(sce= re->main->scene.first; sce; sce= sce->id.next)
2234                 sce->id.flag &= ~LIB_DOIT;
2235         
2236         if(RE_GetCamera(re) && composite_needs_render(re->scene, 1))
2237                 re->scene->id.flag |= LIB_DOIT;
2238         
2239         if(re->scene->nodetree==NULL) return;
2240         
2241         /* check for render-layers nodes using other scenes, we tag them LIB_DOIT */
2242         for(node= re->scene->nodetree->nodes.first; node; node= node->next) {
2243                 if(node->type==CMP_NODE_R_LAYERS) {
2244                         if(node->id) {
2245                                 if(node->id != (ID *)re->scene)
2246                                         node->id->flag |= LIB_DOIT;
2247                         }
2248                 }
2249         }
2250         
2251 }
2252
2253 static void ntree_render_scenes(Render *re)
2254 {
2255         bNode *node;
2256         int cfra= re->scene->r.cfra;
2257         int restore_scene= 0;
2258         
2259         if(re->scene->nodetree==NULL) return;
2260         
2261         tag_scenes_for_render(re);
2262         
2263         /* now foreach render-result node tagged we do a full render */
2264         /* results are stored in a way compisitor will find it */
2265         for(node= re->scene->nodetree->nodes.first; node; node= node->next) {
2266                 if(node->type==CMP_NODE_R_LAYERS) {
2267                         if(node->id && node->id != (ID *)re->scene) {
2268                                 if(node->id->flag & LIB_DOIT) {
2269                                         Scene *scene = (Scene*)node->id;
2270
2271                                         render_scene(re, scene, cfra);
2272                                         restore_scene= (scene != re->scene);
2273                                         node->id->flag &= ~LIB_DOIT;
2274                                         
2275                                         nodeUpdate(re->scene->nodetree, node);
2276                                 }
2277                         }
2278                 }
2279         }
2280
2281         /* restore scene if we rendered another last */
2282         if(restore_scene)
2283                 set_scene_bg(re->main, re->scene);
2284 }
2285
2286 /* bad call... need to think over proper method still */
2287 static void render_composit_stats(void *UNUSED(arg), char *str)
2288 {
2289         R.i.infostr= str;
2290         R.stats_draw(R.sdh, &R.i);
2291         R.i.infostr= NULL;
2292 }
2293
2294
2295 /* invokes Freestyle stroke rendering */
2296 static void add_freestyle(Render *re)
2297 {
2298         SceneRenderLayer *srl;
2299         LinkData *link;
2300
2301         FRS_init_stroke_rendering(re);
2302
2303         for(srl= (SceneRenderLayer *)re->scene->r.layers.first; srl; srl= srl->next) {
2304
2305                 link = (LinkData *)MEM_callocN(sizeof(LinkData), "LinkData to Freestyle render");
2306                 BLI_addtail(&re->freestyle_renders, link);
2307
2308                 if( FRS_is_freestyle_enabled(srl) ) {
2309                         link->data = (void *)FRS_do_stroke_rendering(re, srl);
2310                 }
2311         }
2312
2313         FRS_finish_stroke_rendering(re);
2314 }
2315
2316 /* merges the results of Freestyle stroke rendering into a given render result */
2317 static void composite_freestyle_renders(Render *re, int sample)
2318 {
2319         Render *freestyle_render;
2320         SceneRenderLayer *srl;
2321         LinkData *link;
2322
2323         link = (LinkData *)re->freestyle_renders.first;
2324         for(srl= (SceneRenderLayer *)re->scene->r.layers.first; srl; srl= srl->next) {
2325                 if( FRS_is_freestyle_enabled(srl) ) {
2326                         freestyle_render = (Render *)link->data;
2327                         read_render_result(freestyle_render, sample);
2328                         FRS_composite_result(re, srl, freestyle_render);
2329                         RE_FreeRenderResult(freestyle_render->result);
2330                         freestyle_render->result = NULL;
2331                 }
2332                 link = link->next;
2333         }
2334 }
2335
2336 /* releases temporary scenes and renders for Freestyle stroke rendering */
2337 static void free_all_freestyle_renders(Scene *scene)
2338 {
2339         Render *re1, *freestyle_render;
2340         LinkData *link;
2341
2342         for(re1= RenderGlobal.renderlist.first; re1; re1= re1->next) {
2343                 for (link = (LinkData *)re1->freestyle_renders.first; link; link = link->next) {
2344                         if (link->data) {
2345                                 freestyle_render = (Render *)link->data;
2346                                 unlink_scene(G.main, freestyle_render->scene, scene);
2347                                 RE_FreeRender(freestyle_render);
2348                         }
2349                 }
2350                 BLI_freelistN( &re1->freestyle_renders );
2351         }
2352 }
2353
2354
2355 /* reads all buffers, calls optional composite, merges in first result->rectf */
2356 static void do_merge_fullsample(Render *re, bNodeTree *ntree)
2357 {
2358         float *rectf, filt[3][3];
2359         int sample;
2360         
2361         /* interaction callbacks */
2362         if(ntree) {
2363                 ntree->stats_draw= render_composit_stats;
2364                 ntree->test_break= re->test_break;
2365                 ntree->progress= re->progress;
2366                 ntree->sdh= re->sdh;
2367                 ntree->tbh= re->tbh;
2368                 ntree->prh= re->prh;
2369         }
2370         
2371         /* filtmask needs it */
2372         R= *re;
2373         
2374         /* we accumulate in here */
2375         rectf= MEM_mapallocN(re->rectx*re->recty*sizeof(float)*4, "fullsample rgba");
2376         
2377         for(sample=0; sample<re->r.osa; sample++) {
2378                 Render *re1;
2379                 RenderResult rres;
2380                 int x, y, mask;
2381                 
2382                 /* enable full sample print */
2383                 R.i.curfsa= sample+1;
2384                 
2385                 /* set all involved renders on the samplebuffers (first was done by render itself, but needs tagged) */
2386                 /* also function below assumes this */
2387                         
2388                 tag_scenes_for_render(re);
2389                 for(re1= RenderGlobal.renderlist.first; re1; re1= re1->next) {
2390                         if(re1->scene->id.flag & LIB_DOIT) {
2391                                 if(re1->r.scemode & R_FULL_SAMPLE) {
2392                                         if(sample) {
2393                                                 read_render_result(re1, sample);
2394                                                 composite_freestyle_renders(re1, sample);
2395                                         }
2396                                         ntreeCompositTagRender(re1->scene); /* ensure node gets exec to put buffers on stack */
2397                                 }
2398                         }
2399                 }
2400                 
2401                 /* composite */
2402                 if(ntree) {
2403                         ntreeCompositTagRender(re->scene);
2404                         ntreeCompositTagAnimated(ntree);
2405                         
2406                         ntreeCompositExecTree(ntree, &re->r, G.background==0);
2407                 }
2408                 
2409                 /* ensure we get either composited result or the active layer */
2410                 RE_AcquireResultImage(re, &rres);
2411                 
2412                 /* accumulate with filter, and clip */
2413                 mask= (1<<sample);
2414                 mask_array(mask, filt);
2415
2416                 for(y=0; y<re->recty; y++) {
2417                         float *rf= rectf + 4*y*re->rectx;
2418                         float *col= rres.rectf + 4*y*re->rectx;
2419                                 
2420                         for(x=0; x<re->rectx; x++, rf+=4, col+=4) {
2421                                 /* clamping to 1.0 is needed for correct AA */
2422                                 if(col[0]<0.0f) col[0]=0.0f; else if(col[0] > 1.0f) col[0]= 1.0f;
2423                                 if(col[1]<0.0f) col[1]=0.0f; else if(col[1] > 1.0f) col[1]= 1.0f;
2424                                 if(col[2]<0.0f) col[2]=0.0f; else if(col[2] > 1.0f) col[2]= 1.0f;
2425                                 
2426                                 add_filt_fmask_coord(filt, col, rf, re->rectx, re->recty, x, y);
2427                         }
2428                 }
2429                 
2430                 RE_ReleaseResultImage(re);
2431
2432                 /* show stuff */
2433                 if(sample!=re->osa-1) {
2434                         /* weak... the display callback wants an active renderlayer pointer... */
2435                         re->result->renlay= render_get_active_layer(re, re->result);
2436                         re->display_draw(re->ddh, re->result, NULL);
2437                 }
2438                 
2439                 if(re->test_break(re->tbh))
2440                         break;
2441         }
2442         
2443         /* clear interaction callbacks */
2444         if(ntree) {
2445                 ntree->stats_draw= NULL;
2446                 ntree->test_break= NULL;
2447                 ntree->progress= NULL;
2448                 ntree->tbh= ntree->sdh= ntree->prh= NULL;
2449         }
2450         
2451         /* disable full sample print */
2452         R.i.curfsa= 0;
2453         
2454         BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
2455         if(re->result->rectf) 
2456                 MEM_freeN(re->result->rectf);
2457         re->result->rectf= rectf;
2458         BLI_rw_mutex_unlock(&re->resultmutex);
2459 }
2460
2461 /* called externally, via compositor */
2462 void RE_MergeFullSample(Render *re, Main *bmain, Scene *sce, bNodeTree *ntree)
2463 {
2464         Scene *scene;
2465         bNode *node;
2466
2467         /* default start situation */
2468         G.afbreek= 0;
2469         
2470         re->main= bmain;
2471         re->scene= sce;
2472         
2473         /* first call RE_ReadRenderResult on every renderlayer scene. this creates Render structs */
2474         
2475         /* tag scenes unread */
2476         for(scene= re->main->scene.first; scene; scene= scene->id.next) 
2477                 scene->id.flag |= LIB_DOIT;
2478         
2479         for(node= ntree->nodes.first; node; node= node->next) {
2480                 if(node->type==CMP_NODE_R_LAYERS) {
2481                         Scene *nodescene= (Scene *)node->id;
2482                         
2483                         if(nodescene==NULL) nodescene= sce;
2484                         if(nodescene->id.flag & LIB_DOIT) {
2485                                 nodescene->r.mode |= R_OSA;     /* render struct needs tables */
2486                                 RE_ReadRenderResult(sce, nodescene);
2487                                 nodescene->id.flag &= ~LIB_DOIT;
2488                         }
2489                 }
2490         }
2491         
2492         /* own render result should be read/allocated */
2493         if(re->scene->id.flag & LIB_DOIT) {
2494                 RE_ReadRenderResult(re->scene, re->scene);
2495                 re->scene->id.flag &= ~LIB_DOIT;
2496         }
2497         
2498         /* and now we can draw (result is there) */
2499         re->display_init(re->dih, re->result);
2500         re->display_clear(re->dch, re->result);
2501         
2502         do_merge_fullsample(re, ntree);
2503 }
2504
2505 /* returns fully composited render-result on given time step (in RenderData) */
2506 static void do_render_composite_fields_blur_3d(Render *re)
2507 {
2508         bNodeTree *ntree= re->scene->nodetree;
2509         int update_newframe=0;
2510         
2511         /* INIT seeding, compositor can use random texture */
2512         BLI_srandom(re->r.cfra);
2513         
2514         if(composite_needs_render(re->scene, 1)) {
2515                 /* save memory... free all cached images */
2516                 ntreeFreeCache(ntree);
2517                 
2518                 do_render_fields_blur_3d(re);
2519         } 
2520         else {
2521                 /* ensure new result gets added, like for regular renders */
2522                 BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
2523                 
2524                 RE_FreeRenderResult(re->result);
2525                 re->result= new_render_result(re, &re->disprect, 0, RR_USEMEM);
2526
2527                 BLI_rw_mutex_unlock(&re->resultmutex);
2528                 
2529                 /* scene render process already updates animsys */
2530                 update_newframe = 1;
2531         }
2532         
2533         /* swap render result */
2534         if(re->r.scemode & R_SINGLE_LAYER)
2535                 pop_render_result(re);
2536         
2537         if(!re->test_break(re->tbh)) {
2538                 
2539                 if(ntree) {
2540                         ntreeCompositTagRender(re->scene);
2541                         ntreeCompositTagAnimated(ntree);
2542                 }
2543                 
2544                 if(ntree && re->scene->use_nodes && re->r.scemode & R_DOCOMP) {
2545                         /* checks if there are render-result nodes that need scene */
2546                         if((re->r.scemode & R_SINGLE_LAYER)==0)
2547                                 ntree_render_scenes(re);
2548                         
2549                         if(!re->test_break(re->tbh)) {
2550                                 ntree->stats_draw= render_composit_stats;
2551                                 ntree->test_break= re->test_break;
2552                                 ntree->progress= re->progress;
2553                                 ntree->sdh= re->sdh;
2554                                 ntree->tbh= re->tbh;
2555                                 ntree->prh= re->prh;
2556                                 
2557                                 /* in case it was never initialized */
2558                                 R.sdh= re->sdh;
2559                                 R.stats_draw= re->stats_draw;
2560                                 
2561                                 if (update_newframe)
2562                                         scene_update_for_newframe(re->main, re->scene, re->lay);
2563                                 
2564                                 if(re->r.scemode & R_FULL_SAMPLE) 
2565                                         do_merge_fullsample(re, ntree);
2566                                 else {
2567                                         ntreeCompositExecTree(ntree, &re->r, G.background==0);
2568                                 }
2569                                 
2570                                 ntree->stats_draw= NULL;
2571                                 ntree->test_break= NULL;
2572                                 ntree->progress= NULL;
2573                                 ntree->tbh= ntree->sdh= ntree->prh= NULL;
2574                         }
2575                 }
2576                 else if(re->r.scemode & R_FULL_SAMPLE)
2577                         do_merge_fullsample(re, NULL);
2578         }
2579
2580         free_all_freestyle_renders(re->scene);
2581
2582         /* weak... the display callback wants an active renderlayer pointer... */
2583         re->result->renlay= render_get_active_layer(re, re->result);
2584         re->display_draw(re->ddh, re->result, NULL);
2585 }
2586
2587 static void renderresult_stampinfo(Render *re)
2588 {
2589         RenderResult rres;
2590
2591         /* this is the basic trick to get the displayed float or char rect from render result */
2592         RE_AcquireResultImage(re, &rres);
2593         BKE_stamp_buf(re->scene, RE_GetCamera(re), (unsigned char *)rres.rect32, rres.rectf, rres.rectx, rres.recty, 4);
2594         RE_ReleaseResultImage(re);
2595 }
2596
2597 int RE_seq_render_active(Scene *scene, RenderData *rd)
2598 {
2599         Editing *ed;
2600         Sequence *seq;
2601
2602         ed = scene->ed;
2603         
2604         if (!(rd->scemode & R_DOSEQ) || !ed || !ed->seqbase.first)
2605                 return 0;
2606         
2607         for (seq= ed->seqbase.first; seq; seq= seq->next) {
2608                 if (seq->type != SEQ_SOUND)
2609                         return 1;
2610         }
2611         
2612         return 0;
2613 }
2614
2615 static void do_render_seq(Render * re)
2616 {
2617         static int recurs_depth = 0;
2618         struct ImBuf *ibuf;
2619         RenderResult *rr; /* don't assign re->result here as it might change during give_ibuf_seq */
2620         int cfra = re->r.cfra;
2621         SeqRenderData context;
2622
2623         re->i.cfra= cfra;
2624
2625         if(recurs_depth==0) {
2626                 /* otherwise sequencer animation isnt updated */
2627                 BKE_animsys_evaluate_all_animation(re->main, re->scene, (float)cfra); // XXX, was BKE_curframe(re->scene)
2628         }
2629
2630         recurs_depth++;
2631
2632         context = seq_new_render_data(re->main, re->scene,
2633                                       re->result->rectx, re->result->recty, 
2634                                       100);
2635
2636         ibuf = give_ibuf_seq(context, cfra, 0);
2637
2638         recurs_depth--;
2639
2640         rr = re->result;
2641         
2642         BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
2643
2644         if(ibuf) {
2645                 if(ibuf->rect_float) {
2646                         if (!rr->rectf)
2647                                 rr->rectf= MEM_mallocN(4*sizeof(float)*rr->rectx*rr->recty, "render_seq rectf");
2648                         
2649                         /* color management: when off ensure rectf is non-lin, since thats what the internal
2650                          * render engine delivers */
2651                         if(re->r.color_mgt_flag & R_COLOR_MANAGEMENT) {
2652                                 if(ibuf->profile == IB_PROFILE_LINEAR_RGB)
2653                                         memcpy(rr->rectf, ibuf->rect_float, 4*sizeof(float)*rr->rectx*rr->recty);
2654                                 else
2655                                         srgb_to_linearrgb_rgba_rgba_buf(rr->rectf, ibuf->rect_float, rr->rectx*rr->recty);
2656                                         
2657                         }
2658                         else {
2659                                 if(ibuf->profile != IB_PROFILE_LINEAR_RGB)
2660                                         memcpy(rr->rectf, ibuf->rect_float, 4*sizeof(float)*rr->rectx*rr->recty);
2661                                 else
2662                                         linearrgb_to_srgb_rgba_rgba_buf(rr->rectf, ibuf->rect_float, rr->rectx*rr->recty);
2663                         }
2664                         
2665                         /* TSK! Since sequence render doesn't free the *rr render result, the old rect32
2666                            can hang around when sequence render has rendered a 32 bits one before */
2667                         if(rr->rect32) {
2668                                 MEM_freeN(rr->rect32);
2669                                 rr->rect32= NULL;
2670                         }
2671                 }
2672                 else if(ibuf->rect) {
2673                         if (!rr->rect32)
2674                                 rr->rect32= MEM_mallocN(sizeof(int)*rr->rectx*rr->recty, "render_seq rect");
2675
2676                         memcpy(rr->rect32, ibuf->rect, 4*rr->rectx*rr->recty);
2677
2678                         /* if (ibuf->zbuf) { */
2679                         /*      if (R.rectz) freeN(R.rectz); */
2680                         /*      R.rectz = BLI_dupallocN(ibuf->zbuf); */
2681                         /* } */
2682
2683                         /* Same things as above, old rectf can hang around from previous render. */
2684                         if(rr->rectf) {
2685                                 MEM_freeN(rr->rectf);
2686                                 rr->rectf= NULL;
2687                         }
2688                 }
2689                 
2690                 if (recurs_depth == 0) { /* with nested scenes, only free on toplevel... */
2691                         Editing * ed = re->scene->ed;
2692                         if (ed) {
2693                                 free_imbuf_seq(re->scene, &ed->seqbase, TRUE, TRUE);
2694                         }
2695                 }
2696                 IMB_freeImBuf(ibuf);
2697         }
2698         else {
2699                 /* render result is delivered empty in most cases, nevertheless we handle all cases */
2700                 if (rr->rectf)
2701                         memset(rr->rectf, 0, 4*sizeof(float)*rr->rectx*rr->recty);
2702                 else if (rr->rect32)
2703                         memset(rr->rect32, 0, 4*rr->rectx*rr->recty);
2704                 else
2705                         rr->rect32= MEM_callocN(sizeof(int)*rr->rectx*rr->recty, "render_seq rect");
2706         }
2707
2708         BLI_rw_mutex_unlock(&re->resultmutex);
2709
2710         /* just in case this flag went missing at some point */
2711         re->r.scemode |= R_DOSEQ;
2712
2713         /* set overall progress of sequence rendering */
2714         if(re->r.efra!=re->r.sfra)
2715                 re->progress(re->prh, (float)(cfra-re->r.sfra) / (re->r.efra-re->r.sfra));
2716         else
2717                 re->progress(re->prh, 1.0f);
2718 }
2719
2720 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
2721
2722 /* main loop: doing sequence + fields + blur + 3d render + compositing */
2723 static void do_render_all_options(Render *re)
2724 {
2725         scene_camera_switch_update(re->scene);
2726
2727         re->i.starttime= PIL_check_seconds_timer();
2728
2729         /* ensure no images are in memory from previous animated sequences */
2730         BKE_image_all_free_anim_ibufs(re->r.cfra);
2731
2732         if(RE_engine_render(re, 1)) {
2733                 /* in this case external render overrides all */
2734         }
2735         else if(RE_seq_render_active(re->scene, &re->r)) {
2736                 /* note: do_render_seq() frees rect32 when sequencer returns float images */
2737                 if(!re->test_break(re->tbh)) 
2738                         do_render_seq(re);
2739                 
2740                 re->stats_draw(re->sdh, &re->i);
2741                 re->display_draw(re->ddh, re->result, NULL);
2742         }
2743         else {
2744                 do_render_composite_fields_blur_3d(re);
2745         }
2746         
2747         /* for UI only */
2748         BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
2749         renderresult_add_names(re->result);
2750         BLI_rw_mutex_unlock(&re->resultmutex);
2751         
2752         re->i.lastframetime= PIL_check_seconds_timer()- re->i.starttime;
2753         
2754         re->stats_draw(re->sdh, &re->i);
2755         
2756         /* stamp image info here */
2757         if((re->r.stamp & R_STAMP_ALL) && (re->r.stamp & R_STAMP_DRAW)) {
2758                 renderresult_stampinfo(re);
2759                 re->display_draw(re->ddh, re->result, NULL);
2760         }
2761 }
2762
2763 static int check_valid_camera(Scene *scene, Object *camera_override)
2764 {
2765         int check_comp= 1;
2766
2767         if (camera_override == NULL && scene->camera == NULL)
2768                 scene->camera= scene_find_camera(scene);
2769
2770         if(scene->r.scemode&R_DOSEQ) {
2771                 if(scene->ed) {
2772                         Sequence *seq= scene->ed->seqbase.first;
2773
2774                         check_comp= 0;
2775
2776                         while(seq) {
2777                                 if(seq->type == SEQ_SCENE) {
2778                                         if(!seq->scene_camera) {
2779                                                 if(!seq->scene->camera && !scene_find_camera(seq->scene)) {
2780                                                         if(seq->scene == scene) {
2781                                                                 /* for current scene camera could be unneeded due to compisite nodes */
2782                                                                 check_comp= 1;
2783                                                         } else {
2784                                                                 /* for other scenes camera is necessary */
2785                                                                 return 0;
2786                                                         }
2787                                                 }
2788                                         }
2789                                 }
2790
2791                                 seq= seq->next;
2792                         }
2793                 }
2794         }
2795
2796         if(check_comp) { /* no sequencer or sequencer depends on compositor */
2797                 if(scene->r.scemode&R_DOCOMP && scene->use_nodes) {
2798                         bNode *node= scene->nodetree->nodes.first;
2799
2800                         while(node) {
2801                                 if(node->type == CMP_NODE_R_LAYERS) {
2802                                         Scene *sce= node->id ? (Scene*)node->id : scene;
2803
2804                                         if(!sce->camera && !scene_find_camera(sce)) {
2805                                                 /* all render layers nodes need camera */
2806                                                 return 0;
2807                                         }
2808                                 }
2809
2810                                 node= node->next;
2811                         }
2812                 } else {
2813                         return (camera_override != NULL || scene->camera != NULL);
2814                 }
2815         }
2816
2817         return 1;
2818 }
2819
2820 int RE_is_rendering_allowed(Scene *scene, Object *camera_override, ReportList *reports)
2821 {
2822         SceneRenderLayer *srl;
2823         
2824         if(scene->r.mode & R_BORDER) {
2825                 if(scene->r.border.xmax <= scene->r.border.xmin ||
2826                    scene->r.border.ymax <= scene->r.border.ymin) {
2827                         BKE_report(reports, RPT_ERROR, "No border area selected.");
2828                         return 0;
2829                 }
2830         }
2831         
2832         if(scene->r.scemode & (R_EXR_TILE_FILE|R_FULL_SAMPLE)) {
2833                 char str[FILE_MAX];
2834                 
2835                 scene_unique_exr_name(scene, str, 0);
2836                 
2837                 if (BLI_file_is_writable(str)==0) {
2838                         BKE_report(reports, RPT_ERROR, "Can not save render buffers, check the temp default path");
2839                         return 0;
2840                 }
2841                 
2842                 /* no fullsample and edge */
2843                 if((scene->r.scemode & R_FULL_SAMPLE) && (scene->r.mode & R_EDGE)) {
2844                         BKE_report(reports, RPT_ERROR, "Full Sample doesn't support Edge Enhance");
2845                         return 0;
2846                 }
2847                 
2848         }
2849         else
2850                 scene->r.scemode &= ~R_FULL_SAMPLE;     /* clear to be sure */
2851         
2852         if(scene->r.scemode & R_DOCOMP) {
2853                 if(scene->use_nodes) {
2854                         bNodeTree *ntree= scene->nodetree;
2855                         bNode *node;
2856                 
2857                         if(ntree==NULL) {
2858                                 BKE_report(reports, RPT_ERROR, "No Nodetree in Scene");
2859                                 return 0;
2860                         }
2861                         
2862                         for(node= ntree->nodes.first; node; node= node->next)
2863                                 if(node->type==CMP_NODE_COMPOSITE)
2864                                         break;
2865                         
2866                         if(node==NULL) {
2867                                 BKE_report(reports, RPT_ERROR, "No Render Output Node in Scene");
2868                                 return 0;
2869                         }
2870                         
2871                         if(scene->r.scemode & R_FULL_SAMPLE) {
2872                                 if(composite_needs_render(scene, 0)==0) {
2873                                         BKE_report(reports, RPT_ERROR, "Full Sample AA not supported without 3d rendering");
2874                                         return 0;
2875                                 }
2876                         }
2877                 }
2878         }
2879         
2880          /* check valid camera, without camera render is OK (compo, seq) */
2881         if(!check_valid_camera(scene, camera_override)) {
2882                 BKE_report(reports, RPT_ERROR, "No camera");
2883                 return 0;
2884         }
2885         
2886         /* get panorama & ortho, only after camera is set */
2887         object_camera_mode(&scene->r, camera_override ? camera_override : scene->camera);
2888
2889         /* forbidden combinations */
2890         if(scene->r.mode & R_PANORAMA) {
2891                 if(scene->r.mode & R_ORTHO) {
2892                         BKE_report(reports, RPT_ERROR, "No Ortho render possible for Panorama");
2893                         return 0;
2894                 }
2895         }
2896
2897         /* layer flag tests */
2898         if(scene->r.scemode & R_SINGLE_LAYER) {
2899                 srl= BLI_findlink(&scene->r.layers, scene->r.actlay);
2900                 /* force layer to be enabled */
2901                 srl->layflag &= ~SCE_LAY_DISABLE;
2902         }
2903         
2904         for(srl= scene->r.layers.first; srl; srl= srl->next)
2905                 if(!(srl->layflag & SCE_LAY_DISABLE))
2906                         break;
2907         if(srl==NULL) {
2908                 BKE_report(reports, RPT_ERROR, "All RenderLayers are disabled");
2909                 return 0;
2910         }
2911         
2912         /* renderer */
2913         if(!ELEM(scene->r.renderer, R_INTERN, R_YAFRAY)) {
2914                 BKE_report(reports, RPT_ERROR, "Unknown render engine set");
2915                 return 0;
2916         }
2917
2918         return 1;
2919 }
2920
2921 static void validate_render_settings(Render *re)
2922 {
2923         if(re->r.scemode & (R_EXR_TILE_FILE|R_FULL_SAMPLE)) {
2924                 /* no osa + fullsample won't work... */
2925                 if(re->r.osa==0)
2926                         re->r.scemode &= ~R_FULL_SAMPLE;
2927         } else re->r.scemode &= ~R_FULL_SAMPLE; /* clear to be sure */
2928 }
2929
2930 static void update_physics_cache(Render *re, Scene *scene, int UNUSED(anim_init))
2931 {
2932         PTCacheBaker baker;
2933
2934         baker.main = re->main;
2935         baker.scene = scene;
2936         baker.pid = NULL;
2937         baker.bake = 0;
2938         baker.render = 1;
2939         baker.anim_init = 1;
2940         baker.quick_step = 1;
2941         baker.break_test = re->test_break;
2942         baker.break_data = re->tbh;
2943         baker.progressbar = NULL;
2944
2945         BKE_ptcache_bake(&baker);
2946 }
2947 /* evaluating scene options for general Blender render */
2948 static int render_initialize_from_main(Render *re, Main *bmain, Scene *scene, SceneRenderLayer *srl, Object *camera_override, unsigned int lay, int anim, int anim_init)
2949 {
2950         int winx, winy;
2951         rcti disprect;
2952         
2953         /* r.xsch and r.ysch has the actual view window size
2954                 r.border is the clipping rect */
2955         
2956         /* calculate actual render result and display size */
2957         winx= (scene->r.size*scene->r.xsch)/100;
2958         winy= (scene->r.size*scene->r.ysch)/100;
2959         
2960         /* we always render smaller part, inserting it in larger image is compositor bizz, it uses disprect for it */
2961         if(scene->r.mode & R_BORDER) {
2962                 disprect.xmin= scene->r.border.xmin*winx;
2963                 disprect.xmax= scene->r.border.xmax*winx;
2964                 
2965                 disprect.ymin= scene->r.border.ymin*winy;
2966                 disprect.ymax= scene->r.border.ymax*winy;
2967         }
2968         else {
2969                 disprect.xmin= disprect.ymin= 0;
2970                 disprect.xmax= winx;
2971                 disprect.ymax= winy;
2972         }
2973         
2974         re->main= bmain;
2975         re->scene= scene;
2976         re->camera_override= camera_override;
2977         re->lay= lay;
2978         
2979         /* not too nice, but it survives anim-border render */
2980         if(anim) {
2981                 re->disprect= disprect;
2982                 return 1;
2983         }
2984         
2985         /* check all scenes involved */
2986         tag_scenes_for_render(re);
2987
2988         /*
2989          * Disabled completely for now,
2990          * can be later set as render profile option
2991          * and default for background render.
2992         */
2993         if(0) {
2994                 /* make sure dynamics are up to date */
2995                 update_physics_cache(re, scene, anim_init);
2996         }
2997         
2998         if(srl || scene->r.scemode & R_SINGLE_LAYER)
2999                 push_render_result(re);
3000         
3001         RE_InitState(re, NULL, &scene->r, srl, winx, winy, &disprect);
3002         if(!re->ok)  /* if an error was printed, abort */
3003                 return 0;
3004         
3005         /* initstate makes new result, have to send changed tags around */
3006         ntreeCompositTagRender(re->scene);
3007
3008         validate_render_settings(re);
3009
3010         re->display_init(re->dih, re->result);
3011         re->display_clear(re->dch, re->result);
3012         
3013         return 1;
3014 }
3015
3016 void RE_SetReports(Render *re, ReportList *reports)
3017 {
3018         re->reports= reports;
3019 }
3020
3021 /* general Blender frame render call */
3022 void RE_BlenderFrame(Render *re, Main *bmain, Scene *scene, SceneRenderLayer *srl, Object *camera_override, unsigned int lay, int frame, const short write_still)
3023 {
3024         /* ugly global still... is to prevent preview events and signal subsurfs etc to make full resol */
3025         G.rendering= 1;
3026         
3027         scene->r.cfra= frame;
3028         
3029         if(render_initialize_from_main(re, bmain, scene, srl, camera_override, lay, 0, 0)) {
3030                 MEM_reset_peak_memory();
3031
3032                 BLI_exec_cb(re->main, (ID *)scene, BLI_CB_EVT_RENDER_PRE);
3033
3034                 do_render_all_options(re);
3035                 
3036                 if(write_still && !G.afbreek) {
3037                         if(BKE_imtype_is_movie(scene->r.imtype)) {
3038                                 /* operator checks this but incase its called from elsewhere */
3039                                 printf("Error: cant write single images with a movie format!\n");
3040                         }
3041                         else {
3042                                 char name[FILE_MAX];
3043                                 BKE_makepicstring(name, scene->r.pic, scene->r.cfra, scene->r.imtype, scene->r.scemode & R_EXTENSION, FALSE);
3044         
3045                                 /* reports only used for Movie */
3046                                 do_write_image_or_movie(re, scene, NULL, name);
3047                         }
3048                 }
3049
3050                 BLI_exec_cb(re->main, (ID *)scene, BLI_CB_EVT_RENDER_POST); /* keep after file save */
3051         }
3052
3053         /* UGLY WARNING */
3054         G.rendering= 0;
3055 }
3056
3057 void RE_RenderFreestyleStrokes(Render *re, Main *bmain, Scene *scene)
3058 {
3059         re->result_ok= 0;
3060         if(render_initialize_from_main(re, bmain, scene, NULL, NULL, scene->lay, 0, 0)) {
3061                 do_render_fields_blur_3d(re);
3062         }
3063         re->result_ok= 1;
3064 }
3065
3066 static int do_write_image_or_movie(Render *re, Scene *scene, bMovieHandle *mh, const char *name_override)
3067 {
3068         char name[FILE_MAX];
3069         RenderResult rres;
3070         Object *camera= RE_GetCamera(re);
3071         int ok= 1;
3072         
3073         RE_AcquireResultImage(re, &rres);
3074
3075         /* write movie or image */
3076         if(BKE_imtype_is_movie(scene->r.imtype)) {
3077                 int dofree = 0;
3078                 /* note; the way it gets 32 bits rects is weak... */
3079                 if(rres.rect32==NULL) {
3080                         rres.rect32= MEM_mapallocN(sizeof(int)*rres.rectx*rres.recty, "temp 32 bits rect");
3081                         dofree = 1;
3082                 }
3083                 RE_ResultGet32(re, (unsigned int *)rres.rect32);
3084                 ok= mh->append_movie(&re->r, scene->r.cfra, rres.rect32, rres.rectx, rres.recty, re->reports);
3085                 if(dofree) {
3086                         MEM_freeN(rres.rect32);
3087                 }
3088                 printf("Append frame %d", scene->r.cfra);
3089         } 
3090         else {
3091                 if(name_override)
3092                         BLI_strncpy(name, name_override, sizeof(name));
3093                 else
3094                         BKE_makepicstring(name, scene->r.pic, scene->r.cfra, scene->r.imtype, scene->r.scemode & R_EXTENSION, TRUE);
3095                 
3096                 if(re->r.imtype==R_MULTILAYER) {
3097                         if(re->result) {
3098                                 RE_WriteRenderResult(re->reports, re->result, name, scene->r.quality);
3099   &