Merge branch 'master' into blender2.8
[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 #include <errno.h>
36
37 #include "MEM_guardedalloc.h"
38
39 #include "BLI_utildefines.h"
40 #include "BLI_listbase.h"
41 #include "BLI_hash_md5.h"
42 #include "BLI_path_util.h"
43 #include "BLI_rect.h"
44 #include "BLI_string.h"
45 #include "BLI_threads.h"
46
47 #include "BKE_appdir.h"
48 #include "BKE_image.h"
49 #include "BKE_global.h"
50 #include "BKE_main.h"
51 #include "BKE_report.h"
52 #include "BKE_camera.h"
53 #include "BKE_scene.h"
54
55 #include "IMB_imbuf.h"
56 #include "IMB_imbuf_types.h"
57 #include "IMB_colormanagement.h"
58
59 #include "intern/openexr/openexr_multi.h"
60
61 #include "render_result.h"
62 #include "render_types.h"
63
64 /********************************** Free *************************************/
65
66 static void render_result_views_free(RenderResult *res)
67 {
68         while (res->views.first) {
69                 RenderView *rv = res->views.first;
70                 BLI_remlink(&res->views, rv);
71
72                 if (rv->rect32)
73                         MEM_freeN(rv->rect32);
74
75                 if (rv->rectz)
76                         MEM_freeN(rv->rectz);
77
78                 if (rv->rectf)
79                         MEM_freeN(rv->rectf);
80
81                 MEM_freeN(rv);
82         }
83
84         res->have_combined = false;
85 }
86
87 void render_result_free(RenderResult *res)
88 {
89         if (res == NULL) return;
90
91         while (res->layers.first) {
92                 RenderLayer *rl = res->layers.first;
93
94                 /* acolrect and scolrect are optionally allocated in shade_tile, only free here since it can be used for drawing */
95                 if (rl->acolrect) MEM_freeN(rl->acolrect);
96                 if (rl->scolrect) MEM_freeN(rl->scolrect);
97                 if (rl->display_buffer) MEM_freeN(rl->display_buffer);
98                 
99                 while (rl->passes.first) {
100                         RenderPass *rpass = rl->passes.first;
101                         if (rpass->rect) MEM_freeN(rpass->rect);
102                         BLI_remlink(&rl->passes, rpass);
103                         MEM_freeN(rpass);
104                 }
105                 BLI_remlink(&res->layers, rl);
106                 MEM_freeN(rl);
107         }
108
109         render_result_views_free(res);
110
111         if (res->rect32)
112                 MEM_freeN(res->rect32);
113         if (res->rectz)
114                 MEM_freeN(res->rectz);
115         if (res->rectf)
116                 MEM_freeN(res->rectf);
117         if (res->text)
118                 MEM_freeN(res->text);
119         if (res->error)
120                 MEM_freeN(res->error);
121
122         BKE_stamp_data_free(res->stamp_data);
123
124         MEM_freeN(res);
125 }
126
127 /* version that's compatible with fullsample buffers */
128 void render_result_free_list(ListBase *lb, RenderResult *rr)
129 {
130         RenderResult *rrnext;
131         
132         for (; rr; rr = rrnext) {
133                 rrnext = rr->next;
134                 
135                 if (lb && lb->first)
136                         BLI_remlink(lb, rr);
137                 
138                 render_result_free(rr);
139         }
140 }
141
142 /********************************* multiview *************************************/
143
144 /* create a new views Listbase in rr without duplicating the memory pointers */
145 void render_result_views_shallowcopy(RenderResult *dst, RenderResult *src)
146 {
147         RenderView *rview;
148
149         if (dst == NULL || src == NULL)
150                 return;
151
152         for (rview = src->views.first; rview; rview = rview->next) {
153                 RenderView *rv;
154
155                 rv = MEM_mallocN(sizeof(RenderView), "new render view");
156                 BLI_addtail(&dst->views, rv);
157
158                 BLI_strncpy(rv->name, rview->name, sizeof(rv->name));
159                 rv->rectf = rview->rectf;
160                 rv->rectz = rview->rectz;
161                 rv->rect32 = rview->rect32;
162         }
163 }
164
165 /* free the views created temporarily */
166 void render_result_views_shallowdelete(RenderResult *rr)
167 {
168         if (rr == NULL)
169                 return;
170
171         while (rr->views.first) {
172                 RenderView *rv = rr->views.first;
173                 BLI_remlink(&rr->views, rv);
174                 MEM_freeN(rv);
175         }
176 }
177
178
179 static char* set_pass_name(char *outname, const char *name, int channel, const char *chan_id)
180 {
181         BLI_strncpy(outname, name, EXR_PASS_MAXNAME);
182         if (channel >= 0) {
183                 char token[3] = {'.', chan_id[channel], '\0'};
184                 strncat(outname, token, EXR_PASS_MAXNAME);
185         }
186         return outname;
187 }
188
189 static void set_pass_full_name(char *fullname, const char *name, int channel, const char *view, const char *chan_id)
190 {
191         BLI_strncpy(fullname, name, EXR_PASS_MAXNAME);
192         if (view && view[0]) {
193                 strncat(fullname, ".", EXR_PASS_MAXNAME);
194                 strncat(fullname, view, EXR_PASS_MAXNAME);
195         }
196         if (channel >= 0) {
197                 char token[3] = {'.', chan_id[channel], '\0'};
198                 strncat(fullname, token, EXR_PASS_MAXNAME);
199         }
200 }
201
202 /********************************** New **************************************/
203
204 static RenderPass *render_layer_add_pass(RenderResult *rr, RenderLayer *rl, int channels, const char *name, const char *viewname, const char *chan_id)
205 {
206         const int view_id = BLI_findstringindex(&rr->views, viewname, offsetof(RenderView, name));
207         RenderPass *rpass = MEM_callocN(sizeof(RenderPass), name);
208         size_t rectsize = ((size_t)rr->rectx) * rr->recty * channels;
209         
210         rpass->channels = channels;
211         rpass->rectx = rl->rectx;
212         rpass->recty = rl->recty;
213         rpass->view_id = view_id;
214
215         BLI_strncpy(rpass->name, name, sizeof(rpass->name));
216         BLI_strncpy(rpass->chan_id, chan_id, sizeof(rpass->chan_id));
217         BLI_strncpy(rpass->view, viewname, sizeof(rpass->view));
218         set_pass_full_name(rpass->fullname, rpass->name, -1, rpass->view, rpass->chan_id);
219         
220         if (rl->exrhandle) {
221                 int a;
222                 for (a = 0; a < channels; a++) {
223                         char passname[EXR_PASS_MAXNAME];
224                         IMB_exr_add_channel(rl->exrhandle, rl->name, set_pass_name(passname, rpass->name, a, rpass->chan_id), viewname, 0, 0, NULL, false);
225                 }
226         }
227         else {
228                 float *rect;
229                 int x;
230                 
231                 rpass->rect = MEM_mapallocN(sizeof(float) * rectsize, name);
232                 if (rpass->rect == NULL) {
233                         MEM_freeN(rpass);
234                         return NULL;
235                 }
236                 
237                 if (STREQ(rpass->name, RE_PASSNAME_VECTOR)) {
238                         /* initialize to max speed */
239                         rect = rpass->rect;
240                         for (x = rectsize - 1; x >= 0; x--)
241                                 rect[x] = PASS_VECTOR_MAX;
242                 }
243                 else if (STREQ(rpass->name, RE_PASSNAME_Z)) {
244                         rect = rpass->rect;
245                         for (x = rectsize - 1; x >= 0; x--)
246                                 rect[x] = 10e10;
247                 }
248         }
249
250         BLI_addtail(&rl->passes, rpass);
251
252         return rpass;
253 }
254 /* wrapper called from render_opengl */
255 RenderPass *gp_add_pass(RenderResult *rr, RenderLayer *rl, int channels, const char *name, const char *viewname)
256 {
257         return render_layer_add_pass(rr, rl, channels, name, viewname, "RGBA");
258 }
259
260 /* called by main render as well for parts */
261 /* will read info from Render *re to define layers */
262 /* called in threads */
263 /* re->winx,winy is coordinate space of entire image, partrct the part within */
264 RenderResult *render_result_new(Render *re, rcti *partrct, int crop, int savebuffers, const char *layername, const char *viewname)
265 {
266         RenderResult *rr;
267         RenderLayer *rl;
268         RenderView *rv;
269         int rectx, recty;
270         
271         rectx = BLI_rcti_size_x(partrct);
272         recty = BLI_rcti_size_y(partrct);
273         
274         if (rectx <= 0 || recty <= 0)
275                 return NULL;
276         
277         rr = MEM_callocN(sizeof(RenderResult), "new render result");
278         rr->rectx = rectx;
279         rr->recty = recty;
280         rr->renrect.xmin = 0; rr->renrect.xmax = rectx - 2 * crop;
281         /* crop is one or two extra pixels rendered for filtering, is used for merging and display too */
282         rr->crop = crop;
283
284         /* tilerect is relative coordinates within render disprect. do not subtract crop yet */
285         rr->tilerect.xmin = partrct->xmin - re->disprect.xmin;
286         rr->tilerect.xmax = partrct->xmax - re->disprect.xmin;
287         rr->tilerect.ymin = partrct->ymin - re->disprect.ymin;
288         rr->tilerect.ymax = partrct->ymax - re->disprect.ymin;
289         
290         if (savebuffers) {
291                 rr->do_exr_tile = true;
292         }
293
294         render_result_views_new(rr, &re->r);
295
296         /* check renderdata for amount of layers */
297         FOREACH_VIEW_LAYER_TO_RENDER_BEGIN(re, view_layer)
298         {
299                 if (layername && layername[0]) {
300                         if (!STREQ(view_layer->name, layername)) {
301                                 continue;
302                         }
303                 }
304
305                 rl = MEM_callocN(sizeof(RenderLayer), "new render layer");
306                 BLI_addtail(&rr->layers, rl);
307                 
308                 BLI_strncpy(rl->name, view_layer->name, sizeof(rl->name));
309                 rl->layflag = view_layer->layflag;
310                 rl->passflag = view_layer->passflag; /* for debugging: view_layer->passflag | SCE_PASS_RAYHITS; */
311                 rl->pass_xor = view_layer->pass_xor;
312                 rl->rectx = rectx;
313                 rl->recty = recty;
314                 
315                 if (rr->do_exr_tile) {
316                         rl->display_buffer = MEM_mapallocN((size_t)rectx * recty * sizeof(unsigned int),
317                                                            "Combined display space rgba");
318                         if (rl->display_buffer == NULL) {
319                                 render_result_free(rr);
320                                 return NULL;
321                         }
322                         rl->exrhandle = IMB_exr_get_handle();
323                 }
324
325                 for (rv = rr->views.first; rv; rv = rv->next) {
326                         const char *view = rv->name;
327
328                         if (viewname && viewname[0])
329                                 if (!STREQ(view, viewname))
330                                         continue;
331
332                         if (rr->do_exr_tile)
333                                 IMB_exr_add_view(rl->exrhandle, view);
334
335 #define RENDER_LAYER_ADD_PASS_SAFE(rr, rl, channels, name, viewname, chan_id) \
336                         do { \
337                                 if (render_layer_add_pass(rr, rl, channels, name, viewname, chan_id) == NULL) { \
338                                         render_result_free(rr); \
339                                         return NULL; \
340                                 } \
341                         } while (false)
342
343                         /* a renderlayer should always have a Combined pass*/
344                         render_layer_add_pass(rr, rl, 4, "Combined", view, "RGBA");
345
346                         if (view_layer->passflag  & SCE_PASS_Z)
347                                 RENDER_LAYER_ADD_PASS_SAFE(rr, rl, 1, RE_PASSNAME_Z, view, "Z");
348                         if (view_layer->passflag  & SCE_PASS_VECTOR)
349                                 RENDER_LAYER_ADD_PASS_SAFE(rr, rl, 4, RE_PASSNAME_VECTOR, view, "XYZW");
350                         if (view_layer->passflag  & SCE_PASS_NORMAL)
351                                 RENDER_LAYER_ADD_PASS_SAFE(rr, rl, 3, RE_PASSNAME_NORMAL, view, "XYZ");
352                         if (view_layer->passflag  & SCE_PASS_UV)
353                                 RENDER_LAYER_ADD_PASS_SAFE(rr, rl, 3, RE_PASSNAME_UV, view, "UVA");
354                         if (view_layer->passflag  & SCE_PASS_RGBA)
355                                 RENDER_LAYER_ADD_PASS_SAFE(rr, rl, 4, RE_PASSNAME_RGBA, view, "RGBA");
356                         if (view_layer->passflag  & SCE_PASS_EMIT)
357                                 RENDER_LAYER_ADD_PASS_SAFE(rr, rl, 3, RE_PASSNAME_EMIT, view, "RGB");
358                         if (view_layer->passflag  & SCE_PASS_DIFFUSE)
359                                 RENDER_LAYER_ADD_PASS_SAFE(rr, rl, 3, RE_PASSNAME_DIFFUSE, view, "RGB");
360                         if (view_layer->passflag  & SCE_PASS_SPEC)
361                                 RENDER_LAYER_ADD_PASS_SAFE(rr, rl, 3, RE_PASSNAME_SPEC, view, "RGB");
362                         if (view_layer->passflag  & SCE_PASS_AO)
363                                 RENDER_LAYER_ADD_PASS_SAFE(rr, rl, 3, RE_PASSNAME_AO, view, "RGB");
364                         if (view_layer->passflag  & SCE_PASS_ENVIRONMENT)
365                                 RENDER_LAYER_ADD_PASS_SAFE(rr, rl, 3, RE_PASSNAME_ENVIRONMENT, view, "RGB");
366                         if (view_layer->passflag  & SCE_PASS_INDIRECT)
367                                 RENDER_LAYER_ADD_PASS_SAFE(rr, rl, 3, RE_PASSNAME_INDIRECT, view, "RGB");
368                         if (view_layer->passflag  & SCE_PASS_SHADOW)
369                                 RENDER_LAYER_ADD_PASS_SAFE(rr, rl, 3, RE_PASSNAME_SHADOW, view, "RGB");
370                         if (view_layer->passflag  & SCE_PASS_REFLECT)
371                                 RENDER_LAYER_ADD_PASS_SAFE(rr, rl, 3, RE_PASSNAME_REFLECT, view, "RGB");
372                         if (view_layer->passflag  & SCE_PASS_REFRACT)
373                                 RENDER_LAYER_ADD_PASS_SAFE(rr, rl, 3, RE_PASSNAME_REFRACT, view, "RGB");
374                         if (view_layer->passflag  & SCE_PASS_INDEXOB)
375                                 RENDER_LAYER_ADD_PASS_SAFE(rr, rl, 1, RE_PASSNAME_INDEXOB, view, "X");
376                         if (view_layer->passflag  & SCE_PASS_INDEXMA)
377                                 RENDER_LAYER_ADD_PASS_SAFE(rr, rl, 1, RE_PASSNAME_INDEXMA, view, "X");
378                         if (view_layer->passflag  & SCE_PASS_MIST)
379                                 RENDER_LAYER_ADD_PASS_SAFE(rr, rl, 1, RE_PASSNAME_MIST, view, "Z");
380                         if (rl->passflag & SCE_PASS_RAYHITS)
381                                 RENDER_LAYER_ADD_PASS_SAFE(rr, rl, 4, RE_PASSNAME_RAYHITS, view, "RGB");
382                         if (view_layer->passflag  & SCE_PASS_DIFFUSE_DIRECT)
383                                 RENDER_LAYER_ADD_PASS_SAFE(rr, rl, 3, RE_PASSNAME_DIFFUSE_DIRECT, view, "RGB");
384                         if (view_layer->passflag  & SCE_PASS_DIFFUSE_INDIRECT)
385                                 RENDER_LAYER_ADD_PASS_SAFE(rr, rl, 3, RE_PASSNAME_DIFFUSE_INDIRECT, view, "RGB");
386                         if (view_layer->passflag  & SCE_PASS_DIFFUSE_COLOR)
387                                 RENDER_LAYER_ADD_PASS_SAFE(rr, rl, 3, RE_PASSNAME_DIFFUSE_COLOR, view, "RGB");
388                         if (view_layer->passflag  & SCE_PASS_GLOSSY_DIRECT)
389                                 RENDER_LAYER_ADD_PASS_SAFE(rr, rl, 3, RE_PASSNAME_GLOSSY_DIRECT, view, "RGB");
390                         if (view_layer->passflag  & SCE_PASS_GLOSSY_INDIRECT)
391                                 RENDER_LAYER_ADD_PASS_SAFE(rr, rl, 3, RE_PASSNAME_GLOSSY_INDIRECT, view, "RGB");
392                         if (view_layer->passflag  & SCE_PASS_GLOSSY_COLOR)
393                                 RENDER_LAYER_ADD_PASS_SAFE(rr, rl, 3, RE_PASSNAME_GLOSSY_COLOR, view, "RGB");
394                         if (view_layer->passflag  & SCE_PASS_TRANSM_DIRECT)
395                                 RENDER_LAYER_ADD_PASS_SAFE(rr, rl, 3, RE_PASSNAME_TRANSM_DIRECT, view, "RGB");
396                         if (view_layer->passflag  & SCE_PASS_TRANSM_INDIRECT)
397                                 RENDER_LAYER_ADD_PASS_SAFE(rr, rl, 3, RE_PASSNAME_TRANSM_INDIRECT, view, "RGB");
398                         if (view_layer->passflag  & SCE_PASS_TRANSM_COLOR)
399                                 RENDER_LAYER_ADD_PASS_SAFE(rr, rl, 3, RE_PASSNAME_TRANSM_COLOR, view, "RGB");
400                         if (view_layer->passflag  & SCE_PASS_SUBSURFACE_DIRECT)
401                                 RENDER_LAYER_ADD_PASS_SAFE(rr, rl, 3, RE_PASSNAME_SUBSURFACE_DIRECT, view, "RGB");
402                         if (view_layer->passflag  & SCE_PASS_SUBSURFACE_INDIRECT)
403                                 RENDER_LAYER_ADD_PASS_SAFE(rr, rl, 3, RE_PASSNAME_SUBSURFACE_INDIRECT, view, "RGB");
404                         if (view_layer->passflag  & SCE_PASS_SUBSURFACE_COLOR)
405                                 RENDER_LAYER_ADD_PASS_SAFE(rr, rl, 3, RE_PASSNAME_SUBSURFACE_COLOR, view, "RGB");
406 #undef RENDER_LAYER_ADD_PASS_SAFE
407                 }
408         }
409         FOREACH_VIEW_LAYER_TO_RENDER_END;
410
411         /* previewrender doesn't do layers, so we make a default one */
412         if (BLI_listbase_is_empty(&rr->layers) && !(layername && layername[0])) {
413                 rl = MEM_callocN(sizeof(RenderLayer), "new render layer");
414                 BLI_addtail(&rr->layers, rl);
415                 
416                 rl->rectx = rectx;
417                 rl->recty = recty;
418
419                 /* duplicate code... */
420                 if (rr->do_exr_tile) {
421                         rl->display_buffer = MEM_mapallocN(rectx * recty * sizeof(unsigned int), "Combined display space rgba");
422                         rl->exrhandle = IMB_exr_get_handle();
423                 }
424
425                 for (rv = rr->views.first; rv; rv = rv->next) {
426                         const char *view = rv->name;
427
428                         if (viewname && viewname[0])
429                                 if (strcmp(view, viewname) != 0)
430                                         continue;
431
432                         if (rr->do_exr_tile)
433                                 IMB_exr_add_view(rl->exrhandle, view);
434
435                         /* a renderlayer should always have a Combined pass */
436                         render_layer_add_pass(rr, rl, 4, RE_PASSNAME_COMBINED, view, "RGBA");
437                 }
438
439                 /* note, this has to be in sync with scene.c */
440                 rl->layflag = 0x7FFF;    /* solid ztra halo strand */
441                 rl->passflag = SCE_PASS_COMBINED;
442                 
443                 re->active_view_layer = 0;
444         }
445         
446         /* border render; calculate offset for use in compositor. compo is centralized coords */
447         /* XXX obsolete? I now use it for drawing border render offset (ton) */
448         rr->xof = re->disprect.xmin + BLI_rcti_cent_x(&re->disprect) - (re->winx / 2);
449         rr->yof = re->disprect.ymin + BLI_rcti_cent_y(&re->disprect) - (re->winy / 2);
450         
451         return rr;
452 }
453
454 void render_result_clone_passes(Render *re, RenderResult *rr, const char *viewname)
455 {
456         RenderLayer *rl;
457         RenderPass *main_rp;
458
459         for (rl = rr->layers.first; rl; rl = rl->next) {
460                 RenderLayer *main_rl = BLI_findstring(&re->result->layers, rl->name, offsetof(RenderLayer, name));
461                 if (!main_rl) {
462                         continue;
463                 }
464
465                 for (main_rp = main_rl->passes.first; main_rp; main_rp = main_rp->next) {
466                         if (viewname && viewname[0] && !STREQ(main_rp->view, viewname)) {
467                                 continue;
468                         }
469
470                         /* Compare fullname to make sure that the view also is equal. */
471                         RenderPass *rp = BLI_findstring(&rl->passes, main_rp->fullname, offsetof(RenderPass, fullname));
472                         if (!rp) {
473                                 render_layer_add_pass(rr, rl, main_rp->channels, main_rp->name, main_rp->view, main_rp->chan_id);
474                         }
475                 }
476         }
477 }
478
479 void render_result_add_pass(RenderResult *rr, const char *name, int channels, const char *chan_id, const char *layername, const char *viewname)
480 {
481         RenderLayer *rl;
482         RenderPass *rp;
483         RenderView *rv;
484
485         for (rl = rr->layers.first; rl; rl = rl->next) {
486                 if (layername && layername[0] && !STREQ(rl->name, layername)) {
487                         continue;
488                 }
489
490                 for (rv = rr->views.first; rv; rv = rv->next) {
491                         const char *view = rv->name;
492
493                         if (viewname && viewname[0] && !STREQ(view, viewname)) continue;
494
495                         /* Ensure that the pass doesn't exist yet. */
496                         for (rp = rl->passes.first; rp; rp = rp->next) {
497                                 if (!STREQ(rp->name, name)) continue;
498                                 if (!STREQ(rp->view, view)) continue;
499                         }
500
501                         if (!rp) {
502                                 render_layer_add_pass(rr, rl, channels, name, view, chan_id);
503                         }
504                 }
505         }
506 }
507
508 static int passtype_from_name(const char *name)
509 {
510         const char delim[] = {'.', '\0'};
511         const char *sep, *suf;
512         int len = BLI_str_partition(name, delim, &sep, &suf);
513
514 #define CHECK_PASS(NAME) if (STREQLEN(name, RE_PASSNAME_ ## NAME, len)) return SCE_PASS_ ## NAME
515
516         CHECK_PASS(COMBINED);
517         CHECK_PASS(Z);
518         CHECK_PASS(VECTOR);
519         CHECK_PASS(NORMAL);
520         CHECK_PASS(UV);
521         CHECK_PASS(RGBA);
522         CHECK_PASS(EMIT);
523         CHECK_PASS(DIFFUSE);
524         CHECK_PASS(SPEC);
525         CHECK_PASS(SHADOW);
526         CHECK_PASS(AO);
527         CHECK_PASS(ENVIRONMENT);
528         CHECK_PASS(INDIRECT);
529         CHECK_PASS(REFLECT);
530         CHECK_PASS(REFRACT);
531         CHECK_PASS(INDEXOB);
532         CHECK_PASS(INDEXMA);
533         CHECK_PASS(MIST);
534         CHECK_PASS(RAYHITS);
535         CHECK_PASS(DIFFUSE_DIRECT);
536         CHECK_PASS(DIFFUSE_INDIRECT);
537         CHECK_PASS(DIFFUSE_COLOR);
538         CHECK_PASS(GLOSSY_DIRECT);
539         CHECK_PASS(GLOSSY_INDIRECT);
540         CHECK_PASS(GLOSSY_COLOR);
541         CHECK_PASS(TRANSM_DIRECT);
542         CHECK_PASS(TRANSM_INDIRECT);
543         CHECK_PASS(TRANSM_COLOR);
544         CHECK_PASS(SUBSURFACE_DIRECT);
545         CHECK_PASS(SUBSURFACE_INDIRECT);
546         CHECK_PASS(SUBSURFACE_COLOR);
547
548 #undef CHECK_PASS
549         return 0;
550 }
551
552 /* callbacks for render_result_new_from_exr */
553 static void *ml_addlayer_cb(void *base, const char *str)
554 {
555         RenderResult *rr = base;
556         RenderLayer *rl;
557         
558         rl = MEM_callocN(sizeof(RenderLayer), "new render layer");
559         BLI_addtail(&rr->layers, rl);
560
561         BLI_strncpy(rl->name, str, EXR_LAY_MAXNAME);
562         return rl;
563 }
564
565 static void ml_addpass_cb(void *base, void *lay, const char *name, float *rect, int totchan, const char *chan_id, const char *view)
566 {
567         RenderResult *rr = base;
568         RenderLayer *rl = lay;
569         RenderPass *rpass = MEM_callocN(sizeof(RenderPass), "loaded pass");
570
571         BLI_addtail(&rl->passes, rpass);
572         rpass->channels = totchan;
573         rl->passflag |= passtype_from_name(name);
574
575         /* channel id chars */
576         BLI_strncpy(rpass->chan_id, chan_id, sizeof(rpass->chan_id));
577
578         rpass->rect = rect;
579         BLI_strncpy(rpass->name, name, EXR_PASS_MAXNAME);
580         BLI_strncpy(rpass->view, view, sizeof(rpass->view));
581         set_pass_full_name(rpass->fullname, name, -1, view, rpass->chan_id);
582
583         if (view[0] != '\0') {
584                 rpass->view_id = BLI_findstringindex(&rr->views, view, offsetof(RenderView, name));
585         }
586         else {
587                 rpass->view_id = 0;
588         }
589 }
590
591 static void *ml_addview_cb(void *base, const char *str)
592 {
593         RenderResult *rr = base;
594         RenderView *rv;
595
596         rv = MEM_callocN(sizeof(RenderView), "new render view");
597         BLI_strncpy(rv->name, str, EXR_VIEW_MAXNAME);
598
599         /* For stereo drawing we need to ensure:
600          * STEREO_LEFT_NAME  == STEREO_LEFT_ID and
601          * STEREO_RIGHT_NAME == STEREO_RIGHT_ID */
602
603         if (STREQ(str, STEREO_LEFT_NAME)) {
604                 BLI_addhead(&rr->views, rv);
605         }
606         else if (STREQ(str, STEREO_RIGHT_NAME)) {
607                 RenderView *left_rv = BLI_findstring(&rr->views, STEREO_LEFT_NAME, offsetof(RenderView, name));
608
609                 if (left_rv == NULL) {
610                         BLI_addhead(&rr->views, rv);
611                 }
612                 else {
613                         BLI_insertlinkafter(&rr->views, left_rv, rv);
614                 }
615         }
616         else {
617                 BLI_addtail(&rr->views, rv);
618         }
619
620         return rv;
621 }
622
623 static int order_render_passes(const void *a, const void *b)
624 {
625         // 1 if a is after b
626         RenderPass *rpa = (RenderPass *) a;
627         RenderPass *rpb = (RenderPass *) b;
628         unsigned int passtype_a = passtype_from_name(rpa->name);
629         unsigned int passtype_b = passtype_from_name(rpb->name);
630
631         /* Render passes with default type always go first. */
632         if (passtype_b && !passtype_a)
633                 return 1;
634         if (passtype_a && !passtype_b)
635                 return 0;
636
637         if (passtype_a && passtype_b) {
638                 if (passtype_a > passtype_b)
639                         return 1;
640                 else if (passtype_a < passtype_b)
641                         return 0;
642         }
643         else {
644                 int cmp = strncmp(rpa->name, rpb->name, EXR_PASS_MAXNAME);
645                 if (cmp > 0)
646                         return 1;
647                 if (cmp < 0)
648                         return 0;
649         }
650
651
652         /* they have the same type */
653         /* left first */
654         if (STREQ(rpa->view, STEREO_LEFT_NAME))
655                 return 0;
656         else if (STREQ(rpb->view, STEREO_LEFT_NAME))
657                 return 1;
658
659         /* right second */
660         if (STREQ(rpa->view, STEREO_RIGHT_NAME))
661                 return 0;
662         else if (STREQ(rpb->view, STEREO_RIGHT_NAME))
663                 return 1;
664
665         /* remaining in ascending id order */
666         return (rpa->view_id < rpb->view_id);
667 }
668
669 /* from imbuf, if a handle was returned and it's not a singlelayer multiview we convert this to render result */
670 RenderResult *render_result_new_from_exr(void *exrhandle, const char *colorspace, bool predivide, int rectx, int recty)
671 {
672         RenderResult *rr = MEM_callocN(sizeof(RenderResult), __func__);
673         RenderLayer *rl;
674         RenderPass *rpass;
675         const char *to_colorspace = IMB_colormanagement_role_colorspace_name_get(COLOR_ROLE_SCENE_LINEAR);
676
677         rr->rectx = rectx;
678         rr->recty = recty;
679         
680         IMB_exr_multilayer_convert(exrhandle, rr, ml_addview_cb, ml_addlayer_cb, ml_addpass_cb);
681
682         for (rl = rr->layers.first; rl; rl = rl->next) {
683                 rl->rectx = rectx;
684                 rl->recty = recty;
685
686                 BLI_listbase_sort(&rl->passes, order_render_passes);
687
688                 for (rpass = rl->passes.first; rpass; rpass = rpass->next) {
689                         rpass->rectx = rectx;
690                         rpass->recty = recty;
691
692                         if (rpass->channels >= 3) {
693                                 IMB_colormanagement_transform(rpass->rect, rpass->rectx, rpass->recty, rpass->channels,
694                                                               colorspace, to_colorspace, predivide);
695                         }
696                 }
697         }
698         
699         return rr;
700 }
701
702 void render_result_view_new(RenderResult *rr, const char *viewname)
703 {
704         RenderView *rv = MEM_callocN(sizeof(RenderView), "new render view");
705         BLI_addtail(&rr->views, rv);
706         BLI_strncpy(rv->name, viewname, sizeof(rv->name));
707 }
708
709 void render_result_views_new(RenderResult *rr, RenderData *rd)
710 {
711         SceneRenderView *srv;
712
713         /* clear previously existing views - for sequencer */
714         render_result_views_free(rr);
715
716         /* check renderdata for amount of views */
717         if ((rd->scemode & R_MULTIVIEW)) {
718                 for (srv = rd->views.first; srv; srv = srv->next) {
719                         if (BKE_scene_multiview_is_render_view_active(rd, srv) == false)
720                                 continue;
721                         render_result_view_new(rr, srv->name);
722                 }
723         }
724
725         /* we always need at least one view */
726         if (BLI_listbase_count_at_most(&rr->views, 1) == 0) {
727                 render_result_view_new(rr, "");
728         }
729 }
730
731 bool render_result_has_views(RenderResult *rr)
732 {
733         RenderView *rv = rr->views.first;
734         return (rv && (rv->next || rv->name[0]));
735 }
736
737 /*********************************** Merge ***********************************/
738
739 static void do_merge_tile(RenderResult *rr, RenderResult *rrpart, float *target, float *tile, int pixsize)
740 {
741         int y, tilex, tiley;
742         size_t ofs, copylen;
743         
744         copylen = tilex = rrpart->rectx;
745         tiley = rrpart->recty;
746         
747         if (rrpart->crop) { /* filters add pixel extra */
748                 tile += pixsize * (rrpart->crop + ((size_t)rrpart->crop) * tilex);
749                 
750                 copylen = tilex - 2 * rrpart->crop;
751                 tiley -= 2 * rrpart->crop;
752                 
753                 ofs = (((size_t)rrpart->tilerect.ymin) + rrpart->crop) * rr->rectx + (rrpart->tilerect.xmin + rrpart->crop);
754                 target += pixsize * ofs;
755         }
756         else {
757                 ofs = (((size_t)rrpart->tilerect.ymin) * rr->rectx + rrpart->tilerect.xmin);
758                 target += pixsize * ofs;
759         }
760
761         copylen *= sizeof(float) * pixsize;
762         tilex *= pixsize;
763         ofs = pixsize * rr->rectx;
764
765         for (y = 0; y < tiley; y++) {
766                 memcpy(target, tile, copylen);
767                 target += ofs;
768                 tile += tilex;
769         }
770 }
771
772 /* used when rendering to a full buffer, or when reading the exr part-layer-pass file */
773 /* no test happens here if it fits... we also assume layers are in sync */
774 /* is used within threads */
775 void render_result_merge(RenderResult *rr, RenderResult *rrpart)
776 {
777         RenderLayer *rl, *rlp;
778         RenderPass *rpass, *rpassp;
779         
780         for (rl = rr->layers.first; rl; rl = rl->next) {
781                 rlp = RE_GetRenderLayer(rrpart, rl->name);
782                 if (rlp) {
783                         /* passes are allocated in sync */
784                         for (rpass = rl->passes.first, rpassp = rlp->passes.first;
785                              rpass && rpassp;
786                              rpass = rpass->next)
787                         {
788                                 /* renderresult have all passes, renderpart only the active view's passes */
789                                 if (strcmp(rpassp->fullname, rpass->fullname) != 0)
790                                         continue;
791
792                                 do_merge_tile(rr, rrpart, rpass->rect, rpassp->rect, rpass->channels);
793
794                                 /* manually get next render pass */
795                                 rpassp = rpassp->next;
796                         }
797                 }
798         }
799 }
800
801 /* Called from the UI and render pipeline, to save multilayer and multiview
802  * images, optionally isolating a specific, view, layer or RGBA/Z pass. */
803 bool RE_WriteRenderResult(ReportList *reports, RenderResult *rr, const char *filename, ImageFormatData *imf, const char *view, int layer)
804 {
805         void *exrhandle = IMB_exr_get_handle();
806         const bool half_float = (imf && imf->depth == R_IMF_CHAN_DEPTH_16);
807         const bool multi_layer = !(imf && imf->imtype == R_IMF_IMTYPE_OPENEXR);
808         const bool write_z = !multi_layer && (imf && (imf->flag & R_IMF_FLAG_ZBUF));
809
810         /* Write first layer if not multilayer and no layer was specified. */
811         if (!multi_layer && layer == -1) {
812                 layer = 0;
813         }
814
815         /* First add views since IMB_exr_add_channel checks number of views. */
816         if (render_result_has_views(rr)) {
817                 for (RenderView *rview = rr->views.first; rview; rview = rview->next) {
818                         if (!view || STREQ(view, rview->name)) {
819                                 IMB_exr_add_view(exrhandle, rview->name);
820                         }
821                 }
822         }
823
824         /* Compositing result. */
825         if (rr->have_combined) {
826                 for (RenderView *rview = rr->views.first; rview; rview = rview->next) {
827                         if (!rview->rectf) {
828                                 continue;
829                         }
830
831                         const char *viewname = rview->name;
832                         if (view) {
833                                 if (!STREQ(view, viewname)) {
834                                         continue;
835                                 }
836                                 else {
837                                         viewname = "";
838                                 }
839                         }
840
841                         /* Skip compositing if only a single other layer is requested. */
842                         if (!multi_layer && layer != 0) {
843                                 continue;
844                         }
845
846                         for (int a = 0; a < 4; a++) {
847                                 char passname[EXR_PASS_MAXNAME];
848                                 char layname[EXR_PASS_MAXNAME];
849                                 const char *chan_id = "RGBA";
850
851                                 if (multi_layer) {
852                                         set_pass_name(passname, "Combined", a, chan_id);
853                                         BLI_strncpy(layname, "Composite", sizeof(layname));
854                                 }
855                                 else {
856                                         passname[0] = chan_id[a];
857                                         passname[1] = '\0';
858                                         layname[0] = '\0';
859                                 }
860
861                                 IMB_exr_add_channel(exrhandle, layname, passname, viewname,
862                                                     4, 4 * rr->rectx, rview->rectf + a, half_float);
863                         }
864
865                         if (write_z && rview->rectz) {
866                                 const char *layname = (multi_layer)? "Composite": "";
867                                 IMB_exr_add_channel(exrhandle, layname, "Z", viewname,
868                                                     1, rr->rectx, rview->rectz, false);
869                         }
870                 }
871         }
872
873         /* Other render layers. */
874         int nr = (rr->have_combined)? 1: 0;
875         for (RenderLayer *rl = rr->layers.first; rl; rl = rl->next, nr++) {
876                 /* Skip other render layers if requested. */
877                 if (!multi_layer && nr != layer) {
878                         continue;
879                 }
880
881                 for (RenderPass *rp = rl->passes.first; rp; rp = rp->next) {
882                         /* Skip non-RGBA and Z passes if not using multi layer. */
883                         if (!multi_layer && !(STREQ(rp->name, RE_PASSNAME_COMBINED) ||
884                                               STREQ(rp->name, "") ||
885                                               (STREQ(rp->name, RE_PASSNAME_Z) && write_z))) {
886                                 continue;
887                         }
888
889                         /* Skip pass if it does not match the requested view(s). */
890                         const char *viewname = rp->view;
891                         if (view) {
892                                 if (!STREQ(view, viewname)) {
893                                         continue;
894                                 }
895                                 else {
896                                         viewname = "";
897                                 }
898                         }
899
900                         /* We only store RGBA passes as half float, for
901                          * others precision loss can be problematic. */
902                         bool pass_half_float = half_float &&
903                                                (STREQ(rp->chan_id, "RGB") ||
904                                                 STREQ(rp->chan_id, "RGBA") ||
905                                                 STREQ(rp->chan_id, "R") ||
906                                                 STREQ(rp->chan_id, "G") ||
907                                                 STREQ(rp->chan_id, "B") ||
908                                                 STREQ(rp->chan_id, "A"));
909
910
911                         for (int a = 0; a < rp->channels; a++) {
912                                 /* Save Combined as RGBA if single layer save. */
913                                 char passname[EXR_PASS_MAXNAME];
914                                 char layname[EXR_PASS_MAXNAME];
915
916                                 if (multi_layer) {
917                                         set_pass_name(passname, rp->name, a, rp->chan_id);
918                                         BLI_strncpy(layname, rl->name, sizeof(layname));
919                                 }
920                                 else {
921                                         passname[0] = rp->chan_id[a];
922                                         passname[1] = '\0';
923                                         layname[0] = '\0';
924                                 }
925
926                                 IMB_exr_add_channel(exrhandle, layname, passname, viewname,
927                                                     rp->channels, rp->channels * rr->rectx, rp->rect + a,
928                                                     pass_half_float);
929                         }
930                 }
931         }
932
933         errno = 0;
934
935         BLI_make_existing_file(filename);
936
937         int compress = (imf ? imf->exr_codec : 0);
938         bool success = IMB_exr_begin_write(exrhandle, filename, rr->rectx, rr->recty, compress, rr->stamp_data);
939         if (success) {
940                 IMB_exr_write_channels(exrhandle);
941         }
942         else {
943                 /* TODO, get the error from openexr's exception */
944                 BKE_reportf(reports, RPT_ERROR, "Error writing render result, %s (see console)", strerror(errno));
945         }
946
947         IMB_exr_close(exrhandle);
948         return success;
949 }
950
951 /**************************** Single Layer Rendering *************************/
952
953 void render_result_single_layer_begin(Render *re)
954 {
955         /* all layers except the active one get temporally pushed away */
956
957         /* officially pushed result should be NULL... error can happen with do_seq */
958         RE_FreeRenderResult(re->pushedresult);
959         
960         re->pushedresult = re->result;
961         re->result = NULL;
962 }
963
964 /* if scemode is R_SINGLE_LAYER, at end of rendering, merge the both render results */
965 void render_result_single_layer_end(Render *re)
966 {
967         ViewLayer *view_layer;
968         RenderLayer *rlpush;
969         RenderLayer *rl;
970         int nr;
971
972         if (re->result == NULL) {
973                 printf("pop render result error; no current result!\n");
974                 return;
975         }
976
977         if (!re->pushedresult)
978                 return;
979
980         if (re->pushedresult->rectx == re->result->rectx && re->pushedresult->recty == re->result->recty) {
981                 /* find which layer in re->pushedresult should be replaced */
982                 rl = re->result->layers.first;
983                 
984                 /* render result should be empty after this */
985                 BLI_remlink(&re->result->layers, rl);
986                 
987                 /* reconstruct render result layers */
988                 for (nr = 0, view_layer = re->view_layers.first; view_layer; view_layer = view_layer->next, nr++) {
989                         if (nr == re->active_view_layer) {
990                                 BLI_addtail(&re->result->layers, rl);
991                         }
992                         else {
993                                 rlpush = RE_GetRenderLayer(re->pushedresult, view_layer->name);
994                                 if (rlpush) {
995                                         BLI_remlink(&re->pushedresult->layers, rlpush);
996                                         BLI_addtail(&re->result->layers, rlpush);
997                                 }
998                         }
999                 }
1000         }
1001
1002         RE_FreeRenderResult(re->pushedresult);
1003         re->pushedresult = NULL;
1004 }
1005
1006 /************************* EXR Tile File Rendering ***************************/
1007
1008 static void save_render_result_tile(RenderResult *rr, RenderResult *rrpart, const char *viewname)
1009 {
1010         RenderLayer *rlp, *rl;
1011         RenderPass *rpassp;
1012         int offs, partx, party;
1013         
1014         BLI_thread_lock(LOCK_IMAGE);
1015         
1016         for (rlp = rrpart->layers.first; rlp; rlp = rlp->next) {
1017                 rl = RE_GetRenderLayer(rr, rlp->name);
1018
1019                 /* should never happen but prevents crash if it does */
1020                 BLI_assert(rl);
1021                 if (UNLIKELY(rl == NULL)) {
1022                         continue;
1023                 }
1024
1025                 if (rrpart->crop) { /* filters add pixel extra */
1026                         offs = (rrpart->crop + rrpart->crop * rrpart->rectx);
1027                 }
1028                 else {
1029                         offs = 0;
1030                 }
1031
1032                 /* passes are allocated in sync */
1033                 for (rpassp = rlp->passes.first; rpassp; rpassp = rpassp->next) {
1034                         const int xstride = rpassp->channels;
1035                         int a;
1036                         char fullname[EXR_PASS_MAXNAME];
1037
1038                         for (a = 0; a < xstride; a++) {
1039                                 set_pass_full_name(fullname, rpassp->name, a, viewname, rpassp->chan_id);
1040
1041                                 IMB_exr_set_channel(rl->exrhandle, rlp->name, fullname,
1042                                                     xstride, xstride * rrpart->rectx, rpassp->rect + a + xstride * offs);
1043                         }
1044                 }
1045                 
1046         }
1047
1048         party = rrpart->tilerect.ymin + rrpart->crop;
1049         partx = rrpart->tilerect.xmin + rrpart->crop;
1050
1051         for (rlp = rrpart->layers.first; rlp; rlp = rlp->next) {
1052                 rl = RE_GetRenderLayer(rr, rlp->name);
1053
1054                 /* should never happen but prevents crash if it does */
1055                 BLI_assert(rl);
1056                 if (UNLIKELY(rl == NULL)) {
1057                         continue;
1058                 }
1059
1060                 IMB_exrtile_write_channels(rl->exrhandle, partx, party, 0, viewname, false);
1061         }
1062
1063         BLI_thread_unlock(LOCK_IMAGE);
1064 }
1065
1066 void render_result_save_empty_result_tiles(Render *re)
1067 {
1068         RenderPart *pa;
1069         RenderResult *rr;
1070         RenderLayer *rl;
1071         
1072         for (rr = re->result; rr; rr = rr->next) {
1073                 for (rl = rr->layers.first; rl; rl = rl->next) {
1074                         for (pa = re->parts.first; pa; pa = pa->next) {
1075                                 if (pa->status != PART_STATUS_MERGED) {
1076                                         int party = pa->disprect.ymin - re->disprect.ymin;
1077                                         int partx = pa->disprect.xmin - re->disprect.xmin;
1078                                         IMB_exrtile_write_channels(rl->exrhandle, partx, party, 0, re->viewname, true);
1079                                 }
1080                         }
1081                 }
1082         }
1083 }
1084
1085 /* begin write of exr tile file */
1086 void render_result_exr_file_begin(Render *re)
1087 {
1088         RenderResult *rr;
1089         RenderLayer *rl;
1090         char str[FILE_MAX];
1091
1092         for (rr = re->result; rr; rr = rr->next) {
1093                 for (rl = rr->layers.first; rl; rl = rl->next) {
1094                         render_result_exr_file_path(re->scene, rl->name, rr->sample_nr, str);
1095                         printf("write exr tmp file, %dx%d, %s\n", rr->rectx, rr->recty, str);
1096                         IMB_exrtile_begin_write(rl->exrhandle, str, 0, rr->rectx, rr->recty, re->partx, re->party);
1097                 }
1098         }
1099 }
1100
1101 /* end write of exr tile file, read back first sample */
1102 void render_result_exr_file_end(Render *re)
1103 {
1104         RenderResult *rr;
1105         RenderLayer *rl;
1106
1107         for (rr = re->result; rr; rr = rr->next) {
1108                 for (rl = rr->layers.first; rl; rl = rl->next) {
1109                         IMB_exr_close(rl->exrhandle);
1110                         rl->exrhandle = NULL;
1111                 }
1112
1113                 rr->do_exr_tile = false;
1114         }
1115         
1116         render_result_free_list(&re->fullresult, re->result);
1117         re->result = NULL;
1118
1119         render_result_exr_file_read_sample(re, 0);
1120 }
1121
1122 /* save part into exr file */
1123 void render_result_exr_file_merge(RenderResult *rr, RenderResult *rrpart, const char *viewname)
1124 {
1125         for (; rr && rrpart; rr = rr->next, rrpart = rrpart->next)
1126                 save_render_result_tile(rr, rrpart, viewname);
1127 }
1128
1129 /* path to temporary exr file */
1130 void render_result_exr_file_path(Scene *scene, const char *layname, int sample, char *filepath)
1131 {
1132         char name[FILE_MAXFILE + MAX_ID_NAME + MAX_ID_NAME + 100];
1133         const char *fi = BLI_path_basename(G.main->name);
1134         
1135         if (sample == 0) {
1136                 BLI_snprintf(name, sizeof(name), "%s_%s_%s.exr", fi, scene->id.name + 2, layname);
1137         }
1138         else {
1139                 BLI_snprintf(name, sizeof(name), "%s_%s_%s%d.exr", fi, scene->id.name + 2, layname, sample);
1140         }
1141
1142         /* Make name safe for paths, see T43275. */
1143         BLI_filename_make_safe(name);
1144
1145         BLI_make_file_string("/", filepath, BKE_tempdir_session(), name);
1146 }
1147
1148 /* only for temp buffer, makes exact copy of render result */
1149 int render_result_exr_file_read_sample(Render *re, int sample)
1150 {
1151         RenderLayer *rl;
1152         char str[FILE_MAXFILE + MAX_ID_NAME + MAX_ID_NAME + 100] = "";
1153         bool success = true;
1154
1155         RE_FreeRenderResult(re->result);
1156         re->result = render_result_new(re, &re->disprect, 0, RR_USE_MEM, RR_ALL_LAYERS, RR_ALL_VIEWS);
1157
1158         for (rl = re->result->layers.first; rl; rl = rl->next) {
1159                 render_result_exr_file_path(re->scene, rl->name, sample, str);
1160                 printf("read exr tmp file: %s\n", str);
1161
1162                 if (!render_result_exr_file_read_path(re->result, rl, str)) {
1163                         printf("cannot read: %s\n", str);
1164                         success = false;
1165                 }
1166         }
1167
1168         return success;
1169 }
1170
1171 /* called for reading temp files, and for external engines */
1172 int render_result_exr_file_read_path(RenderResult *rr, RenderLayer *rl_single, const char *filepath)
1173 {
1174         RenderLayer *rl;
1175         RenderPass *rpass;
1176         void *exrhandle = IMB_exr_get_handle();
1177         int rectx, recty;
1178
1179         if (IMB_exr_begin_read(exrhandle, filepath, &rectx, &recty) == 0) {
1180                 printf("failed being read %s\n", filepath);
1181                 IMB_exr_close(exrhandle);
1182                 return 0;
1183         }
1184
1185         if (rr == NULL || rectx != rr->rectx || recty != rr->recty) {
1186                 if (rr)
1187                         printf("error in reading render result: dimensions don't match\n");
1188                 else
1189                         printf("error in reading render result: NULL result pointer\n");
1190                 IMB_exr_close(exrhandle);
1191                 return 0;
1192         }
1193
1194         for (rl = rr->layers.first; rl; rl = rl->next) {
1195                 if (rl_single && rl_single != rl)
1196                         continue;
1197                 
1198                 /* passes are allocated in sync */
1199                 for (rpass = rl->passes.first; rpass; rpass = rpass->next) {
1200                         const int xstride = rpass->channels;
1201                         int a;
1202                         char fullname[EXR_PASS_MAXNAME];
1203
1204                         for (a = 0; a < xstride; a++) {
1205                                 set_pass_full_name(fullname, rpass->name, a, rpass->view, rpass->chan_id);
1206                                 IMB_exr_set_channel(exrhandle, rl->name, fullname,
1207                                                     xstride, xstride * rectx, rpass->rect + a);
1208                         }
1209
1210                         set_pass_full_name(rpass->fullname, rpass->name, -1, rpass->view, rpass->chan_id);
1211                 }
1212         }
1213
1214         IMB_exr_read_channels(exrhandle);
1215         IMB_exr_close(exrhandle);
1216
1217         return 1;
1218 }
1219
1220 static void render_result_exr_file_cache_path(Scene *sce, const char *root, char *r_path)
1221 {
1222         char filename_full[FILE_MAX + MAX_ID_NAME + 100], filename[FILE_MAXFILE], dirname[FILE_MAXDIR];
1223         char path_digest[16] = {0};
1224         char path_hexdigest[33];
1225
1226         /* If root is relative, use either current .blend file dir, or temp one if not saved. */
1227         if (G.main->name[0]) {
1228                 BLI_split_dirfile(G.main->name, dirname, filename, sizeof(dirname), sizeof(filename));
1229                 BLI_replace_extension(filename, sizeof(filename), "");  /* strip '.blend' */
1230                 BLI_hash_md5_buffer(G.main->name, strlen(G.main->name), path_digest);
1231         }
1232         else {
1233                 BLI_strncpy(dirname, BKE_tempdir_base(), sizeof(dirname));
1234                 BLI_strncpy(filename, "UNSAVED", sizeof(filename));
1235         }
1236         BLI_hash_md5_to_hexdigest(path_digest, path_hexdigest);
1237
1238         /* Default to *non-volatile* tmp dir. */
1239         if (*root == '\0') {
1240                 root = BKE_tempdir_base();
1241         }
1242
1243         BLI_snprintf(filename_full, sizeof(filename_full), "cached_RR_%s_%s_%s.exr",
1244                      filename, sce->id.name + 2, path_hexdigest);
1245         BLI_make_file_string(dirname, r_path, root, filename_full);
1246 }
1247
1248 void render_result_exr_file_cache_write(Render *re)
1249 {
1250         RenderResult *rr = re->result;
1251         char str[FILE_MAXFILE + FILE_MAXFILE + MAX_ID_NAME + 100];
1252         char *root = U.render_cachedir;
1253
1254         render_result_exr_file_cache_path(re->scene, root, str);
1255         printf("Caching exr file, %dx%d, %s\n", rr->rectx, rr->recty, str);
1256
1257         RE_WriteRenderResult(NULL, rr, str, NULL, NULL, -1);
1258 }
1259
1260 /* For cache, makes exact copy of render result */
1261 bool render_result_exr_file_cache_read(Render *re)
1262 {
1263         char str[FILE_MAXFILE + MAX_ID_NAME + MAX_ID_NAME + 100] = "";
1264         char *root = U.render_cachedir;
1265
1266         RE_FreeRenderResult(re->result);
1267         re->result = render_result_new(re, &re->disprect, 0, RR_USE_MEM, RR_ALL_LAYERS, RR_ALL_VIEWS);
1268
1269         /* First try cache. */
1270         render_result_exr_file_cache_path(re->scene, root, str);
1271
1272         printf("read exr cache file: %s\n", str);
1273         if (!render_result_exr_file_read_path(re->result, NULL, str)) {
1274                 printf("cannot read: %s\n", str);
1275                 return false;
1276         }
1277         return true;
1278 }
1279
1280 /*************************** Combined Pixel Rect *****************************/
1281
1282 ImBuf *render_result_rect_to_ibuf(RenderResult *rr, RenderData *rd, const int view_id)
1283 {
1284         ImBuf *ibuf = IMB_allocImBuf(rr->rectx, rr->recty, rd->im_format.planes, 0);
1285         RenderView *rv = RE_RenderViewGetById(rr, view_id);
1286
1287         /* if not exists, BKE_imbuf_write makes one */
1288         ibuf->rect = (unsigned int *) rv->rect32;
1289         ibuf->rect_float = rv->rectf;
1290         ibuf->zbuf_float = rv->rectz;
1291
1292         /* float factor for random dither, imbuf takes care of it */
1293         ibuf->dither = rd->dither_intensity;
1294         
1295         /* prepare to gamma correct to sRGB color space
1296          * note that sequence editor can generate 8bpc render buffers
1297          */
1298         if (ibuf->rect) {
1299                 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)) {
1300                         if (rd->im_format.depth == R_IMF_CHAN_DEPTH_8) {
1301                                 /* Higher depth bits are supported but not needed for current file output. */
1302                                 ibuf->rect_float = NULL;
1303                         }
1304                         else {
1305                                 IMB_float_from_rect(ibuf);
1306                         }
1307                 }
1308                 else {
1309                         /* ensure no float buffer remained from previous frame */
1310                         ibuf->rect_float = NULL;
1311                 }
1312         }
1313
1314         /* color -> grayscale */
1315         /* editing directly would alter the render view */
1316         if (rd->im_format.planes == R_IMF_PLANES_BW) {
1317                 ImBuf *ibuf_bw = IMB_dupImBuf(ibuf);
1318                 IMB_color_to_bw(ibuf_bw);
1319                 IMB_freeImBuf(ibuf);
1320                 ibuf = ibuf_bw;
1321         }
1322
1323         return ibuf;
1324 }
1325
1326 void RE_render_result_rect_from_ibuf(RenderResult *rr, RenderData *UNUSED(rd), ImBuf *ibuf, const int view_id)
1327 {
1328         RenderView *rv = RE_RenderViewGetById(rr, view_id);
1329
1330         if (ibuf->rect_float) {
1331                 rr->have_combined = true;
1332
1333                 if (!rv->rectf)
1334                         rv->rectf = MEM_mallocN(4 * sizeof(float) * rr->rectx * rr->recty, "render_seq rectf");
1335                 
1336                 memcpy(rv->rectf, ibuf->rect_float, 4 * sizeof(float) * rr->rectx * rr->recty);
1337
1338                 /* TSK! Since sequence render doesn't free the *rr render result, the old rect32
1339                  * can hang around when sequence render has rendered a 32 bits one before */
1340                 MEM_SAFE_FREE(rv->rect32);
1341         }
1342         else if (ibuf->rect) {
1343                 rr->have_combined = true;
1344
1345                 if (!rv->rect32)
1346                         rv->rect32 = MEM_mallocN(sizeof(int) * rr->rectx * rr->recty, "render_seq rect");
1347
1348                 memcpy(rv->rect32, ibuf->rect, 4 * rr->rectx * rr->recty);
1349
1350                 /* Same things as above, old rectf can hang around from previous render. */
1351                 MEM_SAFE_FREE(rv->rectf);
1352         }
1353 }
1354
1355 void render_result_rect_fill_zero(RenderResult *rr, const int view_id)
1356 {
1357         RenderView *rv = RE_RenderViewGetById(rr, view_id);
1358
1359         if (rv->rectf)
1360                 memset(rv->rectf, 0, 4 * sizeof(float) * rr->rectx * rr->recty);
1361         else if (rv->rect32)
1362                 memset(rv->rect32, 0, 4 * rr->rectx * rr->recty);
1363         else
1364                 rv->rect32 = MEM_callocN(sizeof(int) * rr->rectx * rr->recty, "render_seq rect");
1365 }
1366
1367 void render_result_rect_get_pixels(RenderResult *rr, unsigned int *rect, int rectx, int recty,
1368                                    const ColorManagedViewSettings *view_settings, const ColorManagedDisplaySettings *display_settings,
1369                                    const int view_id)
1370 {
1371         RenderView *rv = RE_RenderViewGetById(rr, view_id);
1372
1373         if (rv->rect32)
1374                 memcpy(rect, rv->rect32, sizeof(int) * rr->rectx * rr->recty);
1375         else if (rv->rectf)
1376                 IMB_display_buffer_transform_apply((unsigned char *) rect, rv->rectf, rr->rectx, rr->recty, 4,
1377                                                    view_settings, display_settings, true);
1378         else
1379                 /* else fill with black */
1380                 memset(rect, 0, sizeof(int) * rectx * recty);
1381 }
1382
1383
1384 /*************************** multiview functions *****************************/
1385
1386 bool RE_HasCombinedLayer(RenderResult *res)
1387 {
1388         RenderView *rv;
1389
1390         if (res == NULL)
1391                 return false;
1392
1393         rv = res->views.first;
1394         if (rv == NULL)
1395                 return false;
1396
1397         return (rv->rect32 || rv->rectf);
1398 }
1399
1400 bool RE_HasFloatPixels(RenderResult *res)
1401 {
1402         RenderView *rview;
1403
1404         for (rview = res->views.first; rview; rview = rview->next) {
1405                 if (rview->rect32 && !rview->rectf) {
1406                         return false;
1407                 }
1408         }
1409
1410         return true;
1411 }
1412
1413 bool RE_RenderResult_is_stereo(RenderResult *res)
1414 {
1415         if (! BLI_findstring(&res->views, STEREO_LEFT_NAME, offsetof(RenderView, name)))
1416                 return false;
1417
1418         if (! BLI_findstring(&res->views, STEREO_RIGHT_NAME, offsetof(RenderView, name)))
1419                 return false;
1420
1421         return true;
1422 }
1423
1424 RenderView *RE_RenderViewGetById(RenderResult *res, const int view_id)
1425 {
1426         RenderView *rv = BLI_findlink(&res->views, view_id);
1427         BLI_assert(res->views.first);
1428         return rv ? rv : res->views.first;
1429 }
1430
1431 RenderView *RE_RenderViewGetByName(RenderResult *res, const char *viewname)
1432 {
1433         RenderView *rv = BLI_findstring(&res->views, viewname, offsetof(RenderView, name));
1434         BLI_assert(res->views.first);
1435         return rv ? rv : res->views.first;
1436 }
1437
1438 static RenderPass *duplicate_render_pass(RenderPass *rpass)
1439 {
1440         RenderPass *new_rpass = MEM_mallocN(sizeof(RenderPass), "new render pass");
1441         *new_rpass = *rpass;
1442         new_rpass->next = new_rpass->prev = NULL;
1443         if (new_rpass->rect != NULL) {
1444                 new_rpass->rect = MEM_dupallocN(new_rpass->rect);
1445         }
1446         return new_rpass;
1447 }
1448
1449 static RenderLayer *duplicate_render_layer(RenderLayer *rl)
1450 {
1451         RenderLayer *new_rl = MEM_mallocN(sizeof(RenderLayer), "new render layer");
1452         *new_rl = *rl;
1453         new_rl->next = new_rl->prev = NULL;
1454         new_rl->passes.first = new_rl->passes.last = NULL;
1455         new_rl->exrhandle = NULL;
1456         if (new_rl->acolrect != NULL) {
1457                 new_rl->acolrect = MEM_dupallocN(new_rl->acolrect);
1458         }
1459         if (new_rl->scolrect != NULL) {
1460                 new_rl->scolrect = MEM_dupallocN(new_rl->scolrect);
1461         }
1462         if (new_rl->display_buffer != NULL) {
1463                 new_rl->display_buffer = MEM_dupallocN(new_rl->display_buffer);
1464         }
1465         for (RenderPass *rpass = rl->passes.first; rpass != NULL; rpass = rpass->next) {
1466                 RenderPass  *new_rpass = duplicate_render_pass(rpass);
1467                 BLI_addtail(&new_rl->passes, new_rpass);
1468         }
1469         return new_rl;
1470 }
1471
1472 static RenderView *duplicate_render_view(RenderView *rview)
1473 {
1474         RenderView *new_rview = MEM_mallocN(sizeof(RenderView), "new render view");
1475         *new_rview = *rview;
1476         if (new_rview->rectf != NULL) {
1477                 new_rview->rectf = MEM_dupallocN(new_rview->rectf);
1478         }
1479         if (new_rview->rectz != NULL) {
1480                 new_rview->rectz = MEM_dupallocN(new_rview->rectz);
1481         }
1482         if (new_rview->rect32 != NULL) {
1483                 new_rview->rect32 = MEM_dupallocN(new_rview->rect32);
1484         }
1485         return new_rview;
1486 }
1487
1488 RenderResult *RE_DuplicateRenderResult(RenderResult *rr)
1489 {
1490         RenderResult *new_rr = MEM_mallocN(sizeof(RenderResult), "new duplicated render result");
1491         *new_rr = *rr;
1492         new_rr->next = new_rr->prev = NULL;
1493         new_rr->layers.first = new_rr->layers.last = NULL;
1494         new_rr->views.first = new_rr->views.last = NULL;
1495         for (RenderLayer *rl = rr->layers.first; rl != NULL; rl = rl->next) {
1496                 RenderLayer *new_rl = duplicate_render_layer(rl);
1497                 BLI_addtail(&new_rr->layers, new_rl);
1498         }
1499         for (RenderView *rview = rr->views.first; rview != NULL; rview = rview->next) {
1500                 RenderView *new_rview = duplicate_render_view(rview);
1501                 BLI_addtail(&new_rr->views, new_rview);
1502         }
1503         if (new_rr->rect32 != NULL) {
1504                 new_rr->rect32 = MEM_dupallocN(new_rr->rect32);
1505         }
1506         if (new_rr->rectf != NULL) {
1507                 new_rr->rectf = MEM_dupallocN(new_rr->rectf);
1508         }
1509         if (new_rr->rectz != NULL) {
1510                 new_rr->rectz = MEM_dupallocN(new_rr->rectz);
1511         }
1512         new_rr->stamp_data = MEM_dupallocN(new_rr->stamp_data);
1513         return new_rr;
1514 }