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