Merged changes in the trunk up to revision 53146.
[blender.git] / source / blender / render / intern / source / render_result.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2006 Blender Foundation.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/render/intern/source/render_result.c
29  *  \ingroup render
30  */
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35
36 #include "MEM_guardedalloc.h"
37
38 #include "BKE_image.h"
39 #include "BKE_global.h"
40 #include "BKE_main.h"
41 #include "BKE_report.h"
42
43 #include "BLI_fileops.h"
44 #include "BLI_listbase.h"
45 #include "BLI_path_util.h"
46 #include "BLI_rect.h"
47 #include "BLI_string.h"
48 #include "BLI_threads.h"
49 #include "BLI_utildefines.h"
50
51 #include "IMB_imbuf.h"
52 #include "IMB_imbuf_types.h"
53 #include "IMB_colormanagement.h"
54
55 #include "intern/openexr/openexr_multi.h"
56
57 #include "render_result.h"
58 #include "render_types.h"
59
60 #include "FRS_freestyle_config.h"
61
62 /********************************** Free *************************************/
63
64 void render_result_free(RenderResult *res)
65 {
66         if (res == NULL) return;
67
68         while (res->layers.first) {
69                 RenderLayer *rl = res->layers.first;
70                 
71                 if (rl->rectf) MEM_freeN(rl->rectf);
72                 /* acolrect and scolrect are optionally allocated in shade_tile, only free here since it can be used for drawing */
73                 if (rl->acolrect) MEM_freeN(rl->acolrect);
74                 if (rl->scolrect) MEM_freeN(rl->scolrect);
75                 
76                 while (rl->passes.first) {
77                         RenderPass *rpass = rl->passes.first;
78                         if (rpass->rect) MEM_freeN(rpass->rect);
79                         BLI_remlink(&rl->passes, rpass);
80                         MEM_freeN(rpass);
81                 }
82                 BLI_remlink(&res->layers, rl);
83                 MEM_freeN(rl);
84         }
85         
86         if (res->rect32)
87                 MEM_freeN(res->rect32);
88         if (res->rectz)
89                 MEM_freeN(res->rectz);
90         if (res->rectf)
91                 MEM_freeN(res->rectf);
92         if (res->text)
93                 MEM_freeN(res->text);
94         
95         MEM_freeN(res);
96 }
97
98 /* version that's compatible with fullsample buffers */
99 void render_result_free_list(ListBase *lb, RenderResult *rr)
100 {
101         RenderResult *rrnext;
102         
103         for (; rr; rr = rrnext) {
104                 rrnext = rr->next;
105                 
106                 if (lb && lb->first)
107                         BLI_remlink(lb, rr);
108                 
109                 render_result_free(rr);
110         }
111 }
112
113 /********************************* Names *************************************/
114
115 /* NOTE: OpenEXR only supports 32 chars for layer+pass names
116  * In blender we now use max 10 chars for pass, max 20 for layer */
117 static const char *get_pass_name(int passtype, int channel)
118 {
119         
120         if (passtype == SCE_PASS_COMBINED) {
121                 if (channel == -1) return "Combined";
122                 if (channel == 0) return "Combined.R";
123                 if (channel == 1) return "Combined.G";
124                 if (channel == 2) return "Combined.B";
125                 return "Combined.A";
126         }
127         if (passtype == SCE_PASS_Z) {
128                 if (channel == -1) return "Depth";
129                 return "Depth.Z";
130         }
131         if (passtype == SCE_PASS_VECTOR) {
132                 if (channel == -1) return "Vector";
133                 if (channel == 0) return "Vector.X";
134                 if (channel == 1) return "Vector.Y";
135                 if (channel == 2) return "Vector.Z";
136                 return "Vector.W";
137         }
138         if (passtype == SCE_PASS_NORMAL) {
139                 if (channel == -1) return "Normal";
140                 if (channel == 0) return "Normal.X";
141                 if (channel == 1) return "Normal.Y";
142                 return "Normal.Z";
143         }
144         if (passtype == SCE_PASS_UV) {
145                 if (channel == -1) return "UV";
146                 if (channel == 0) return "UV.U";
147                 if (channel == 1) return "UV.V";
148                 return "UV.A";
149         }
150         if (passtype == SCE_PASS_RGBA) {
151                 if (channel == -1) return "Color";
152                 if (channel == 0) return "Color.R";
153                 if (channel == 1) return "Color.G";
154                 if (channel == 2) return "Color.B";
155                 return "Color.A";
156         }
157         if (passtype == SCE_PASS_EMIT) {
158                 if (channel == -1) return "Emit";
159                 if (channel == 0) return "Emit.R";
160                 if (channel == 1) return "Emit.G";
161                 return "Emit.B";
162         }
163         if (passtype == SCE_PASS_DIFFUSE) {
164                 if (channel == -1) return "Diffuse";
165                 if (channel == 0) return "Diffuse.R";
166                 if (channel == 1) return "Diffuse.G";
167                 return "Diffuse.B";
168         }
169         if (passtype == SCE_PASS_SPEC) {
170                 if (channel == -1) return "Spec";
171                 if (channel == 0) return "Spec.R";
172                 if (channel == 1) return "Spec.G";
173                 return "Spec.B";
174         }
175         if (passtype == SCE_PASS_SHADOW) {
176                 if (channel == -1) return "Shadow";
177                 if (channel == 0) return "Shadow.R";
178                 if (channel == 1) return "Shadow.G";
179                 return "Shadow.B";
180         }
181         if (passtype == SCE_PASS_AO) {
182                 if (channel == -1) return "AO";
183                 if (channel == 0) return "AO.R";
184                 if (channel == 1) return "AO.G";
185                 return "AO.B";
186         }
187         if (passtype == SCE_PASS_ENVIRONMENT) {
188                 if (channel == -1) return "Env";
189                 if (channel == 0) return "Env.R";
190                 if (channel == 1) return "Env.G";
191                 return "Env.B";
192         }
193         if (passtype == SCE_PASS_INDIRECT) {
194                 if (channel == -1) return "Indirect";
195                 if (channel == 0) return "Indirect.R";
196                 if (channel == 1) return "Indirect.G";
197                 return "Indirect.B";
198         }
199         if (passtype == SCE_PASS_REFLECT) {
200                 if (channel == -1) return "Reflect";
201                 if (channel == 0) return "Reflect.R";
202                 if (channel == 1) return "Reflect.G";
203                 return "Reflect.B";
204         }
205         if (passtype == SCE_PASS_REFRACT) {
206                 if (channel == -1) return "Refract";
207                 if (channel == 0) return "Refract.R";
208                 if (channel == 1) return "Refract.G";
209                 return "Refract.B";
210         }
211         if (passtype == SCE_PASS_INDEXOB) {
212                 if (channel == -1) return "IndexOB";
213                 return "IndexOB.X";
214         }
215         if (passtype == SCE_PASS_INDEXMA) {
216                 if (channel == -1) return "IndexMA";
217                 return "IndexMA.X";
218         }
219         if (passtype == SCE_PASS_MIST) {
220                 if (channel == -1) return "Mist";
221                 return "Mist.Z";
222         }
223         if (passtype == SCE_PASS_RAYHITS) {
224                 if (channel == -1) return "Rayhits";
225                 if (channel == 0) return "Rayhits.R";
226                 if (channel == 1) return "Rayhits.G";
227                 return "Rayhits.B";
228         }
229         if (passtype == SCE_PASS_DIFFUSE_DIRECT) {
230                 if (channel == -1) return "DiffDir";
231                 if (channel == 0) return "DiffDir.R";
232                 if (channel == 1) return "DiffDir.G";
233                 return "DiffDir.B";
234         }
235         if (passtype == SCE_PASS_DIFFUSE_INDIRECT) {
236                 if (channel == -1) return "DiffInd";
237                 if (channel == 0) return "DiffInd.R";
238                 if (channel == 1) return "DiffInd.G";
239                 return "DiffInd.B";
240         }
241         if (passtype == SCE_PASS_DIFFUSE_COLOR) {
242                 if (channel == -1) return "DiffCol";
243                 if (channel == 0) return "DiffCol.R";
244                 if (channel == 1) return "DiffCol.G";
245                 return "DiffCol.B";
246         }
247         if (passtype == SCE_PASS_GLOSSY_DIRECT) {
248                 if (channel == -1) return "GlossDir";
249                 if (channel == 0) return "GlossDir.R";
250                 if (channel == 1) return "GlossDir.G";
251                 return "GlossDir.B";
252         }
253         if (passtype == SCE_PASS_GLOSSY_INDIRECT) {
254                 if (channel == -1) return "GlossInd";
255                 if (channel == 0) return "GlossInd.R";
256                 if (channel == 1) return "GlossInd.G";
257                 return "GlossInd.B";
258         }
259         if (passtype == SCE_PASS_GLOSSY_COLOR) {
260                 if (channel == -1) return "GlossCol";
261                 if (channel == 0) return "GlossCol.R";
262                 if (channel == 1) return "GlossCol.G";
263                 return "GlossCol.B";
264         }
265         if (passtype == SCE_PASS_TRANSM_DIRECT) {
266                 if (channel == -1) return "TransDir";
267                 if (channel == 0) return "TransDir.R";
268                 if (channel == 1) return "TransDir.G";
269                 return "TransDir.B";
270         }
271         if (passtype == SCE_PASS_TRANSM_INDIRECT) {
272                 if (channel == -1) return "TransInd";
273                 if (channel == 0) return "TransInd.R";
274                 if (channel == 1) return "TransInd.G";
275                 return "TransInd.B";
276         }
277         if (passtype == SCE_PASS_TRANSM_COLOR) {
278                 if (channel == -1) return "TransCol";
279                 if (channel == 0) return "TransCol.R";
280                 if (channel == 1) return "TransCol.G";
281                 return "TransCol.B";
282         }
283         return "Unknown";
284 }
285
286 static int passtype_from_name(const char *str)
287 {
288         
289         if (strcmp(str, "Combined") == 0)
290                 return SCE_PASS_COMBINED;
291
292         if (strcmp(str, "Depth") == 0)
293                 return SCE_PASS_Z;
294
295         if (strcmp(str, "Vector") == 0)
296                 return SCE_PASS_VECTOR;
297
298         if (strcmp(str, "Normal") == 0)
299                 return SCE_PASS_NORMAL;
300
301         if (strcmp(str, "UV") == 0)
302                 return SCE_PASS_UV;
303
304         if (strcmp(str, "Color") == 0)
305                 return SCE_PASS_RGBA;
306
307         if (strcmp(str, "Emit") == 0)
308                 return SCE_PASS_EMIT;
309
310         if (strcmp(str, "Diffuse") == 0)
311                 return SCE_PASS_DIFFUSE;
312
313         if (strcmp(str, "Spec") == 0)
314                 return SCE_PASS_SPEC;
315
316         if (strcmp(str, "Shadow") == 0)
317                 return SCE_PASS_SHADOW;
318         
319         if (strcmp(str, "AO") == 0)
320                 return SCE_PASS_AO;
321
322         if (strcmp(str, "Env") == 0)
323                 return SCE_PASS_ENVIRONMENT;
324
325         if (strcmp(str, "Indirect") == 0)
326                 return SCE_PASS_INDIRECT;
327
328         if (strcmp(str, "Reflect") == 0)
329                 return SCE_PASS_REFLECT;
330
331         if (strcmp(str, "Refract") == 0)
332                 return SCE_PASS_REFRACT;
333
334         if (strcmp(str, "IndexOB") == 0)
335                 return SCE_PASS_INDEXOB;
336
337         if (strcmp(str, "IndexMA") == 0)
338                 return SCE_PASS_INDEXMA;
339
340         if (strcmp(str, "Mist") == 0)
341                 return SCE_PASS_MIST;
342         
343         if (strcmp(str, "RayHits") == 0)
344                 return SCE_PASS_RAYHITS;
345
346         if (strcmp(str, "DiffDir") == 0)
347                 return SCE_PASS_DIFFUSE_DIRECT;
348
349         if (strcmp(str, "DiffInd") == 0)
350                 return SCE_PASS_DIFFUSE_INDIRECT;
351
352         if (strcmp(str, "DiffCol") == 0)
353                 return SCE_PASS_DIFFUSE_COLOR;
354
355         if (strcmp(str, "GlossDir") == 0)
356                 return SCE_PASS_GLOSSY_DIRECT;
357
358         if (strcmp(str, "GlossInd") == 0)
359                 return SCE_PASS_GLOSSY_INDIRECT;
360
361         if (strcmp(str, "GlossCol") == 0)
362                 return SCE_PASS_GLOSSY_COLOR;
363
364         if (strcmp(str, "TransDir") == 0)
365                 return SCE_PASS_TRANSM_DIRECT;
366
367         if (strcmp(str, "TransInd") == 0)
368                 return SCE_PASS_TRANSM_INDIRECT;
369
370         if (strcmp(str, "TransCol") == 0)
371                 return SCE_PASS_TRANSM_COLOR;
372
373         return 0;
374 }
375
376 /********************************** New **************************************/
377
378 static void render_layer_add_pass(RenderResult *rr, RenderLayer *rl, int channels, int passtype)
379 {
380         const char *typestr = get_pass_name(passtype, 0);
381         RenderPass *rpass = MEM_callocN(sizeof(RenderPass), typestr);
382         int rectsize = rr->rectx * rr->recty * channels;
383         
384         BLI_addtail(&rl->passes, rpass);
385         rpass->passtype = passtype;
386         rpass->channels = channels;
387         rpass->rectx = rl->rectx;
388         rpass->recty = rl->recty;
389         BLI_strncpy(rpass->name, get_pass_name(rpass->passtype, -1), sizeof(rpass->name));
390         
391         if (rl->exrhandle) {
392                 int a;
393                 for (a = 0; a < channels; a++)
394                         IMB_exr_add_channel(rl->exrhandle, rl->name, get_pass_name(passtype, a), 0, 0, NULL);
395         }
396         else {
397                 float *rect;
398                 int x;
399                 
400                 rpass->rect = MEM_mapallocN(sizeof(float) * rectsize, typestr);
401                 
402                 if (passtype == SCE_PASS_VECTOR) {
403                         /* initialize to max speed */
404                         rect = rpass->rect;
405                         for (x = rectsize - 1; x >= 0; x--)
406                                 rect[x] = PASS_VECTOR_MAX;
407                 }
408                 else if (passtype == SCE_PASS_Z) {
409                         rect = rpass->rect;
410                         for (x = rectsize - 1; x >= 0; x--)
411                                 rect[x] = 10e10;
412                 }
413         }
414 }
415
416 /* called by main render as well for parts */
417 /* will read info from Render *re to define layers */
418 /* called in threads */
419 /* re->winx,winy is coordinate space of entire image, partrct the part within */
420 RenderResult *render_result_new(Render *re, rcti *partrct, int crop, int savebuffers, const char *layername)
421 {
422         RenderResult *rr;
423         RenderLayer *rl;
424         SceneRenderLayer *srl;
425         int rectx, recty, nr;
426         
427         rectx = BLI_rcti_size_x(partrct);
428         recty = BLI_rcti_size_y(partrct);
429         
430         if (rectx <= 0 || recty <= 0)
431                 return NULL;
432         
433         rr = MEM_callocN(sizeof(RenderResult), "new render result");
434         rr->rectx = rectx;
435         rr->recty = recty;
436         rr->renrect.xmin = 0; rr->renrect.xmax = rectx - 2 * crop;
437         /* crop is one or two extra pixels rendered for filtering, is used for merging and display too */
438         rr->crop = crop;
439         
440         /* tilerect is relative coordinates within render disprect. do not subtract crop yet */
441         rr->tilerect.xmin = partrct->xmin - re->disprect.xmin;
442         rr->tilerect.xmax = partrct->xmax - re->disprect.xmin;
443         rr->tilerect.ymin = partrct->ymin - re->disprect.ymin;
444         rr->tilerect.ymax = partrct->ymax - re->disprect.ymin;
445         
446         if (savebuffers) {
447                 rr->do_exr_tile = TRUE;
448         }
449
450         /* check renderdata for amount of layers */
451         for (nr = 0, srl = re->r.layers.first; srl; srl = srl->next, nr++) {
452
453                 if (layername && layername[0])
454                         if (strcmp(srl->name, layername) != 0)
455                                 continue;
456
457                 if ((re->r.scemode & R_SINGLE_LAYER) && nr != re->r.actlay)
458                         continue;
459                 if (srl->layflag & SCE_LAY_DISABLE)
460                         continue;
461                 
462                 rl = MEM_callocN(sizeof(RenderLayer), "new render layer");
463                 BLI_addtail(&rr->layers, rl);
464                 
465                 BLI_strncpy(rl->name, srl->name, sizeof(rl->name));
466                 rl->lay = srl->lay;
467                 rl->lay_zmask = srl->lay_zmask;
468                 rl->lay_exclude = srl->lay_exclude;
469                 rl->layflag = srl->layflag;
470                 rl->passflag = srl->passflag; /* for debugging: srl->passflag | SCE_PASS_RAYHITS; */
471                 rl->pass_xor = srl->pass_xor;
472                 rl->light_override = srl->light_override;
473                 rl->mat_override = srl->mat_override;
474                 rl->rectx = rectx;
475                 rl->recty = recty;
476                 
477                 if (rr->do_exr_tile) {
478                         rl->exrhandle = IMB_exr_get_handle();
479
480                         IMB_exr_add_channel(rl->exrhandle, rl->name, "Combined.R", 0, 0, NULL);
481                         IMB_exr_add_channel(rl->exrhandle, rl->name, "Combined.G", 0, 0, NULL);
482                         IMB_exr_add_channel(rl->exrhandle, rl->name, "Combined.B", 0, 0, NULL);
483                         IMB_exr_add_channel(rl->exrhandle, rl->name, "Combined.A", 0, 0, NULL);
484                 }
485                 else
486                         rl->rectf = MEM_mapallocN(rectx * recty * sizeof(float) * 4, "Combined rgba");
487                 
488                 if (srl->passflag  & SCE_PASS_Z)
489                         render_layer_add_pass(rr, rl, 1, SCE_PASS_Z);
490                 if (srl->passflag  & SCE_PASS_VECTOR)
491                         render_layer_add_pass(rr, rl, 4, SCE_PASS_VECTOR);
492                 if (srl->passflag  & SCE_PASS_NORMAL)
493                         render_layer_add_pass(rr, rl, 3, SCE_PASS_NORMAL);
494                 if (srl->passflag  & SCE_PASS_UV) 
495                         render_layer_add_pass(rr, rl, 3, SCE_PASS_UV);
496                 if (srl->passflag  & SCE_PASS_RGBA)
497                         render_layer_add_pass(rr, rl, 4, SCE_PASS_RGBA);
498                 if (srl->passflag  & SCE_PASS_EMIT)
499                         render_layer_add_pass(rr, rl, 3, SCE_PASS_EMIT);
500                 if (srl->passflag  & SCE_PASS_DIFFUSE)
501                         render_layer_add_pass(rr, rl, 3, SCE_PASS_DIFFUSE);
502                 if (srl->passflag  & SCE_PASS_SPEC)
503                         render_layer_add_pass(rr, rl, 3, SCE_PASS_SPEC);
504                 if (srl->passflag  & SCE_PASS_AO)
505                         render_layer_add_pass(rr, rl, 3, SCE_PASS_AO);
506                 if (srl->passflag  & SCE_PASS_ENVIRONMENT)
507                         render_layer_add_pass(rr, rl, 3, SCE_PASS_ENVIRONMENT);
508                 if (srl->passflag  & SCE_PASS_INDIRECT)
509                         render_layer_add_pass(rr, rl, 3, SCE_PASS_INDIRECT);
510                 if (srl->passflag  & SCE_PASS_SHADOW)
511                         render_layer_add_pass(rr, rl, 3, SCE_PASS_SHADOW);
512                 if (srl->passflag  & SCE_PASS_REFLECT)
513                         render_layer_add_pass(rr, rl, 3, SCE_PASS_REFLECT);
514                 if (srl->passflag  & SCE_PASS_REFRACT)
515                         render_layer_add_pass(rr, rl, 3, SCE_PASS_REFRACT);
516                 if (srl->passflag  & SCE_PASS_INDEXOB)
517                         render_layer_add_pass(rr, rl, 1, SCE_PASS_INDEXOB);
518                 if (srl->passflag  & SCE_PASS_INDEXMA)
519                         render_layer_add_pass(rr, rl, 1, SCE_PASS_INDEXMA);
520                 if (srl->passflag  & SCE_PASS_MIST)
521                         render_layer_add_pass(rr, rl, 1, SCE_PASS_MIST);
522                 if (rl->passflag & SCE_PASS_RAYHITS)
523                         render_layer_add_pass(rr, rl, 4, SCE_PASS_RAYHITS);
524                 if (srl->passflag  & SCE_PASS_DIFFUSE_DIRECT)
525                         render_layer_add_pass(rr, rl, 3, SCE_PASS_DIFFUSE_DIRECT);
526                 if (srl->passflag  & SCE_PASS_DIFFUSE_INDIRECT)
527                         render_layer_add_pass(rr, rl, 3, SCE_PASS_DIFFUSE_INDIRECT);
528                 if (srl->passflag  & SCE_PASS_DIFFUSE_COLOR)
529                         render_layer_add_pass(rr, rl, 3, SCE_PASS_DIFFUSE_COLOR);
530                 if (srl->passflag  & SCE_PASS_GLOSSY_DIRECT)
531                         render_layer_add_pass(rr, rl, 3, SCE_PASS_GLOSSY_DIRECT);
532                 if (srl->passflag  & SCE_PASS_GLOSSY_INDIRECT)
533                         render_layer_add_pass(rr, rl, 3, SCE_PASS_GLOSSY_INDIRECT);
534                 if (srl->passflag  & SCE_PASS_GLOSSY_COLOR)
535                         render_layer_add_pass(rr, rl, 3, SCE_PASS_GLOSSY_COLOR);
536                 if (srl->passflag  & SCE_PASS_TRANSM_DIRECT)
537                         render_layer_add_pass(rr, rl, 3, SCE_PASS_TRANSM_DIRECT);
538                 if (srl->passflag  & SCE_PASS_TRANSM_INDIRECT)
539                         render_layer_add_pass(rr, rl, 3, SCE_PASS_TRANSM_INDIRECT);
540                 if (srl->passflag  & SCE_PASS_TRANSM_COLOR)
541                         render_layer_add_pass(rr, rl, 3, SCE_PASS_TRANSM_COLOR);
542                 
543         }
544         /* sss, previewrender and envmap don't do layers, so we make a default one */
545         if (rr->layers.first == NULL && !(layername && layername[0])) {
546                 rl = MEM_callocN(sizeof(RenderLayer), "new render layer");
547                 BLI_addtail(&rr->layers, rl);
548                 
549                 rl->rectx = rectx;
550                 rl->recty = recty;
551
552                 /* duplicate code... */
553                 if (rr->do_exr_tile) {
554                         rl->exrhandle = IMB_exr_get_handle();
555
556                         IMB_exr_add_channel(rl->exrhandle, rl->name, "Combined.R", 0, 0, NULL);
557                         IMB_exr_add_channel(rl->exrhandle, rl->name, "Combined.G", 0, 0, NULL);
558                         IMB_exr_add_channel(rl->exrhandle, rl->name, "Combined.B", 0, 0, NULL);
559                         IMB_exr_add_channel(rl->exrhandle, rl->name, "Combined.A", 0, 0, NULL);
560                 }
561                 else {
562                         rl->rectf = MEM_mapallocN(rectx * recty * sizeof(float) * 4, "Combined rgba");
563                 }
564                 
565                 /* note, this has to be in sync with scene.c */
566                 rl->lay = (1 << 20) - 1;
567                 rl->layflag = 0x7FFF;    /* solid ztra halo strand */
568                 rl->passflag = SCE_PASS_COMBINED;
569                 FRS_add_freestyle_config( srl );
570                 
571                 re->r.actlay = 0;
572         }
573         
574         /* border render; calculate offset for use in compositor. compo is centralized coords */
575         rr->xof = re->disprect.xmin + BLI_rcti_cent_x(&re->disprect) - (re->winx / 2);
576         rr->yof = re->disprect.ymin + BLI_rcti_cent_y(&re->disprect) - (re->winy / 2);
577         
578         return rr;
579 }
580
581 /* allocate osa new results for samples */
582 RenderResult *render_result_new_full_sample(Render *re, ListBase *lb, rcti *partrct, int crop, int savebuffers)
583 {
584         int a;
585         
586         if (re->osa == 0)
587                 return render_result_new(re, partrct, crop, savebuffers, RR_ALL_LAYERS);
588         
589         for (a = 0; a < re->osa; a++) {
590                 RenderResult *rr = render_result_new(re, partrct, crop, savebuffers, RR_ALL_LAYERS);
591                 BLI_addtail(lb, rr);
592                 rr->sample_nr = a;
593         }
594         
595         return lb->first;
596 }
597
598 /* callbacks for render_result_new_from_exr */
599 static void *ml_addlayer_cb(void *base, const char *str)
600 {
601         RenderResult *rr = base;
602         RenderLayer *rl;
603         
604         rl = MEM_callocN(sizeof(RenderLayer), "new render layer");
605         BLI_addtail(&rr->layers, rl);
606         
607         BLI_strncpy(rl->name, str, EXR_LAY_MAXNAME);
608         return rl;
609 }
610
611 static void ml_addpass_cb(void *UNUSED(base), void *lay, const char *str, float *rect, int totchan, const char *chan_id)
612 {
613         RenderLayer *rl = lay;
614         RenderPass *rpass = MEM_callocN(sizeof(RenderPass), "loaded pass");
615         int a;
616         
617         BLI_addtail(&rl->passes, rpass);
618         rpass->channels = totchan;
619
620         rpass->passtype = passtype_from_name(str);
621         if (rpass->passtype == 0) printf("unknown pass %s\n", str);
622         rl->passflag |= rpass->passtype;
623         
624         BLI_strncpy(rpass->name, str, EXR_PASS_MAXNAME);
625         /* channel id chars */
626         for (a = 0; a < totchan; a++)
627                 rpass->chan_id[a] = chan_id[a];
628         
629         rpass->rect = rect;
630 }
631
632 /* from imbuf, if a handle was returned we convert this to render result */
633 RenderResult *render_result_new_from_exr(void *exrhandle, const char *colorspace, int predivide, int rectx, int recty)
634 {
635         RenderResult *rr = MEM_callocN(sizeof(RenderResult), __func__);
636         RenderLayer *rl;
637         RenderPass *rpass;
638         const char *to_colorspace = IMB_colormanagement_role_colorspace_name_get(COLOR_ROLE_SCENE_LINEAR);
639
640         rr->rectx = rectx;
641         rr->recty = recty;
642         
643         IMB_exr_multilayer_convert(exrhandle, rr, ml_addlayer_cb, ml_addpass_cb);
644
645         for (rl = rr->layers.first; rl; rl = rl->next) {
646                 rl->rectx = rectx;
647                 rl->recty = recty;
648
649                 for (rpass = rl->passes.first; rpass; rpass = rpass->next) {
650                         rpass->rectx = rectx;
651                         rpass->recty = recty;
652
653                         if (rpass->channels >= 3) {
654                                 IMB_colormanagement_transform(rpass->rect, rpass->rectx, rpass->recty, rpass->channels,
655                                                               colorspace, to_colorspace, predivide);
656                         }
657                 }
658         }
659         
660         return rr;
661 }
662
663 /*********************************** Merge ***********************************/
664
665 static void do_merge_tile(RenderResult *rr, RenderResult *rrpart, float *target, float *tile, int pixsize)
666 {
667         int y, ofs, copylen, tilex, tiley;
668         
669         copylen = tilex = rrpart->rectx;
670         tiley = rrpart->recty;
671         
672         if (rrpart->crop) { /* filters add pixel extra */
673                 tile += pixsize * (rrpart->crop + rrpart->crop * tilex);
674                 
675                 copylen = tilex - 2 * rrpart->crop;
676                 tiley -= 2 * rrpart->crop;
677                 
678                 ofs = (rrpart->tilerect.ymin + rrpart->crop) * rr->rectx + (rrpart->tilerect.xmin + rrpart->crop);
679                 target += pixsize * ofs;
680         }
681         else {
682                 ofs = (rrpart->tilerect.ymin * rr->rectx + rrpart->tilerect.xmin);
683                 target += pixsize * ofs;
684         }
685
686         copylen *= sizeof(float) * pixsize;
687         tilex *= pixsize;
688         ofs = pixsize * rr->rectx;
689
690         for (y = 0; y < tiley; y++) {
691                 memcpy(target, tile, copylen);
692                 target += ofs;
693                 tile += tilex;
694         }
695 }
696
697 /* used when rendering to a full buffer, or when reading the exr part-layer-pass file */
698 /* no test happens here if it fits... we also assume layers are in sync */
699 /* is used within threads */
700 void render_result_merge(RenderResult *rr, RenderResult *rrpart)
701 {
702         RenderLayer *rl, *rlp;
703         RenderPass *rpass, *rpassp;
704         
705         for (rl = rr->layers.first; rl; rl = rl->next) {
706                 rlp = RE_GetRenderLayer(rrpart, rl->name);
707                 if (rlp) {
708                         /* combined */
709                         if (rl->rectf && rlp->rectf)
710                                 do_merge_tile(rr, rrpart, rl->rectf, rlp->rectf, 4);
711
712                         /* passes are allocated in sync */
713                         for (rpass = rl->passes.first, rpassp = rlp->passes.first;
714                              rpass && rpassp;
715                              rpass = rpass->next, rpassp = rpassp->next)
716                         {
717                                 do_merge_tile(rr, rrpart, rpass->rect, rpassp->rect, rpass->channels);
718                         }
719                 }
720         }
721 }
722
723 /* for passes read from files, these have names stored */
724 static char *make_pass_name(RenderPass *rpass, int chan)
725 {
726         static char name[16];
727         int len;
728         
729         BLI_strncpy(name, rpass->name, EXR_PASS_MAXNAME);
730         len = strlen(name);
731         name[len] = '.';
732         name[len + 1] = rpass->chan_id[chan];
733         name[len + 2] = 0;
734
735         return name;
736 }
737
738 /* filename already made absolute */
739 /* called from within UI, saves both rendered result as a file-read result */
740 int RE_WriteRenderResult(ReportList *reports, RenderResult *rr, const char *filename, int compress)
741 {
742         RenderLayer *rl;
743         RenderPass *rpass;
744         void *exrhandle = IMB_exr_get_handle();
745         int success;
746
747         BLI_make_existing_file(filename);
748         
749         /* composite result */
750         if (rr->rectf) {
751                 IMB_exr_add_channel(exrhandle, "Composite", "Combined.R", 4, 4 * rr->rectx, rr->rectf);
752                 IMB_exr_add_channel(exrhandle, "Composite", "Combined.G", 4, 4 * rr->rectx, rr->rectf + 1);
753                 IMB_exr_add_channel(exrhandle, "Composite", "Combined.B", 4, 4 * rr->rectx, rr->rectf + 2);
754                 IMB_exr_add_channel(exrhandle, "Composite", "Combined.A", 4, 4 * rr->rectx, rr->rectf + 3);
755         }
756         
757         /* add layers/passes and assign channels */
758         for (rl = rr->layers.first; rl; rl = rl->next) {
759                 
760                 /* combined */
761                 if (rl->rectf) {
762                         int a, xstride = 4;
763                         for (a = 0; a < xstride; a++) {
764                                 IMB_exr_add_channel(exrhandle, rl->name, get_pass_name(SCE_PASS_COMBINED, a),
765                                                     xstride, xstride * rr->rectx, rl->rectf + a);
766                         }
767                 }
768                 
769                 /* passes are allocated in sync */
770                 for (rpass = rl->passes.first; rpass; rpass = rpass->next) {
771                         int a, xstride = rpass->channels;
772                         for (a = 0; a < xstride; a++) {
773                                 if (rpass->passtype) {
774                                         IMB_exr_add_channel(exrhandle, rl->name, get_pass_name(rpass->passtype, a),
775                                                             xstride, xstride * rr->rectx, rpass->rect + a);
776                                 }
777                                 else {
778                                         IMB_exr_add_channel(exrhandle, rl->name, make_pass_name(rpass, a),
779                                                             xstride, xstride * rr->rectx, rpass->rect + a);
780                                 }
781                         }
782                 }
783         }
784
785         /* when the filename has no permissions, this can fail */
786         if (IMB_exr_begin_write(exrhandle, filename, rr->rectx, rr->recty, compress)) {
787                 IMB_exr_write_channels(exrhandle);
788                 success = TRUE;
789         }
790         else {
791                 /* TODO, get the error from openexr's exception */
792                 BKE_report(reports, RPT_ERROR, "Error writing render result (see console)");
793                 success = FALSE;
794         }
795         IMB_exr_close(exrhandle);
796
797         return success;
798 }
799
800 /**************************** Single Layer Rendering *************************/
801
802 void render_result_single_layer_begin(Render *re)
803 {
804         /* all layers except the active one get temporally pushed away */
805
806         /* officially pushed result should be NULL... error can happen with do_seq */
807         RE_FreeRenderResult(re->pushedresult);
808         
809         re->pushedresult = re->result;
810         re->result = NULL;
811 }
812
813 /* if scemode is R_SINGLE_LAYER, at end of rendering, merge the both render results */
814 void render_result_single_layer_end(Render *re)
815 {
816         SceneRenderLayer *srl;
817         RenderLayer *rlpush;
818         RenderLayer *rl;
819         int nr;
820
821         if (re->result == NULL) {
822                 printf("pop render result error; no current result!\n");
823                 return;
824         }
825
826         if (!re->pushedresult)
827                 return;
828
829         if (re->pushedresult->rectx == re->result->rectx && re->pushedresult->recty == re->result->recty) {
830                 /* find which layer in re->pushedresult should be replaced */
831                 rl = re->result->layers.first;
832                 
833                 /* render result should be empty after this */
834                 BLI_remlink(&re->result->layers, rl);
835                 
836                 /* reconstruct render result layers */
837                 for (nr = 0, srl = re->scene->r.layers.first; srl; srl = srl->next, nr++) {
838                         if (nr == re->r.actlay) {
839                                 BLI_addtail(&re->result->layers, rl);
840                         }
841                         else {
842                                 rlpush = RE_GetRenderLayer(re->pushedresult, srl->name);
843                                 if (rlpush) {
844                                         BLI_remlink(&re->pushedresult->layers, rlpush);
845                                         BLI_addtail(&re->result->layers, rlpush);
846                                 }
847                         }
848                 }
849         }
850
851         RE_FreeRenderResult(re->pushedresult);
852         re->pushedresult = NULL;
853 }
854
855 /************************* EXR Tile File Rendering ***************************/
856
857 static void save_render_result_tile(RenderResult *rr, RenderResult *rrpart)
858 {
859         RenderLayer *rlp, *rl;
860         RenderPass *rpassp;
861         int offs, partx, party;
862         
863         BLI_lock_thread(LOCK_IMAGE);
864         
865         for (rlp = rrpart->layers.first; rlp; rlp = rlp->next) {
866                 rl = RE_GetRenderLayer(rr, rlp->name);
867
868                 /* should never happen but prevents crash if it does */
869                 BLI_assert(rl);
870                 if (UNLIKELY(rl == NULL)) {
871                         continue;
872                 }
873
874                 if (rrpart->crop) { /* filters add pixel extra */
875                         offs = (rrpart->crop + rrpart->crop * rrpart->rectx);
876                 }
877                 else {
878                         offs = 0;
879                 }
880                 
881                 /* combined */
882                 if (rlp->rectf) {
883                         int a, xstride = 4;
884                         for (a = 0; a < xstride; a++) {
885                                 IMB_exr_set_channel(rl->exrhandle, rlp->name, get_pass_name(SCE_PASS_COMBINED, a), 
886                                                     xstride, xstride * rrpart->rectx, rlp->rectf + a + xstride * offs);
887                         }
888                 }
889                 
890                 /* passes are allocated in sync */
891                 for (rpassp = rlp->passes.first; rpassp; rpassp = rpassp->next) {
892                         int a, xstride = rpassp->channels;
893                         for (a = 0; a < xstride; a++) {
894                                 IMB_exr_set_channel(rl->exrhandle, rlp->name, get_pass_name(rpassp->passtype, a), 
895                                                     xstride, xstride * rrpart->rectx, rpassp->rect + a + xstride * offs);
896                         }
897                 }
898                 
899         }
900
901         party = rrpart->tilerect.ymin + rrpart->crop;
902         partx = rrpart->tilerect.xmin + rrpart->crop;
903
904         for (rlp = rrpart->layers.first; rlp; rlp = rlp->next) {
905                 rl = RE_GetRenderLayer(rr, rlp->name);
906
907                 /* should never happen but prevents crash if it does */
908                 BLI_assert(rl);
909                 if (UNLIKELY(rl == NULL)) {
910                         continue;
911                 }
912
913                 IMB_exrtile_write_channels(rl->exrhandle, partx, party, 0);
914         }
915
916         BLI_unlock_thread(LOCK_IMAGE);
917 }
918
919 static void save_empty_result_tiles(Render *re)
920 {
921         RenderPart *pa;
922         RenderResult *rr;
923         RenderLayer *rl;
924         
925         for (rr = re->result; rr; rr = rr->next) {
926                 for (rl = rr->layers.first; rl; rl = rl->next) {
927                         IMB_exrtile_clear_channels(rl->exrhandle);
928                 
929                         for (pa = re->parts.first; pa; pa = pa->next) {
930                                 if (pa->ready == 0) {
931                                         int party = pa->disprect.ymin - re->disprect.ymin + pa->crop;
932                                         int partx = pa->disprect.xmin - re->disprect.xmin + pa->crop;
933                                         IMB_exrtile_write_channels(rl->exrhandle, partx, party, 0);
934                                 }
935                         }
936                 }
937         }
938 }
939
940 /* begin write of exr tile file */
941 void render_result_exr_file_begin(Render *re)
942 {
943         RenderResult *rr;
944         RenderLayer *rl;
945         char str[FILE_MAX];
946
947         for (rr = re->result; rr; rr = rr->next) {
948                 for (rl = rr->layers.first; rl; rl = rl->next) {
949                         render_result_exr_file_path(re->scene, rl->name, rr->sample_nr, str);
950                         printf("write exr tmp file, %dx%d, %s\n", rr->rectx, rr->recty, str);
951                         IMB_exrtile_begin_write(rl->exrhandle, str, 0, rr->rectx, rr->recty, re->partx, re->party);
952                 }
953         }
954 }
955
956 /* end write of exr tile file, read back first sample */
957 void render_result_exr_file_end(Render *re)
958 {
959         RenderResult *rr;
960         RenderLayer *rl;
961
962         save_empty_result_tiles(re);
963         
964         for (rr = re->result; rr; rr = rr->next) {
965                 for (rl = rr->layers.first; rl; rl = rl->next) {
966                         IMB_exr_close(rl->exrhandle);
967                         rl->exrhandle = NULL;
968                 }
969
970                 rr->do_exr_tile = FALSE;
971         }
972         
973         render_result_free_list(&re->fullresult, re->result);
974         re->result = NULL;
975
976         render_result_exr_file_read(re, 0);
977 }
978
979 /* save part into exr file */
980 void render_result_exr_file_merge(RenderResult *rr, RenderResult *rrpart)
981 {
982         for (; rr && rrpart; rr = rr->next, rrpart = rrpart->next)
983                 save_render_result_tile(rr, rrpart);
984 }
985
986 /* path to temporary exr file */
987 void render_result_exr_file_path(Scene *scene, const char *layname, int sample, char *filepath)
988 {
989         char di[FILE_MAX], name[FILE_MAXFILE + MAX_ID_NAME + MAX_ID_NAME + 100], fi[FILE_MAXFILE];
990         
991         BLI_strncpy(di, G.main->name, FILE_MAX);
992         BLI_splitdirstring(di, fi);
993         
994         if (sample == 0)
995                 BLI_snprintf(name, sizeof(name), "%s_%s_%s.exr", fi, scene->id.name + 2, layname);
996         else
997                 BLI_snprintf(name, sizeof(name), "%s_%s_%s%d.exr", fi, scene->id.name + 2, layname, sample);
998
999         BLI_make_file_string("/", filepath, BLI_temporary_dir(), name);
1000 }
1001
1002 /* only for temp buffer files, makes exact copy of render result */
1003 int render_result_exr_file_read(Render *re, int sample)
1004 {
1005         RenderLayer *rl;
1006         char str[FILE_MAX];
1007         int success = TRUE;
1008
1009         RE_FreeRenderResult(re->result);
1010         re->result = render_result_new(re, &re->disprect, 0, RR_USE_MEM, RR_ALL_LAYERS);
1011
1012         for (rl = re->result->layers.first; rl; rl = rl->next) {
1013
1014                 render_result_exr_file_path(re->scene, rl->name, sample, str);
1015                 printf("read exr tmp file: %s\n", str);
1016
1017                 if (!render_result_exr_file_read_path(re->result, rl, str)) {
1018                         printf("cannot read: %s\n", str);
1019                         success = FALSE;
1020
1021                 }
1022         }
1023
1024         return success;
1025 }
1026
1027 /* called for reading temp files, and for external engines */
1028 int render_result_exr_file_read_path(RenderResult *rr, RenderLayer *rl_single, const char *filepath)
1029 {
1030         RenderLayer *rl;
1031         RenderPass *rpass;
1032         void *exrhandle = IMB_exr_get_handle();
1033         int rectx, recty;
1034
1035         if (IMB_exr_begin_read(exrhandle, filepath, &rectx, &recty) == 0) {
1036                 printf("failed being read %s\n", filepath);
1037                 IMB_exr_close(exrhandle);
1038                 return 0;
1039         }
1040
1041         if (rr == NULL || rectx != rr->rectx || recty != rr->recty) {
1042                 if (rr)
1043                         printf("error in reading render result: dimensions don't match\n");
1044                 else
1045                         printf("error in reading render result: NULL result pointer\n");
1046                 IMB_exr_close(exrhandle);
1047                 return 0;
1048         }
1049
1050         for (rl = rr->layers.first; rl; rl = rl->next) {
1051                 if (rl_single && rl_single != rl)
1052                         continue;
1053
1054                 /* combined */
1055                 if (rl->rectf) {
1056                         int a, xstride = 4;
1057                         for (a = 0; a < xstride; a++)
1058                                 IMB_exr_set_channel(exrhandle, rl->name, get_pass_name(SCE_PASS_COMBINED, a), 
1059                                                     xstride, xstride * rectx, rl->rectf + a);
1060                 }
1061                 
1062                 /* passes are allocated in sync */
1063                 for (rpass = rl->passes.first; rpass; rpass = rpass->next) {
1064                         int a, xstride = rpass->channels;
1065                         for (a = 0; a < xstride; a++)
1066                                 IMB_exr_set_channel(exrhandle, rl->name, get_pass_name(rpass->passtype, a), 
1067                                                     xstride, xstride * rectx, rpass->rect + a);
1068
1069                         BLI_strncpy(rpass->name, get_pass_name(rpass->passtype, -1), sizeof(rpass->name));
1070                 }
1071         }
1072
1073         IMB_exr_read_channels(exrhandle);
1074         IMB_exr_close(exrhandle);
1075
1076         return 1;
1077 }
1078
1079 /*************************** Combined Pixel Rect *****************************/
1080
1081 ImBuf *render_result_rect_to_ibuf(RenderResult *rr, RenderData *rd)
1082 {
1083         int flags = (rd->color_mgt_flag & R_COLOR_MANAGEMENT_PREDIVIDE) ? IB_cm_predivide : 0;
1084         ImBuf *ibuf = IMB_allocImBuf(rr->rectx, rr->recty, rd->im_format.planes, flags);
1085         
1086         /* if not exists, BKE_imbuf_write makes one */
1087         ibuf->rect = (unsigned int *)rr->rect32;
1088         ibuf->rect_float = rr->rectf;
1089         ibuf->zbuf_float = rr->rectz;
1090         
1091         /* float factor for random dither, imbuf takes care of it */
1092         ibuf->dither = rd->dither_intensity;
1093         
1094         /* prepare to gamma correct to sRGB color space
1095          * note that sequence editor can generate 8bpc render buffers
1096          */
1097         if (ibuf->rect) {
1098                 if (BKE_imtype_valid_depths(rd->im_format.imtype) & (R_IMF_CHAN_DEPTH_12 | R_IMF_CHAN_DEPTH_16 | R_IMF_CHAN_DEPTH_24 | R_IMF_CHAN_DEPTH_32)) {
1099                         IMB_float_from_rect(ibuf);
1100                 }
1101         }
1102
1103         /* color -> grayscale */
1104         /* editing directly would alter the render view */
1105         if (rd->im_format.planes == R_IMF_PLANES_BW) {
1106                 ImBuf *ibuf_bw = IMB_dupImBuf(ibuf);
1107                 IMB_color_to_bw(ibuf_bw);
1108                 IMB_freeImBuf(ibuf);
1109                 ibuf = ibuf_bw;
1110         }
1111
1112         return ibuf;
1113 }
1114
1115 void render_result_rect_from_ibuf(RenderResult *rr, RenderData *UNUSED(rd), ImBuf *ibuf)
1116 {
1117         if (ibuf->rect_float) {
1118                 if (!rr->rectf)
1119                         rr->rectf = MEM_mallocN(4 * sizeof(float) * rr->rectx * rr->recty, "render_seq rectf");
1120                 
1121                 memcpy(rr->rectf, ibuf->rect_float, 4 * sizeof(float) * rr->rectx * rr->recty);
1122
1123                 /* TSK! Since sequence render doesn't free the *rr render result, the old rect32
1124                  * can hang around when sequence render has rendered a 32 bits one before */
1125                 if (rr->rect32) {
1126                         MEM_freeN(rr->rect32);
1127                         rr->rect32 = NULL;
1128                 }
1129         }
1130         else if (ibuf->rect) {
1131                 if (!rr->rect32)
1132                         rr->rect32 = MEM_mallocN(sizeof(int) * rr->rectx * rr->recty, "render_seq rect");
1133
1134                 memcpy(rr->rect32, ibuf->rect, 4 * rr->rectx * rr->recty);
1135
1136                 /* Same things as above, old rectf can hang around from previous render. */
1137                 if (rr->rectf) {
1138                         MEM_freeN(rr->rectf);
1139                         rr->rectf = NULL;
1140                 }
1141         }
1142 }
1143
1144 void render_result_rect_fill_zero(RenderResult *rr)
1145 {
1146         if (rr->rectf)
1147                 memset(rr->rectf, 0, 4 * sizeof(float) * rr->rectx * rr->recty);
1148         else if (rr->rect32)
1149                 memset(rr->rect32, 0, 4 * rr->rectx * rr->recty);
1150         else
1151                 rr->rect32 = MEM_callocN(sizeof(int) * rr->rectx * rr->recty, "render_seq rect");
1152 }
1153
1154 void render_result_rect_get_pixels(RenderResult *rr, RenderData *rd, unsigned int *rect, int rectx, int recty,
1155                                    const ColorManagedViewSettings *view_settings, const ColorManagedDisplaySettings *display_settings)
1156 {
1157         if (rr->rect32) {
1158                 memcpy(rect, rr->rect32, sizeof(int) * rr->rectx * rr->recty);
1159         }
1160         else if (rr->rectf) {
1161                 int predivide = (rd->color_mgt_flag & R_COLOR_MANAGEMENT_PREDIVIDE);
1162
1163                 IMB_display_buffer_transform_apply((unsigned char *) rect, rr->rectf, rr->rectx, rr->recty, 4,
1164                                                    view_settings, display_settings, predivide);
1165         }
1166         else
1167                 /* else fill with black */
1168                 memset(rect, 0, sizeof(int) * rectx * recty);
1169 }
1170