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