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