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