Merge branch 'master' into blender2.8
[blender.git] / source / blender / render / intern / source / render_result.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2006 Blender Foundation.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/render/intern/source/render_result.c
29  *  \ingroup render
30  */
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <errno.h>
36
37 #include "MEM_guardedalloc.h"
38
39 #include "BLI_utildefines.h"
40 #include "BLI_listbase.h"
41 #include "BLI_hash_md5.h"
42 #include "BLI_path_util.h"
43 #include "BLI_rect.h"
44 #include "BLI_string.h"
45 #include "BLI_threads.h"
46
47 #include "BKE_appdir.h"
48 #include "BKE_image.h"
49 #include "BKE_global.h"
50 #include "BKE_main.h"
51 #include "BKE_report.h"
52 #include "BKE_camera.h"
53 #include "BKE_scene.h"
54
55 #include "IMB_imbuf.h"
56 #include "IMB_imbuf_types.h"
57 #include "IMB_colormanagement.h"
58
59 #include "intern/openexr/openexr_multi.h"
60
61 #include "render_result.h"
62 #include "render_types.h"
63
64 /********************************** Free *************************************/
65
66 static void render_result_views_free(RenderResult *res)
67 {
68         while (res->views.first) {
69                 RenderView *rv = res->views.first;
70                 BLI_remlink(&res->views, rv);
71
72                 if (rv->rect32)
73                         MEM_freeN(rv->rect32);
74
75                 if (rv->rectz)
76                         MEM_freeN(rv->rectz);
77
78                 if (rv->rectf)
79                         MEM_freeN(rv->rectf);
80
81                 MEM_freeN(rv);
82         }
83 }
84
85 void render_result_free(RenderResult *res)
86 {
87         if (res == NULL) return;
88
89         while (res->layers.first) {
90                 RenderLayer *rl = res->layers.first;
91
92                 /* acolrect and scolrect are optionally allocated in shade_tile, only free here since it can be used for drawing */
93                 if (rl->acolrect) MEM_freeN(rl->acolrect);
94                 if (rl->scolrect) MEM_freeN(rl->scolrect);
95                 if (rl->display_buffer) MEM_freeN(rl->display_buffer);
96                 
97                 while (rl->passes.first) {
98                         RenderPass *rpass = rl->passes.first;
99                         if (rpass->rect) MEM_freeN(rpass->rect);
100                         BLI_remlink(&rl->passes, rpass);
101                         MEM_freeN(rpass);
102                 }
103                 BLI_remlink(&res->layers, rl);
104                 MEM_freeN(rl);
105         }
106
107         render_result_views_free(res);
108
109         if (res->rect32)
110                 MEM_freeN(res->rect32);
111         if (res->rectz)
112                 MEM_freeN(res->rectz);
113         if (res->rectf)
114                 MEM_freeN(res->rectf);
115         if (res->text)
116                 MEM_freeN(res->text);
117         if (res->error)
118                 MEM_freeN(res->error);
119
120         BKE_stamp_data_free(res->stamp_data);
121
122         MEM_freeN(res);
123 }
124
125 /* version that's compatible with fullsample buffers */
126 void render_result_free_list(ListBase *lb, RenderResult *rr)
127 {
128         RenderResult *rrnext;
129         
130         for (; rr; rr = rrnext) {
131                 rrnext = rr->next;
132                 
133                 if (lb && lb->first)
134                         BLI_remlink(lb, rr);
135                 
136                 render_result_free(rr);
137         }
138 }
139
140 /********************************* multiview *************************************/
141
142 /* create a new views Listbase in rr without duplicating the memory pointers */
143 void render_result_views_shallowcopy(RenderResult *dst, RenderResult *src)
144 {
145         RenderView *rview;
146
147         if (dst == NULL || src == NULL)
148                 return;
149
150         for (rview = src->views.first; rview; rview = rview->next) {
151                 RenderView *rv;
152
153                 rv = MEM_mallocN(sizeof(RenderView), "new render view");
154                 BLI_addtail(&dst->views, rv);
155
156                 BLI_strncpy(rv->name, rview->name, sizeof(rv->name));
157                 rv->rectf = rview->rectf;
158                 rv->rectz = rview->rectz;
159                 rv->rect32 = rview->rect32;
160         }
161 }
162
163 /* free the views created temporarily */
164 void render_result_views_shallowdelete(RenderResult *rr)
165 {
166         if (rr == NULL)
167                 return;
168
169         while (rr->views.first) {
170                 RenderView *rv = rr->views.first;
171                 BLI_remlink(&rr->views, rv);
172                 MEM_freeN(rv);
173         }
174 }
175
176
177 static char* set_pass_name(char *outname, const char *name, int channel, const char *chan_id)
178 {
179         BLI_strncpy(outname, name, EXR_PASS_MAXNAME);
180         if (channel >= 0) {
181                 char token[3] = {'.', chan_id[channel], '\0'};
182                 strncat(outname, token, EXR_PASS_MAXNAME);
183         }
184         return outname;
185 }
186
187 static void set_pass_full_name(char *fullname, const char *name, int channel, const char *view, const char *chan_id)
188 {
189         BLI_strncpy(fullname, name, EXR_PASS_MAXNAME);
190         if (view && view[0]) {
191                 strncat(fullname, ".", EXR_PASS_MAXNAME);
192                 strncat(fullname, view, EXR_PASS_MAXNAME);
193         }
194         if (channel >= 0) {
195                 char token[3] = {'.', chan_id[channel], '\0'};
196                 strncat(fullname, token, EXR_PASS_MAXNAME);
197         }
198 }
199
200 /********************************** New **************************************/
201
202 static RenderPass *render_layer_add_pass(RenderResult *rr, RenderLayer *rl, int channels, const char *name, const char *viewname, const char *chan_id)
203 {
204         const int view_id = BLI_findstringindex(&rr->views, viewname, offsetof(RenderView, name));
205         RenderPass *rpass = MEM_callocN(sizeof(RenderPass), name);
206         size_t rectsize = ((size_t)rr->rectx) * rr->recty * channels;
207         
208         rpass->channels = channels;
209         rpass->rectx = rl->rectx;
210         rpass->recty = rl->recty;
211         rpass->view_id = view_id;
212
213         BLI_strncpy(rpass->name, name, sizeof(rpass->name));
214         BLI_strncpy(rpass->chan_id, chan_id, sizeof(rpass->chan_id));
215         BLI_strncpy(rpass->view, viewname, sizeof(rpass->view));
216         set_pass_full_name(rpass->fullname, rpass->name, -1, rpass->view, rpass->chan_id);
217         
218         if (rl->exrhandle) {
219                 int a;
220                 for (a = 0; a < channels; a++) {
221                         char passname[EXR_PASS_MAXNAME];
222                         IMB_exr_add_channel(rl->exrhandle, rl->name, set_pass_name(passname, rpass->name, a, rpass->chan_id), viewname, 0, 0, NULL, false);
223                 }
224         }
225         else {
226                 float *rect;
227                 int x;
228                 
229                 rpass->rect = MEM_mapallocN(sizeof(float) * rectsize, name);
230                 if (rpass->rect == NULL) {
231                         MEM_freeN(rpass);
232                         return NULL;
233                 }
234                 
235                 if (STREQ(rpass->name, RE_PASSNAME_VECTOR)) {
236                         /* initialize to max speed */
237                         rect = rpass->rect;
238                         for (x = rectsize - 1; x >= 0; x--)
239                                 rect[x] = PASS_VECTOR_MAX;
240                 }
241                 else if (STREQ(rpass->name, RE_PASSNAME_Z)) {
242                         rect = rpass->rect;
243                         for (x = rectsize - 1; x >= 0; x--)
244                                 rect[x] = 10e10;
245                 }
246         }
247
248         BLI_addtail(&rl->passes, rpass);
249
250         return rpass;
251 }
252 /* wrapper called from render_opengl */
253 RenderPass *gp_add_pass(RenderResult *rr, RenderLayer *rl, int channels, const char *name, const char *viewname)
254 {
255         return render_layer_add_pass(rr, rl, channels, name, viewname, "RGBA");
256 }
257
258 /* called by main render as well for parts */
259 /* will read info from Render *re to define layers */
260 /* called in threads */
261 /* re->winx,winy is coordinate space of entire image, partrct the part within */
262 RenderResult *render_result_new(Render *re, rcti *partrct, int crop, int savebuffers, const char *layername, const char *viewname)
263 {
264         RenderResult *rr;
265         RenderLayer *rl;
266         RenderView *rv;
267         ViewLayer *view_layer;
268         int rectx, recty;
269         int nr;
270         
271         rectx = BLI_rcti_size_x(partrct);
272         recty = BLI_rcti_size_y(partrct);
273         
274         if (rectx <= 0 || recty <= 0)
275                 return NULL;
276         
277         rr = MEM_callocN(sizeof(RenderResult), "new render result");
278         rr->rectx = rectx;
279         rr->recty = recty;
280         rr->renrect.xmin = 0; rr->renrect.xmax = rectx - 2 * crop;
281         /* crop is one or two extra pixels rendered for filtering, is used for merging and display too */
282         rr->crop = crop;
283
284         /* tilerect is relative coordinates within render disprect. do not subtract crop yet */
285         rr->tilerect.xmin = partrct->xmin - re->disprect.xmin;
286         rr->tilerect.xmax = partrct->xmax - re->disprect.xmin;
287         rr->tilerect.ymin = partrct->ymin - re->disprect.ymin;
288         rr->tilerect.ymax = partrct->ymax - re->disprect.ymin;
289         
290         if (savebuffers) {
291                 rr->do_exr_tile = true;
292         }
293
294         render_result_views_new(rr, &re->r);
295
296         /* check renderdata for amount of layers */
297         for (nr = 0, view_layer = re->view_layers.first; view_layer; view_layer = view_layer->next, nr++) {
298
299                 if (layername && layername[0])
300                         if (!STREQ(view_layer->name, layername))
301                                 continue;
302
303                 if (re->r.scemode & R_SINGLE_LAYER) {
304                         if (nr != re->active_view_layer) {
305                                 continue;
306                         }
307                 }
308                 else {
309                         if ((view_layer->flag & VIEW_LAYER_RENDER) == 0) {
310                                 continue;
311                         }
312                 }
313                 
314                 rl = MEM_callocN(sizeof(RenderLayer), "new render layer");
315                 BLI_addtail(&rr->layers, rl);
316                 
317                 BLI_strncpy(rl->name, view_layer->name, sizeof(rl->name));
318                 rl->layflag = view_layer->layflag;
319                 rl->passflag = view_layer->passflag; /* for debugging: view_layer->passflag | SCE_PASS_RAYHITS; */
320                 rl->pass_xor = view_layer->pass_xor;
321                 rl->rectx = rectx;
322                 rl->recty = recty;
323                 
324                 if (rr->do_exr_tile) {
325                         rl->display_buffer = MEM_mapallocN((size_t)rectx * recty * sizeof(unsigned int),
326                                                            "Combined display space rgba");
327                         if (rl->display_buffer == NULL) {
328                                 render_result_free(rr);
329                                 return NULL;
330                         }
331                         rl->exrhandle = IMB_exr_get_handle();
332                 }
333
334                 for (rv = rr->views.first; rv; rv = rv->next) {
335                         const char *view = rv->name;
336
337                         if (viewname && viewname[0])
338                                 if (!STREQ(view, viewname))
339                                         continue;
340
341                         if (rr->do_exr_tile)
342                                 IMB_exr_add_view(rl->exrhandle, view);
343
344 #define RENDER_LAYER_ADD_PASS_SAFE(rr, rl, channels, name, viewname, chan_id) \
345                         do { \
346                                 if (render_layer_add_pass(rr, rl, channels, name, viewname, chan_id) == NULL) { \
347                                         render_result_free(rr); \
348                                         return NULL; \
349                                 } \
350                         } while (false)
351
352                         /* a renderlayer should always have a Combined pass*/
353                         render_layer_add_pass(rr, rl, 4, "Combined", view, "RGBA");
354
355                         if (view_layer->passflag  & SCE_PASS_Z)
356                                 RENDER_LAYER_ADD_PASS_SAFE(rr, rl, 1, RE_PASSNAME_Z, view, "Z");
357                         if (view_layer->passflag  & SCE_PASS_VECTOR)
358                                 RENDER_LAYER_ADD_PASS_SAFE(rr, rl, 4, RE_PASSNAME_VECTOR, view, "XYZW");
359                         if (view_layer->passflag  & SCE_PASS_NORMAL)
360                                 RENDER_LAYER_ADD_PASS_SAFE(rr, rl, 3, RE_PASSNAME_NORMAL, view, "XYZ");
361                         if (view_layer->passflag  & SCE_PASS_UV)
362                                 RENDER_LAYER_ADD_PASS_SAFE(rr, rl, 3, RE_PASSNAME_UV, view, "UVA");
363                         if (view_layer->passflag  & SCE_PASS_RGBA)
364                                 RENDER_LAYER_ADD_PASS_SAFE(rr, rl, 4, RE_PASSNAME_RGBA, view, "RGBA");
365                         if (view_layer->passflag  & SCE_PASS_EMIT)
366                                 RENDER_LAYER_ADD_PASS_SAFE(rr, rl, 3, RE_PASSNAME_EMIT, view, "RGB");
367                         if (view_layer->passflag  & SCE_PASS_DIFFUSE)
368                                 RENDER_LAYER_ADD_PASS_SAFE(rr, rl, 3, RE_PASSNAME_DIFFUSE, view, "RGB");
369                         if (view_layer->passflag  & SCE_PASS_SPEC)
370                                 RENDER_LAYER_ADD_PASS_SAFE(rr, rl, 3, RE_PASSNAME_SPEC, view, "RGB");
371                         if (view_layer->passflag  & SCE_PASS_AO)
372                                 RENDER_LAYER_ADD_PASS_SAFE(rr, rl, 3, RE_PASSNAME_AO, view, "RGB");
373                         if (view_layer->passflag  & SCE_PASS_ENVIRONMENT)
374                                 RENDER_LAYER_ADD_PASS_SAFE(rr, rl, 3, RE_PASSNAME_ENVIRONMENT, view, "RGB");
375                         if (view_layer->passflag  & SCE_PASS_INDIRECT)
376                                 RENDER_LAYER_ADD_PASS_SAFE(rr, rl, 3, RE_PASSNAME_INDIRECT, view, "RGB");
377                         if (view_layer->passflag  & SCE_PASS_SHADOW)
378                                 RENDER_LAYER_ADD_PASS_SAFE(rr, rl, 3, RE_PASSNAME_SHADOW, view, "RGB");
379                         if (view_layer->passflag  & SCE_PASS_REFLECT)
380                                 RENDER_LAYER_ADD_PASS_SAFE(rr, rl, 3, RE_PASSNAME_REFLECT, view, "RGB");
381                         if (view_layer->passflag  & SCE_PASS_REFRACT)
382                                 RENDER_LAYER_ADD_PASS_SAFE(rr, rl, 3, RE_PASSNAME_REFRACT, view, "RGB");
383                         if (view_layer->passflag  & SCE_PASS_INDEXOB)
384                                 RENDER_LAYER_ADD_PASS_SAFE(rr, rl, 1, RE_PASSNAME_INDEXOB, view, "X");
385                         if (view_layer->passflag  & SCE_PASS_INDEXMA)
386                                 RENDER_LAYER_ADD_PASS_SAFE(rr, rl, 1, RE_PASSNAME_INDEXMA, view, "X");
387                         if (view_layer->passflag  & SCE_PASS_MIST)
388                                 RENDER_LAYER_ADD_PASS_SAFE(rr, rl, 1, RE_PASSNAME_MIST, view, "Z");
389                         if (rl->passflag & SCE_PASS_RAYHITS)
390                                 RENDER_LAYER_ADD_PASS_SAFE(rr, rl, 4, RE_PASSNAME_RAYHITS, view, "RGB");
391                         if (view_layer->passflag  & SCE_PASS_DIFFUSE_DIRECT)
392                                 RENDER_LAYER_ADD_PASS_SAFE(rr, rl, 3, RE_PASSNAME_DIFFUSE_DIRECT, view, "RGB");
393                         if (view_layer->passflag  & SCE_PASS_DIFFUSE_INDIRECT)
394                                 RENDER_LAYER_ADD_PASS_SAFE(rr, rl, 3, RE_PASSNAME_DIFFUSE_INDIRECT, view, "RGB");
395                         if (view_layer->passflag  & SCE_PASS_DIFFUSE_COLOR)
396                                 RENDER_LAYER_ADD_PASS_SAFE(rr, rl, 3, RE_PASSNAME_DIFFUSE_COLOR, view, "RGB");
397                         if (view_layer->passflag  & SCE_PASS_GLOSSY_DIRECT)
398                                 RENDER_LAYER_ADD_PASS_SAFE(rr, rl, 3, RE_PASSNAME_GLOSSY_DIRECT, view, "RGB");
399                         if (view_layer->passflag  & SCE_PASS_GLOSSY_INDIRECT)
400                                 RENDER_LAYER_ADD_PASS_SAFE(rr, rl, 3, RE_PASSNAME_GLOSSY_INDIRECT, view, "RGB");
401                         if (view_layer->passflag  & SCE_PASS_GLOSSY_COLOR)
402                                 RENDER_LAYER_ADD_PASS_SAFE(rr, rl, 3, RE_PASSNAME_GLOSSY_COLOR, view, "RGB");
403                         if (view_layer->passflag  & SCE_PASS_TRANSM_DIRECT)
404                                 RENDER_LAYER_ADD_PASS_SAFE(rr, rl, 3, RE_PASSNAME_TRANSM_DIRECT, view, "RGB");
405                         if (view_layer->passflag  & SCE_PASS_TRANSM_INDIRECT)
406                                 RENDER_LAYER_ADD_PASS_SAFE(rr, rl, 3, RE_PASSNAME_TRANSM_INDIRECT, view, "RGB");
407                         if (view_layer->passflag  & SCE_PASS_TRANSM_COLOR)
408                                 RENDER_LAYER_ADD_PASS_SAFE(rr, rl, 3, RE_PASSNAME_TRANSM_COLOR, view, "RGB");
409                         if (view_layer->passflag  & SCE_PASS_SUBSURFACE_DIRECT)
410                                 RENDER_LAYER_ADD_PASS_SAFE(rr, rl, 3, RE_PASSNAME_SUBSURFACE_DIRECT, view, "RGB");
411                         if (view_layer->passflag  & SCE_PASS_SUBSURFACE_INDIRECT)
412                                 RENDER_LAYER_ADD_PASS_SAFE(rr, rl, 3, RE_PASSNAME_SUBSURFACE_INDIRECT, view, "RGB");
413                         if (view_layer->passflag  & SCE_PASS_SUBSURFACE_COLOR)
414                                 RENDER_LAYER_ADD_PASS_SAFE(rr, rl, 3, RE_PASSNAME_SUBSURFACE_COLOR, view, "RGB");
415 #undef RENDER_LAYER_ADD_PASS_SAFE
416                 }
417         }
418         /* sss, previewrender and envmap don't do layers, so we make a default one */
419         if (BLI_listbase_is_empty(&rr->layers) && !(layername && layername[0])) {
420                 rl = MEM_callocN(sizeof(RenderLayer), "new render layer");
421                 BLI_addtail(&rr->layers, rl);
422                 
423                 rl->rectx = rectx;
424                 rl->recty = recty;
425
426                 /* duplicate code... */
427                 if (rr->do_exr_tile) {
428                         rl->display_buffer = MEM_mapallocN(rectx * recty * sizeof(unsigned int), "Combined display space rgba");
429                         rl->exrhandle = IMB_exr_get_handle();
430                 }
431
432                 for (rv = rr->views.first; rv; rv = rv->next) {
433                         const char *view = rv->name;
434
435                         if (viewname && viewname[0])
436                                 if (strcmp(view, viewname) != 0)
437                                         continue;
438
439                         if (rr->do_exr_tile)
440                                 IMB_exr_add_view(rl->exrhandle, view);
441
442                         /* a renderlayer should always have a Combined pass */
443                         render_layer_add_pass(rr, rl, 4, RE_PASSNAME_COMBINED, view, "RGBA");
444                 }
445
446                 /* note, this has to be in sync with scene.c */
447                 rl->layflag = 0x7FFF;    /* solid ztra halo strand */
448                 rl->passflag = SCE_PASS_COMBINED;
449                 
450                 re->active_view_layer = 0;
451         }
452         
453         /* border render; calculate offset for use in compositor. compo is centralized coords */
454         /* XXX obsolete? I now use it for drawing border render offset (ton) */
455         rr->xof = re->disprect.xmin + BLI_rcti_cent_x(&re->disprect) - (re->winx / 2);
456         rr->yof = re->disprect.ymin + BLI_rcti_cent_y(&re->disprect) - (re->winy / 2);
457         
458         return rr;
459 }
460
461 void render_result_clone_passes(Render *re, RenderResult *rr, const char *viewname)
462 {
463         RenderLayer *rl;
464         RenderPass *main_rp;
465
466         for (rl = rr->layers.first; rl; rl = rl->next) {
467                 RenderLayer *main_rl = BLI_findstring(&re->result->layers, rl->name, offsetof(RenderLayer, name));
468                 if (!main_rl) {
469                         continue;
470                 }
471
472                 for (main_rp = main_rl->passes.first; main_rp; main_rp = main_rp->next) {
473                         if (viewname && viewname[0] && !STREQ(main_rp->view, viewname)) {
474                                 continue;
475                         }
476
477                         /* Compare fullname to make sure that the view also is equal. */
478                         RenderPass *rp = BLI_findstring(&rl->passes, main_rp->fullname, offsetof(RenderPass, fullname));
479                         if (!rp) {
480                                 render_layer_add_pass(rr, rl, main_rp->channels, main_rp->name, main_rp->view, main_rp->chan_id);
481                         }
482                 }
483         }
484 }
485
486 void render_result_add_pass(RenderResult *rr, const char *name, int channels, const char *chan_id, const char *layername, const char *viewname)
487 {
488         RenderLayer *rl;
489         RenderPass *rp;
490         RenderView *rv;
491
492         for (rl = rr->layers.first; rl; rl = rl->next) {
493                 if (layername && layername[0] && !STREQ(rl->name, layername)) {
494                         continue;
495                 }
496
497                 for (rv = rr->views.first; rv; rv = rv->next) {
498                         const char *view = rv->name;
499
500                         if (viewname && viewname[0] && !STREQ(view, viewname)) continue;
501
502                         /* Ensure that the pass doesn't exist yet. */
503                         for (rp = rl->passes.first; rp; rp = rp->next) {
504                                 if (!STREQ(rp->name, name)) continue;
505                                 if (!STREQ(rp->view, view)) continue;
506                         }
507
508                         if (!rp) {
509                                 render_layer_add_pass(rr, rl, channels, name, view, chan_id);
510                         }
511                 }
512         }
513 }
514
515 /* allocate osa new results for samples */
516 RenderResult *render_result_new_full_sample(Render *re, ListBase *lb, rcti *partrct, int crop, int savebuffers, const char *viewname)
517 {
518         int a;
519         
520         if (re->osa == 0)
521                 return render_result_new(re, partrct, crop, savebuffers, RR_ALL_LAYERS, viewname);
522         
523         for (a = 0; a < re->osa; a++) {
524                 RenderResult *rr = render_result_new(re, partrct, crop, savebuffers, RR_ALL_LAYERS, viewname);
525                 BLI_addtail(lb, rr);
526                 rr->sample_nr = a;
527         }
528         
529         return lb->first;
530 }
531
532 static int passtype_from_name(const char *name)
533 {
534         const char delim[] = {'.', '\0'};
535         const char *sep, *suf;
536         int len = BLI_str_partition(name, delim, &sep, &suf);
537
538 #define CHECK_PASS(NAME) if (STREQLEN(name, RE_PASSNAME_ ## NAME, len)) return SCE_PASS_ ## NAME
539
540         CHECK_PASS(COMBINED);
541         CHECK_PASS(Z);
542         CHECK_PASS(VECTOR);
543         CHECK_PASS(NORMAL);
544         CHECK_PASS(UV);
545         CHECK_PASS(RGBA);
546         CHECK_PASS(EMIT);
547         CHECK_PASS(DIFFUSE);
548         CHECK_PASS(SPEC);
549         CHECK_PASS(SHADOW);
550         CHECK_PASS(AO);
551         CHECK_PASS(ENVIRONMENT);
552         CHECK_PASS(INDIRECT);
553         CHECK_PASS(REFLECT);
554         CHECK_PASS(REFRACT);
555         CHECK_PASS(INDEXOB);
556         CHECK_PASS(INDEXMA);
557         CHECK_PASS(MIST);
558         CHECK_PASS(RAYHITS);
559         CHECK_PASS(DIFFUSE_DIRECT);
560         CHECK_PASS(DIFFUSE_INDIRECT);
561         CHECK_PASS(DIFFUSE_COLOR);
562         CHECK_PASS(GLOSSY_DIRECT);
563         CHECK_PASS(GLOSSY_INDIRECT);
564         CHECK_PASS(GLOSSY_COLOR);
565         CHECK_PASS(TRANSM_DIRECT);
566         CHECK_PASS(TRANSM_INDIRECT);
567         CHECK_PASS(TRANSM_COLOR);
568         CHECK_PASS(SUBSURFACE_DIRECT);
569         CHECK_PASS(SUBSURFACE_INDIRECT);
570         CHECK_PASS(SUBSURFACE_COLOR);
571
572 #undef CHECK_PASS
573         return 0;
574 }
575
576 /* callbacks for render_result_new_from_exr */
577 static void *ml_addlayer_cb(void *base, const char *str)
578 {
579         RenderResult *rr = base;
580         RenderLayer *rl;
581         
582         rl = MEM_callocN(sizeof(RenderLayer), "new render layer");
583         BLI_addtail(&rr->layers, rl);
584
585         BLI_strncpy(rl->name, str, EXR_LAY_MAXNAME);
586         return rl;
587 }
588
589 static void ml_addpass_cb(void *base, void *lay, const char *name, float *rect, int totchan, const char *chan_id, const char *view)
590 {
591         RenderResult *rr = base;
592         RenderLayer *rl = lay;
593         RenderPass *rpass = MEM_callocN(sizeof(RenderPass), "loaded pass");
594
595         BLI_addtail(&rl->passes, rpass);
596         rpass->channels = totchan;
597         rl->passflag |= passtype_from_name(name);
598
599         /* channel id chars */
600         BLI_strncpy(rpass->chan_id, chan_id, sizeof(rpass->chan_id));
601
602         rpass->rect = rect;
603         BLI_strncpy(rpass->name, name, EXR_PASS_MAXNAME);
604         BLI_strncpy(rpass->view, view, sizeof(rpass->view));
605         set_pass_full_name(rpass->fullname, name, -1, view, rpass->chan_id);
606
607         if (view[0] != '\0') {
608                 rpass->view_id = BLI_findstringindex(&rr->views, view, offsetof(RenderView, name));
609         }
610         else {
611                 rpass->view_id = 0;
612         }
613 }
614
615 static void *ml_addview_cb(void *base, const char *str)
616 {
617         RenderResult *rr = base;
618         RenderView *rv;
619
620         rv = MEM_callocN(sizeof(RenderView), "new render view");
621         BLI_strncpy(rv->name, str, EXR_VIEW_MAXNAME);
622
623         /* For stereo drawing we need to ensure:
624          * STEREO_LEFT_NAME  == STEREO_LEFT_ID and
625          * STEREO_RIGHT_NAME == STEREO_RIGHT_ID */
626
627         if (STREQ(str, STEREO_LEFT_NAME)) {
628                 BLI_addhead(&rr->views, rv);
629         }
630         else if (STREQ(str, STEREO_RIGHT_NAME)) {
631                 RenderView *left_rv = BLI_findstring(&rr->views, STEREO_LEFT_NAME, offsetof(RenderView, name));
632
633                 if (left_rv == NULL) {
634                         BLI_addhead(&rr->views, rv);
635                 }
636                 else {
637                         BLI_insertlinkafter(&rr->views, left_rv, rv);
638                 }
639         }
640         else {
641                 BLI_addtail(&rr->views, rv);
642         }
643
644         return rv;
645 }
646
647 static int order_render_passes(const void *a, const void *b)
648 {
649         // 1 if a is after b
650         RenderPass *rpa = (RenderPass *) a;
651         RenderPass *rpb = (RenderPass *) b;
652         unsigned int passtype_a = passtype_from_name(rpa->name);
653         unsigned int passtype_b = passtype_from_name(rpb->name);
654
655         /* Render passes with default type always go first. */
656         if (passtype_b && !passtype_a)
657                 return 1;
658         if (passtype_a && !passtype_b)
659                 return 0;
660
661         if (passtype_a && passtype_b) {
662                 if (passtype_a > passtype_b)
663                         return 1;
664                 else if (passtype_a < passtype_b)
665                         return 0;
666         }
667         else {
668                 int cmp = strncmp(rpa->name, rpb->name, EXR_PASS_MAXNAME);
669                 if (cmp > 0)
670                         return 1;
671                 if (cmp < 0)
672                         return 0;
673         }
674
675
676         /* they have the same type */
677         /* left first */
678         if (STREQ(rpa->view, STEREO_LEFT_NAME))
679                 return 0;
680         else if (STREQ(rpb->view, STEREO_LEFT_NAME))
681                 return 1;
682
683         /* right second */
684         if (STREQ(rpa->view, STEREO_RIGHT_NAME))
685                 return 0;
686         else if (STREQ(rpb->view, STEREO_RIGHT_NAME))
687                 return 1;
688
689         /* remaining in ascending id order */
690         return (rpa->view_id < rpb->view_id);
691 }
692
693 /* from imbuf, if a handle was returned and it's not a singlelayer multiview we convert this to render result */
694 RenderResult *render_result_new_from_exr(void *exrhandle, const char *colorspace, bool predivide, int rectx, int recty)
695 {
696         RenderResult *rr = MEM_callocN(sizeof(RenderResult), __func__);
697         RenderLayer *rl;
698         RenderPass *rpass;
699         const char *to_colorspace = IMB_colormanagement_role_colorspace_name_get(COLOR_ROLE_SCENE_LINEAR);
700
701         rr->rectx = rectx;
702         rr->recty = recty;
703         
704         IMB_exr_multilayer_convert(exrhandle, rr, ml_addview_cb, ml_addlayer_cb, ml_addpass_cb);
705
706         for (rl = rr->layers.first; rl; rl = rl->next) {
707                 rl->rectx = rectx;
708                 rl->recty = recty;
709
710                 BLI_listbase_sort(&rl->passes, order_render_passes);
711
712                 for (rpass = rl->passes.first; rpass; rpass = rpass->next) {
713                         rpass->rectx = rectx;
714                         rpass->recty = recty;
715
716                         if (rpass->channels >= 3) {
717                                 IMB_colormanagement_transform(rpass->rect, rpass->rectx, rpass->recty, rpass->channels,
718                                                               colorspace, to_colorspace, predivide);
719                         }
720                 }
721         }
722         
723         return rr;
724 }
725
726 void render_result_view_new(RenderResult *rr, const char *viewname)
727 {
728         RenderView *rv = MEM_callocN(sizeof(RenderView), "new render view");
729         BLI_addtail(&rr->views, rv);
730         BLI_strncpy(rv->name, viewname, sizeof(rv->name));
731 }
732
733 void render_result_views_new(RenderResult *rr, RenderData *rd)
734 {
735         SceneRenderView *srv;
736
737         /* clear previously existing views - for sequencer */
738         render_result_views_free(rr);
739
740         /* check renderdata for amount of views */
741         if ((rd->scemode & R_MULTIVIEW)) {
742                 for (srv = rd->views.first; srv; srv = srv->next) {
743                         if (BKE_scene_multiview_is_render_view_active(rd, srv) == false)
744                                 continue;
745                         render_result_view_new(rr, srv->name);
746                 }
747         }
748
749         /* we always need at least one view */
750         if (BLI_listbase_count_ex(&rr->views, 1) == 0) {
751                 render_result_view_new(rr, "");
752         }
753 }
754
755 bool render_result_has_views(RenderResult *rr)
756 {
757         RenderView *rv = rr->views.first;
758         return (rv && (rv->next || rv->name[0]));
759 }
760
761 /*********************************** Merge ***********************************/
762
763 static void do_merge_tile(RenderResult *rr, RenderResult *rrpart, float *target, float *tile, int pixsize)
764 {
765         int y, tilex, tiley;
766         size_t ofs, copylen;
767         
768         copylen = tilex = rrpart->rectx;
769         tiley = rrpart->recty;
770         
771         if (rrpart->crop) { /* filters add pixel extra */
772                 tile += pixsize * (rrpart->crop + ((size_t)rrpart->crop) * tilex);
773                 
774                 copylen = tilex - 2 * rrpart->crop;
775                 tiley -= 2 * rrpart->crop;
776                 
777                 ofs = (((size_t)rrpart->tilerect.ymin) + rrpart->crop) * rr->rectx + (rrpart->tilerect.xmin + rrpart->crop);
778                 target += pixsize * ofs;
779         }
780         else {
781                 ofs = (((size_t)rrpart->tilerect.ymin) * rr->rectx + rrpart->tilerect.xmin);
782                 target += pixsize * ofs;
783         }
784
785         copylen *= sizeof(float) * pixsize;
786         tilex *= pixsize;
787         ofs = pixsize * rr->rectx;
788
789         for (y = 0; y < tiley; y++) {
790                 memcpy(target, tile, copylen);
791                 target += ofs;
792                 tile += tilex;
793         }
794 }
795
796 /* used when rendering to a full buffer, or when reading the exr part-layer-pass file */
797 /* no test happens here if it fits... we also assume layers are in sync */
798 /* is used within threads */
799 void render_result_merge(RenderResult *rr, RenderResult *rrpart)
800 {
801         RenderLayer *rl, *rlp;
802         RenderPass *rpass, *rpassp;
803         
804         for (rl = rr->layers.first; rl; rl = rl->next) {
805                 rlp = RE_GetRenderLayer(rrpart, rl->name);
806                 if (rlp) {
807                         /* passes are allocated in sync */
808                         for (rpass = rl->passes.first, rpassp = rlp->passes.first;
809                              rpass && rpassp;
810                              rpass = rpass->next)
811                         {
812                                 /* renderresult have all passes, renderpart only the active view's passes */
813                                 if (strcmp(rpassp->fullname, rpass->fullname) != 0)
814                                         continue;
815
816                                 do_merge_tile(rr, rrpart, rpass->rect, rpassp->rect, rpass->channels);
817
818                                 /* manually get next render pass */
819                                 rpassp = rpassp->next;
820                         }
821                 }
822         }
823 }
824
825 /* Called from the UI and render pipeline, to save multilayer and multiview
826  * images, optionally isolating a specific, view, layer or RGBA/Z pass. */
827 bool RE_WriteRenderResult(ReportList *reports, RenderResult *rr, const char *filename, ImageFormatData *imf, const char *view, int layer)
828 {
829         void *exrhandle = IMB_exr_get_handle();
830         const bool half_float = (imf && imf->depth == R_IMF_CHAN_DEPTH_16);
831         const bool multi_layer = !(imf && imf->imtype == R_IMF_IMTYPE_OPENEXR);
832         const bool write_z = !multi_layer && (imf && (imf->flag & R_IMF_FLAG_ZBUF));
833
834         /* Write first layer if not multilayer and no layer was specified. */
835         if (!multi_layer && layer == -1) {
836                 layer = 0;
837         }
838
839         /* First add views since IMB_exr_add_channel checks number of views. */
840         if (render_result_has_views(rr)) {
841                 for (RenderView *rview = rr->views.first; rview; rview = rview->next) {
842                         if (!view || STREQ(view, rview->name)) {
843                                 IMB_exr_add_view(exrhandle, rview->name);
844                         }
845                 }
846         }
847
848         /* Compositing result. */
849         if (rr->have_combined) {
850                 for (RenderView *rview = rr->views.first; rview; rview = rview->next) {
851                         if (!rview->rectf) {
852                                 continue;
853                         }
854
855                         const char *viewname = rview->name;
856                         if (view) {
857                                 if (!STREQ(view, viewname)) {
858                                         continue;
859                                 }
860                                 else {
861                                         viewname = "";
862                                 }
863                         }
864
865                         /* Skip compositing if only a single other layer is requested. */
866                         if (!multi_layer && layer != 0) {
867                                 continue;
868                         }
869
870                         for (int a = 0; a < 4; a++) {
871                                 char passname[EXR_PASS_MAXNAME];
872                                 char layname[EXR_PASS_MAXNAME];
873                                 const char *chan_id = "RGBA";
874
875                                 if (multi_layer) {
876                                         set_pass_name(passname, "Combined", a, chan_id);
877                                         BLI_strncpy(layname, "Composite", sizeof(layname));
878                                 }
879                                 else {
880                                         passname[0] = chan_id[a];
881                                         passname[1] = '\0';
882                                         layname[0] = '\0';
883                                 }
884
885                                 IMB_exr_add_channel(exrhandle, layname, passname, viewname,
886                                                     4, 4 * rr->rectx, rview->rectf + a, half_float);
887                         }
888
889                         if (write_z && rview->rectz) {
890                                 const char *layname = (multi_layer)? "Composite": "";
891                                 IMB_exr_add_channel(exrhandle, layname, "Z", viewname,
892                                                     1, rr->rectx, rview->rectz, false);
893                         }
894                 }
895         }
896
897         /* Other render layers. */
898         int nr = (rr->have_combined)? 1: 0;
899         for (RenderLayer *rl = rr->layers.first; rl; rl = rl->next, nr++) {
900                 /* Skip other render layers if requested. */
901                 if (!multi_layer && nr != layer) {
902                         continue;
903                 }
904
905                 for (RenderPass *rp = rl->passes.first; rp; rp = rp->next) {
906                         /* Skip non-RGBA and Z passes if not using multi layer. */
907                         if (!multi_layer && !(STREQ(rp->name, RE_PASSNAME_COMBINED) ||
908                                               STREQ(rp->name, "") ||
909                                               (STREQ(rp->name, RE_PASSNAME_Z) && write_z))) {
910                                 continue;
911                         }
912
913                         /* Skip pass if it does not match the requested view(s). */
914                         const char *viewname = rp->view;
915                         if (view) {
916                                 if (!STREQ(view, viewname)) {
917                                         continue;
918                                 }
919                                 else {
920                                         viewname = "";
921                                 }
922                         }
923
924                         /* We only store RGBA passes as half float, for
925                          * others precision loss can be problematic. */
926                         bool pass_half_float = half_float &&
927                                                (STREQ(rp->chan_id, "RGB") ||
928                                                 STREQ(rp->chan_id, "RGBA") ||
929                                                 STREQ(rp->chan_id, "R") ||
930                                                 STREQ(rp->chan_id, "G") ||
931                                                 STREQ(rp->chan_id, "B") ||
932                                                 STREQ(rp->chan_id, "A"));
933
934
935                         for (int a = 0; a < rp->channels; a++) {
936                                 /* Save Combined as RGBA if single layer save. */
937                                 char passname[EXR_PASS_MAXNAME];
938                                 char layname[EXR_PASS_MAXNAME];
939
940                                 if (multi_layer) {
941                                         set_pass_name(passname, rp->name, a, rp->chan_id);
942                                         BLI_strncpy(layname, rl->name, sizeof(layname));
943                                 }
944                                 else {
945                                         passname[0] = rp->chan_id[a];
946                                         passname[1] = '\0';
947                                         layname[0] = '\0';
948                                 }
949
950                                 IMB_exr_add_channel(exrhandle, layname, passname, viewname,
951                                                     rp->channels, rp->channels * rr->rectx, rp->rect + a,
952                                                     pass_half_float);
953                         }
954                 }
955         }
956
957         errno = 0;
958
959         BLI_make_existing_file(filename);
960
961         int compress = (imf ? imf->exr_codec : 0);
962         bool success = IMB_exr_begin_write(exrhandle, filename, rr->rectx, rr->recty, compress, rr->stamp_data);
963         if (success) {
964                 IMB_exr_write_channels(exrhandle);
965         }
966         else {
967                 /* TODO, get the error from openexr's exception */
968                 BKE_reportf(reports, RPT_ERROR, "Error writing render result, %s (see console)", strerror(errno));
969         }
970
971         IMB_exr_close(exrhandle);
972         return success;
973 }
974
975 /**************************** Single Layer Rendering *************************/
976
977 void render_result_single_layer_begin(Render *re)
978 {
979         /* all layers except the active one get temporally pushed away */
980
981         /* officially pushed result should be NULL... error can happen with do_seq */
982         RE_FreeRenderResult(re->pushedresult);
983         
984         re->pushedresult = re->result;
985         re->result = NULL;
986 }
987
988 /* if scemode is R_SINGLE_LAYER, at end of rendering, merge the both render results */
989 void render_result_single_layer_end(Render *re)
990 {
991         ViewLayer *view_layer;
992         RenderLayer *rlpush;
993         RenderLayer *rl;
994         int nr;
995
996         if (re->result == NULL) {
997                 printf("pop render result error; no current result!\n");
998                 return;
999         }
1000
1001         if (!re->pushedresult)
1002                 return;
1003
1004         if (re->pushedresult->rectx == re->result->rectx && re->pushedresult->recty == re->result->recty) {
1005                 /* find which layer in re->pushedresult should be replaced */
1006                 rl = re->result->layers.first;
1007                 
1008                 /* render result should be empty after this */
1009                 BLI_remlink(&re->result->layers, rl);
1010                 
1011                 /* reconstruct render result layers */
1012                 for (nr = 0, view_layer = re->view_layers.first; view_layer; view_layer = view_layer->next, nr++) {
1013                         if (nr == re->active_view_layer) {
1014                                 BLI_addtail(&re->result->layers, rl);
1015                         }
1016                         else {
1017                                 rlpush = RE_GetRenderLayer(re->pushedresult, view_layer->name);
1018                                 if (rlpush) {
1019                                         BLI_remlink(&re->pushedresult->layers, rlpush);
1020                                         BLI_addtail(&re->result->layers, rlpush);
1021                                 }
1022                         }
1023                 }
1024         }
1025
1026         RE_FreeRenderResult(re->pushedresult);
1027         re->pushedresult = NULL;
1028 }
1029
1030 /************************* EXR Tile File Rendering ***************************/
1031
1032 static void save_render_result_tile(RenderResult *rr, RenderResult *rrpart, const char *viewname)
1033 {
1034         RenderLayer *rlp, *rl;
1035         RenderPass *rpassp;
1036         int offs, partx, party;
1037         
1038         BLI_lock_thread(LOCK_IMAGE);
1039         
1040         for (rlp = rrpart->layers.first; rlp; rlp = rlp->next) {
1041                 rl = RE_GetRenderLayer(rr, rlp->name);
1042
1043                 /* should never happen but prevents crash if it does */
1044                 BLI_assert(rl);
1045                 if (UNLIKELY(rl == NULL)) {
1046                         continue;
1047                 }
1048
1049                 if (rrpart->crop) { /* filters add pixel extra */
1050                         offs = (rrpart->crop + rrpart->crop * rrpart->rectx);
1051                 }
1052                 else {
1053                         offs = 0;
1054                 }
1055
1056                 /* passes are allocated in sync */
1057                 for (rpassp = rlp->passes.first; rpassp; rpassp = rpassp->next) {
1058                         const int xstride = rpassp->channels;
1059                         int a;
1060                         char fullname[EXR_PASS_MAXNAME];
1061
1062                         for (a = 0; a < xstride; a++) {
1063                                 set_pass_full_name(fullname, rpassp->name, a, viewname, rpassp->chan_id);
1064
1065                                 IMB_exr_set_channel(rl->exrhandle, rlp->name, fullname,
1066                                                     xstride, xstride * rrpart->rectx, rpassp->rect + a + xstride * offs);
1067                         }
1068                 }
1069                 
1070         }
1071
1072         party = rrpart->tilerect.ymin + rrpart->crop;
1073         partx = rrpart->tilerect.xmin + rrpart->crop;
1074
1075         for (rlp = rrpart->layers.first; rlp; rlp = rlp->next) {
1076                 rl = RE_GetRenderLayer(rr, rlp->name);
1077
1078                 /* should never happen but prevents crash if it does */
1079                 BLI_assert(rl);
1080                 if (UNLIKELY(rl == NULL)) {
1081                         continue;
1082                 }
1083
1084                 IMB_exrtile_write_channels(rl->exrhandle, partx, party, 0, viewname);
1085         }
1086
1087         BLI_unlock_thread(LOCK_IMAGE);
1088 }
1089
1090 void render_result_save_empty_result_tiles(Render *re)
1091 {
1092         RenderPart *pa;
1093         RenderResult *rr;
1094         RenderLayer *rl;
1095         
1096         for (rr = re->result; rr; rr = rr->next) {
1097                 for (rl = rr->layers.first; rl; rl = rl->next) {
1098                         IMB_exr_clear_channels(rl->exrhandle);
1099                 
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);
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                 if (!rv->rectf)
1358                         rv->rectf = MEM_mallocN(4 * sizeof(float) * rr->rectx * rr->recty, "render_seq rectf");
1359                 
1360                 memcpy(rv->rectf, ibuf->rect_float, 4 * sizeof(float) * rr->rectx * rr->recty);
1361
1362                 /* TSK! Since sequence render doesn't free the *rr render result, the old rect32
1363                  * can hang around when sequence render has rendered a 32 bits one before */
1364                 MEM_SAFE_FREE(rv->rect32);
1365         }
1366         else if (ibuf->rect) {
1367                 if (!rv->rect32)
1368                         rv->rect32 = MEM_mallocN(sizeof(int) * rr->rectx * rr->recty, "render_seq rect");
1369
1370                 memcpy(rv->rect32, ibuf->rect, 4 * rr->rectx * rr->recty);
1371
1372                 /* Same things as above, old rectf can hang around from previous render. */
1373                 MEM_SAFE_FREE(rv->rectf);
1374         }
1375 }
1376
1377 void render_result_rect_fill_zero(RenderResult *rr, const int view_id)
1378 {
1379         RenderView *rv = RE_RenderViewGetById(rr, view_id);
1380
1381         if (rv->rectf)
1382                 memset(rv->rectf, 0, 4 * sizeof(float) * rr->rectx * rr->recty);
1383         else if (rv->rect32)
1384                 memset(rv->rect32, 0, 4 * rr->rectx * rr->recty);
1385         else
1386                 rv->rect32 = MEM_callocN(sizeof(int) * rr->rectx * rr->recty, "render_seq rect");
1387 }
1388
1389 void render_result_rect_get_pixels(RenderResult *rr, unsigned int *rect, int rectx, int recty,
1390                                    const ColorManagedViewSettings *view_settings, const ColorManagedDisplaySettings *display_settings,
1391                                    const int view_id)
1392 {
1393         RenderView *rv = RE_RenderViewGetById(rr, view_id);
1394
1395         if (rv->rect32)
1396                 memcpy(rect, rv->rect32, sizeof(int) * rr->rectx * rr->recty);
1397         else if (rv->rectf)
1398                 IMB_display_buffer_transform_apply((unsigned char *) rect, rv->rectf, rr->rectx, rr->recty, 4,
1399                                                    view_settings, display_settings, true);
1400         else
1401                 /* else fill with black */
1402                 memset(rect, 0, sizeof(int) * rectx * recty);
1403 }
1404
1405
1406 /*************************** multiview functions *****************************/
1407
1408 bool RE_HasCombinedLayer(RenderResult *res)
1409 {
1410         RenderView *rv;
1411
1412         if (res == NULL)
1413                 return false;
1414
1415         rv = res->views.first;
1416         if (rv == NULL)
1417                 return false;
1418
1419         return (rv->rect32 || rv->rectf);
1420 }
1421
1422 bool RE_RenderResult_is_stereo(RenderResult *res)
1423 {
1424         if (! BLI_findstring(&res->views, STEREO_LEFT_NAME, offsetof(RenderView, name)))
1425                 return false;
1426
1427         if (! BLI_findstring(&res->views, STEREO_RIGHT_NAME, offsetof(RenderView, name)))
1428                 return false;
1429
1430         return true;
1431 }
1432
1433 RenderView *RE_RenderViewGetById(RenderResult *res, const int view_id)
1434 {
1435         RenderView *rv = BLI_findlink(&res->views, view_id);
1436         BLI_assert(res->views.first);
1437         return rv ? rv : res->views.first;
1438 }
1439
1440 RenderView *RE_RenderViewGetByName(RenderResult *res, const char *viewname)
1441 {
1442         RenderView *rv = BLI_findstring(&res->views, viewname, offsetof(RenderView, name));
1443         BLI_assert(res->views.first);
1444         return rv ? rv : res->views.first;
1445 }
1446
1447 static RenderPass *duplicate_render_pass(RenderPass *rpass)
1448 {
1449         RenderPass *new_rpass = MEM_mallocN(sizeof(RenderPass), "new render pass");
1450         *new_rpass = *rpass;
1451         new_rpass->next = new_rpass->prev = NULL;
1452         if (new_rpass->rect != NULL) {
1453                 new_rpass->rect = MEM_dupallocN(new_rpass->rect);
1454         }
1455         return new_rpass;
1456 }
1457
1458 static RenderLayer *duplicate_render_layer(RenderLayer *rl)
1459 {
1460         RenderLayer *new_rl = MEM_mallocN(sizeof(RenderLayer), "new render layer");
1461         *new_rl = *rl;
1462         new_rl->next = new_rl->prev = NULL;
1463         new_rl->passes.first = new_rl->passes.last = NULL;
1464         new_rl->exrhandle = NULL;
1465         if (new_rl->acolrect != NULL) {
1466                 new_rl->acolrect = MEM_dupallocN(new_rl->acolrect);
1467         }
1468         if (new_rl->scolrect != NULL) {
1469                 new_rl->scolrect = MEM_dupallocN(new_rl->scolrect);
1470         }
1471         if (new_rl->display_buffer != NULL) {
1472                 new_rl->display_buffer = MEM_dupallocN(new_rl->display_buffer);
1473         }
1474         for (RenderPass *rpass = rl->passes.first; rpass != NULL; rpass = rpass->next) {
1475                 RenderPass  *new_rpass = duplicate_render_pass(rpass);
1476                 BLI_addtail(&new_rl->passes, new_rpass);
1477         }
1478         return new_rl;
1479 }
1480
1481 static RenderView *duplicate_render_view(RenderView *rview)
1482 {
1483         RenderView *new_rview = MEM_mallocN(sizeof(RenderView), "new render view");
1484         *new_rview = *rview;
1485         if (new_rview->rectf != NULL) {
1486                 new_rview->rectf = MEM_dupallocN(new_rview->rectf);
1487         }
1488         if (new_rview->rectf != NULL) {
1489                 new_rview->rectf = MEM_dupallocN(new_rview->rectf);
1490         }
1491         if (new_rview->rectz != NULL) {
1492                 new_rview->rectz = MEM_dupallocN(new_rview->rectz);
1493         }
1494         if (new_rview->rect32 != NULL) {
1495                 new_rview->rect32 = MEM_dupallocN(new_rview->rect32);
1496         }
1497         return new_rview;
1498 }
1499
1500 RenderResult *RE_DuplicateRenderResult(RenderResult *rr)
1501 {
1502         RenderResult *new_rr = MEM_mallocN(sizeof(RenderResult), "new duplicated render result");
1503         *new_rr = *rr;
1504         new_rr->next = new_rr->prev = NULL;
1505         new_rr->layers.first = new_rr->layers.last = NULL;
1506         new_rr->views.first = new_rr->views.last = NULL;
1507         for (RenderLayer *rl = rr->layers.first; rl != NULL; rl = rl->next) {
1508                 RenderLayer *new_rl = duplicate_render_layer(rl);
1509                 BLI_addtail(&new_rr->layers, new_rl);
1510         }
1511         for (RenderView *rview = rr->views.first; rview != NULL; rview = rview->next) {
1512                 RenderView *new_rview = duplicate_render_view(rview);
1513                 BLI_addtail(&new_rr->views, new_rview);
1514         }
1515         if (new_rr->rect32 != NULL) {
1516                 new_rr->rect32 = MEM_dupallocN(new_rr->rect32);
1517         }
1518         if (new_rr->rectf != NULL) {
1519                 new_rr->rectf = MEM_dupallocN(new_rr->rectf);
1520         }
1521         if (new_rr->rectz != NULL) {
1522                 new_rr->rectz = MEM_dupallocN(new_rr->rectz);
1523         }
1524         new_rr->stamp_data = MEM_dupallocN(new_rr->stamp_data);
1525         return new_rr;
1526 }