Rename any instance of scene layer or render layer in code with view layer
[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                         for (int a = 0; a < rp->channels; a++) {
925                                 /* Save Combined as RGBA if single layer save. */
926                                 char passname[EXR_PASS_MAXNAME];
927                                 char layname[EXR_PASS_MAXNAME];
928
929                                 if (multi_layer) {
930                                         set_pass_name(passname, rp->name, a, rp->chan_id);
931                                         BLI_strncpy(layname, rl->name, sizeof(layname));
932                                 }
933                                 else {
934                                         passname[0] = rp->chan_id[a];
935                                         passname[1] = '\0';
936                                         layname[0] = '\0';
937                                 }
938
939                                 /* Add channel. */
940                                 IMB_exr_add_channel(exrhandle, layname, passname, viewname,
941                                                     rp->channels, rp->channels * rr->rectx, rp->rect + a,
942                                                     STREQ(rp->name, RE_PASSNAME_Z) ? false : half_float);
943                         }
944                 }
945         }
946
947         errno = 0;
948
949         BLI_make_existing_file(filename);
950
951         int compress = (imf ? imf->exr_codec : 0);
952         bool success = IMB_exr_begin_write(exrhandle, filename, rr->rectx, rr->recty, compress, rr->stamp_data);
953         if (success) {
954                 IMB_exr_write_channels(exrhandle);
955         }
956         else {
957                 /* TODO, get the error from openexr's exception */
958                 BKE_reportf(reports, RPT_ERROR, "Error writing render result, %s (see console)", strerror(errno));
959         }
960
961         IMB_exr_close(exrhandle);
962         return success;
963 }
964
965 /**************************** Single Layer Rendering *************************/
966
967 void render_result_single_layer_begin(Render *re)
968 {
969         /* all layers except the active one get temporally pushed away */
970
971         /* officially pushed result should be NULL... error can happen with do_seq */
972         RE_FreeRenderResult(re->pushedresult);
973         
974         re->pushedresult = re->result;
975         re->result = NULL;
976 }
977
978 /* if scemode is R_SINGLE_LAYER, at end of rendering, merge the both render results */
979 void render_result_single_layer_end(Render *re)
980 {
981         ViewLayer *view_layer;
982         RenderLayer *rlpush;
983         RenderLayer *rl;
984         int nr;
985
986         if (re->result == NULL) {
987                 printf("pop render result error; no current result!\n");
988                 return;
989         }
990
991         if (!re->pushedresult)
992                 return;
993
994         if (re->pushedresult->rectx == re->result->rectx && re->pushedresult->recty == re->result->recty) {
995                 /* find which layer in re->pushedresult should be replaced */
996                 rl = re->result->layers.first;
997                 
998                 /* render result should be empty after this */
999                 BLI_remlink(&re->result->layers, rl);
1000                 
1001                 /* reconstruct render result layers */
1002                 for (nr = 0, view_layer = re->view_layers.first; view_layer; view_layer = view_layer->next, nr++) {
1003                         if (nr == re->active_view_layer) {
1004                                 BLI_addtail(&re->result->layers, rl);
1005                         }
1006                         else {
1007                                 rlpush = RE_GetRenderLayer(re->pushedresult, view_layer->name);
1008                                 if (rlpush) {
1009                                         BLI_remlink(&re->pushedresult->layers, rlpush);
1010                                         BLI_addtail(&re->result->layers, rlpush);
1011                                 }
1012                         }
1013                 }
1014         }
1015
1016         RE_FreeRenderResult(re->pushedresult);
1017         re->pushedresult = NULL;
1018 }
1019
1020 /************************* EXR Tile File Rendering ***************************/
1021
1022 static void save_render_result_tile(RenderResult *rr, RenderResult *rrpart, const char *viewname)
1023 {
1024         RenderLayer *rlp, *rl;
1025         RenderPass *rpassp;
1026         int offs, partx, party;
1027         
1028         BLI_lock_thread(LOCK_IMAGE);
1029         
1030         for (rlp = rrpart->layers.first; rlp; rlp = rlp->next) {
1031                 rl = RE_GetRenderLayer(rr, rlp->name);
1032
1033                 /* should never happen but prevents crash if it does */
1034                 BLI_assert(rl);
1035                 if (UNLIKELY(rl == NULL)) {
1036                         continue;
1037                 }
1038
1039                 if (rrpart->crop) { /* filters add pixel extra */
1040                         offs = (rrpart->crop + rrpart->crop * rrpart->rectx);
1041                 }
1042                 else {
1043                         offs = 0;
1044                 }
1045
1046                 /* passes are allocated in sync */
1047                 for (rpassp = rlp->passes.first; rpassp; rpassp = rpassp->next) {
1048                         const int xstride = rpassp->channels;
1049                         int a;
1050                         char fullname[EXR_PASS_MAXNAME];
1051
1052                         for (a = 0; a < xstride; a++) {
1053                                 set_pass_full_name(fullname, rpassp->name, a, viewname, rpassp->chan_id);
1054
1055                                 IMB_exr_set_channel(rl->exrhandle, rlp->name, fullname,
1056                                                     xstride, xstride * rrpart->rectx, rpassp->rect + a + xstride * offs);
1057                         }
1058                 }
1059                 
1060         }
1061
1062         party = rrpart->tilerect.ymin + rrpart->crop;
1063         partx = rrpart->tilerect.xmin + rrpart->crop;
1064
1065         for (rlp = rrpart->layers.first; rlp; rlp = rlp->next) {
1066                 rl = RE_GetRenderLayer(rr, rlp->name);
1067
1068                 /* should never happen but prevents crash if it does */
1069                 BLI_assert(rl);
1070                 if (UNLIKELY(rl == NULL)) {
1071                         continue;
1072                 }
1073
1074                 IMB_exrtile_write_channels(rl->exrhandle, partx, party, 0, viewname);
1075         }
1076
1077         BLI_unlock_thread(LOCK_IMAGE);
1078 }
1079
1080 void render_result_save_empty_result_tiles(Render *re)
1081 {
1082         RenderPart *pa;
1083         RenderResult *rr;
1084         RenderLayer *rl;
1085         
1086         for (rr = re->result; rr; rr = rr->next) {
1087                 for (rl = rr->layers.first; rl; rl = rl->next) {
1088                         IMB_exr_clear_channels(rl->exrhandle);
1089                 
1090                         for (pa = re->parts.first; pa; pa = pa->next) {
1091                                 if (pa->status != PART_STATUS_MERGED) {
1092                                         int party = pa->disprect.ymin - re->disprect.ymin + pa->crop;
1093                                         int partx = pa->disprect.xmin - re->disprect.xmin + pa->crop;
1094                                         IMB_exrtile_write_channels(rl->exrhandle, partx, party, 0, re->viewname);
1095                                 }
1096                         }
1097                 }
1098         }
1099 }
1100
1101 /* begin write of exr tile file */
1102 void render_result_exr_file_begin(Render *re)
1103 {
1104         RenderResult *rr;
1105         RenderLayer *rl;
1106         char str[FILE_MAX];
1107
1108         for (rr = re->result; rr; rr = rr->next) {
1109                 for (rl = rr->layers.first; rl; rl = rl->next) {
1110                         render_result_exr_file_path(re->scene, rl->name, rr->sample_nr, str);
1111                         printf("write exr tmp file, %dx%d, %s\n", rr->rectx, rr->recty, str);
1112                         IMB_exrtile_begin_write(rl->exrhandle, str, 0, rr->rectx, rr->recty, re->partx, re->party);
1113                 }
1114         }
1115 }
1116
1117 /* end write of exr tile file, read back first sample */
1118 void render_result_exr_file_end(Render *re)
1119 {
1120         RenderResult *rr;
1121         RenderLayer *rl;
1122
1123         for (rr = re->result; rr; rr = rr->next) {
1124                 for (rl = rr->layers.first; rl; rl = rl->next) {
1125                         IMB_exr_close(rl->exrhandle);
1126                         rl->exrhandle = NULL;
1127                 }
1128
1129                 rr->do_exr_tile = false;
1130         }
1131         
1132         render_result_free_list(&re->fullresult, re->result);
1133         re->result = NULL;
1134
1135         render_result_exr_file_read_sample(re, 0);
1136 }
1137
1138 /* save part into exr file */
1139 void render_result_exr_file_merge(RenderResult *rr, RenderResult *rrpart, const char *viewname)
1140 {
1141         for (; rr && rrpart; rr = rr->next, rrpart = rrpart->next)
1142                 save_render_result_tile(rr, rrpart, viewname);
1143 }
1144
1145 /* path to temporary exr file */
1146 void render_result_exr_file_path(Scene *scene, const char *layname, int sample, char *filepath)
1147 {
1148         char name[FILE_MAXFILE + MAX_ID_NAME + MAX_ID_NAME + 100];
1149         const char *fi = BLI_path_basename(G.main->name);
1150         
1151         if (sample == 0) {
1152                 BLI_snprintf(name, sizeof(name), "%s_%s_%s.exr", fi, scene->id.name + 2, layname);
1153         }
1154         else {
1155                 BLI_snprintf(name, sizeof(name), "%s_%s_%s%d.exr", fi, scene->id.name + 2, layname, sample);
1156         }
1157
1158         /* Make name safe for paths, see T43275. */
1159         BLI_filename_make_safe(name);
1160
1161         BLI_make_file_string("/", filepath, BKE_tempdir_session(), name);
1162 }
1163
1164 /* only for temp buffer, makes exact copy of render result */
1165 int render_result_exr_file_read_sample(Render *re, int sample)
1166 {
1167         RenderLayer *rl;
1168         char str[FILE_MAXFILE + MAX_ID_NAME + MAX_ID_NAME + 100] = "";
1169         bool success = true;
1170
1171         RE_FreeRenderResult(re->result);
1172         re->result = render_result_new(re, &re->disprect, 0, RR_USE_MEM, RR_ALL_LAYERS, RR_ALL_VIEWS);
1173
1174         for (rl = re->result->layers.first; rl; rl = rl->next) {
1175                 render_result_exr_file_path(re->scene, rl->name, sample, str);
1176                 printf("read exr tmp file: %s\n", str);
1177
1178                 if (!render_result_exr_file_read_path(re->result, rl, str)) {
1179                         printf("cannot read: %s\n", str);
1180                         success = false;
1181                 }
1182         }
1183
1184         return success;
1185 }
1186
1187 /* called for reading temp files, and for external engines */
1188 int render_result_exr_file_read_path(RenderResult *rr, RenderLayer *rl_single, const char *filepath)
1189 {
1190         RenderLayer *rl;
1191         RenderPass *rpass;
1192         void *exrhandle = IMB_exr_get_handle();
1193         int rectx, recty;
1194
1195         if (IMB_exr_begin_read(exrhandle, filepath, &rectx, &recty) == 0) {
1196                 printf("failed being read %s\n", filepath);
1197                 IMB_exr_close(exrhandle);
1198                 return 0;
1199         }
1200
1201         if (rr == NULL || rectx != rr->rectx || recty != rr->recty) {
1202                 if (rr)
1203                         printf("error in reading render result: dimensions don't match\n");
1204                 else
1205                         printf("error in reading render result: NULL result pointer\n");
1206                 IMB_exr_close(exrhandle);
1207                 return 0;
1208         }
1209
1210         for (rl = rr->layers.first; rl; rl = rl->next) {
1211                 if (rl_single && rl_single != rl)
1212                         continue;
1213                 
1214                 /* passes are allocated in sync */
1215                 for (rpass = rl->passes.first; rpass; rpass = rpass->next) {
1216                         const int xstride = rpass->channels;
1217                         int a;
1218                         char fullname[EXR_PASS_MAXNAME];
1219
1220                         for (a = 0; a < xstride; a++) {
1221                                 set_pass_full_name(fullname, rpass->name, a, rpass->view, rpass->chan_id);
1222                                 IMB_exr_set_channel(exrhandle, rl->name, fullname,
1223                                                     xstride, xstride * rectx, rpass->rect + a);
1224                         }
1225
1226                         set_pass_full_name(rpass->fullname, rpass->name, -1, rpass->view, rpass->chan_id);
1227                 }
1228         }
1229
1230         IMB_exr_read_channels(exrhandle);
1231         IMB_exr_close(exrhandle);
1232
1233         return 1;
1234 }
1235
1236 static void render_result_exr_file_cache_path(Scene *sce, const char *root, char *r_path)
1237 {
1238         char filename_full[FILE_MAX + MAX_ID_NAME + 100], filename[FILE_MAXFILE], dirname[FILE_MAXDIR];
1239         char path_digest[16] = {0};
1240         char path_hexdigest[33];
1241
1242         /* If root is relative, use either current .blend file dir, or temp one if not saved. */
1243         if (G.main->name[0]) {
1244                 BLI_split_dirfile(G.main->name, dirname, filename, sizeof(dirname), sizeof(filename));
1245                 BLI_replace_extension(filename, sizeof(filename), "");  /* strip '.blend' */
1246                 BLI_hash_md5_buffer(G.main->name, strlen(G.main->name), path_digest);
1247         }
1248         else {
1249                 BLI_strncpy(dirname, BKE_tempdir_base(), sizeof(dirname));
1250                 BLI_strncpy(filename, "UNSAVED", sizeof(filename));
1251         }
1252         BLI_hash_md5_to_hexdigest(path_digest, path_hexdigest);
1253
1254         /* Default to *non-volatile* tmp dir. */
1255         if (*root == '\0') {
1256                 root = BKE_tempdir_base();
1257         }
1258
1259         BLI_snprintf(filename_full, sizeof(filename_full), "cached_RR_%s_%s_%s.exr",
1260                      filename, sce->id.name + 2, path_hexdigest);
1261         BLI_make_file_string(dirname, r_path, root, filename_full);
1262 }
1263
1264 void render_result_exr_file_cache_write(Render *re)
1265 {
1266         RenderResult *rr = re->result;
1267         char str[FILE_MAXFILE + FILE_MAXFILE + MAX_ID_NAME + 100];
1268         char *root = U.render_cachedir;
1269
1270         render_result_exr_file_cache_path(re->scene, root, str);
1271         printf("Caching exr file, %dx%d, %s\n", rr->rectx, rr->recty, str);
1272
1273         RE_WriteRenderResult(NULL, rr, str, NULL, NULL, -1);
1274 }
1275
1276 /* For cache, makes exact copy of render result */
1277 bool render_result_exr_file_cache_read(Render *re)
1278 {
1279         char str[FILE_MAXFILE + MAX_ID_NAME + MAX_ID_NAME + 100] = "";
1280         char *root = U.render_cachedir;
1281
1282         RE_FreeRenderResult(re->result);
1283         re->result = render_result_new(re, &re->disprect, 0, RR_USE_MEM, RR_ALL_LAYERS, RR_ALL_VIEWS);
1284
1285         /* First try cache. */
1286         render_result_exr_file_cache_path(re->scene, root, str);
1287
1288         printf("read exr cache file: %s\n", str);
1289         if (!render_result_exr_file_read_path(re->result, NULL, str)) {
1290                 printf("cannot read: %s\n", str);
1291                 return false;
1292         }
1293         return true;
1294 }
1295
1296 /*************************** Combined Pixel Rect *****************************/
1297
1298 ImBuf *render_result_rect_to_ibuf(RenderResult *rr, RenderData *rd, const int view_id)
1299 {
1300         ImBuf *ibuf = IMB_allocImBuf(rr->rectx, rr->recty, rd->im_format.planes, 0);
1301         RenderView *rv = RE_RenderViewGetById(rr, view_id);
1302
1303         /* if not exists, BKE_imbuf_write makes one */
1304         ibuf->rect = (unsigned int *) rv->rect32;
1305         ibuf->rect_float = rv->rectf;
1306         ibuf->zbuf_float = rv->rectz;
1307
1308         /* float factor for random dither, imbuf takes care of it */
1309         ibuf->dither = rd->dither_intensity;
1310         
1311         /* prepare to gamma correct to sRGB color space
1312          * note that sequence editor can generate 8bpc render buffers
1313          */
1314         if (ibuf->rect) {
1315                 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)) {
1316                         if (rd->im_format.depth == R_IMF_CHAN_DEPTH_8) {
1317                                 /* Higher depth bits are supported but not needed for current file output. */
1318                                 ibuf->rect_float = NULL;
1319                         }
1320                         else {
1321                                 IMB_float_from_rect(ibuf);
1322                         }
1323                 }
1324                 else {
1325                         /* ensure no float buffer remained from previous frame */
1326                         ibuf->rect_float = NULL;
1327                 }
1328         }
1329
1330         /* color -> grayscale */
1331         /* editing directly would alter the render view */
1332         if (rd->im_format.planes == R_IMF_PLANES_BW) {
1333                 ImBuf *ibuf_bw = IMB_dupImBuf(ibuf);
1334                 IMB_color_to_bw(ibuf_bw);
1335                 IMB_freeImBuf(ibuf);
1336                 ibuf = ibuf_bw;
1337         }
1338
1339         return ibuf;
1340 }
1341
1342 void RE_render_result_rect_from_ibuf(RenderResult *rr, RenderData *UNUSED(rd), ImBuf *ibuf, const int view_id)
1343 {
1344         RenderView *rv = RE_RenderViewGetById(rr, view_id);
1345
1346         if (ibuf->rect_float) {
1347                 if (!rv->rectf)
1348                         rv->rectf = MEM_mallocN(4 * sizeof(float) * rr->rectx * rr->recty, "render_seq rectf");
1349                 
1350                 memcpy(rv->rectf, ibuf->rect_float, 4 * sizeof(float) * rr->rectx * rr->recty);
1351
1352                 /* TSK! Since sequence render doesn't free the *rr render result, the old rect32
1353                  * can hang around when sequence render has rendered a 32 bits one before */
1354                 MEM_SAFE_FREE(rv->rect32);
1355         }
1356         else if (ibuf->rect) {
1357                 if (!rv->rect32)
1358                         rv->rect32 = MEM_mallocN(sizeof(int) * rr->rectx * rr->recty, "render_seq rect");
1359
1360                 memcpy(rv->rect32, ibuf->rect, 4 * rr->rectx * rr->recty);
1361
1362                 /* Same things as above, old rectf can hang around from previous render. */
1363                 MEM_SAFE_FREE(rv->rectf);
1364         }
1365 }
1366
1367 void render_result_rect_fill_zero(RenderResult *rr, const int view_id)
1368 {
1369         RenderView *rv = RE_RenderViewGetById(rr, view_id);
1370
1371         if (rv->rectf)
1372                 memset(rv->rectf, 0, 4 * sizeof(float) * rr->rectx * rr->recty);
1373         else if (rv->rect32)
1374                 memset(rv->rect32, 0, 4 * rr->rectx * rr->recty);
1375         else
1376                 rv->rect32 = MEM_callocN(sizeof(int) * rr->rectx * rr->recty, "render_seq rect");
1377 }
1378
1379 void render_result_rect_get_pixels(RenderResult *rr, unsigned int *rect, int rectx, int recty,
1380                                    const ColorManagedViewSettings *view_settings, const ColorManagedDisplaySettings *display_settings,
1381                                    const int view_id)
1382 {
1383         RenderView *rv = RE_RenderViewGetById(rr, view_id);
1384
1385         if (rv->rect32)
1386                 memcpy(rect, rv->rect32, sizeof(int) * rr->rectx * rr->recty);
1387         else if (rv->rectf)
1388                 IMB_display_buffer_transform_apply((unsigned char *) rect, rv->rectf, rr->rectx, rr->recty, 4,
1389                                                    view_settings, display_settings, true);
1390         else
1391                 /* else fill with black */
1392                 memset(rect, 0, sizeof(int) * rectx * recty);
1393 }
1394
1395
1396 /*************************** multiview functions *****************************/
1397
1398 bool RE_HasCombinedLayer(RenderResult *res)
1399 {
1400         RenderView *rv;
1401
1402         if (res == NULL)
1403                 return false;
1404
1405         rv = res->views.first;
1406         if (rv == NULL)
1407                 return false;
1408
1409         return (rv->rect32 || rv->rectf);
1410 }
1411
1412 bool RE_RenderResult_is_stereo(RenderResult *res)
1413 {
1414         if (! BLI_findstring(&res->views, STEREO_LEFT_NAME, offsetof(RenderView, name)))
1415                 return false;
1416
1417         if (! BLI_findstring(&res->views, STEREO_RIGHT_NAME, offsetof(RenderView, name)))
1418                 return false;
1419
1420         return true;
1421 }
1422
1423 RenderView *RE_RenderViewGetById(RenderResult *res, const int view_id)
1424 {
1425         RenderView *rv = BLI_findlink(&res->views, view_id);
1426         BLI_assert(res->views.first);
1427         return rv ? rv : res->views.first;
1428 }
1429
1430 RenderView *RE_RenderViewGetByName(RenderResult *res, const char *viewname)
1431 {
1432         RenderView *rv = BLI_findstring(&res->views, viewname, offsetof(RenderView, name));
1433         BLI_assert(res->views.first);
1434         return rv ? rv : res->views.first;
1435 }
1436
1437 static RenderPass *duplicate_render_pass(RenderPass *rpass)
1438 {
1439         RenderPass *new_rpass = MEM_mallocN(sizeof(RenderPass), "new render pass");
1440         *new_rpass = *rpass;
1441         new_rpass->next = new_rpass->prev = NULL;
1442         if (new_rpass->rect != NULL) {
1443                 new_rpass->rect = MEM_dupallocN(new_rpass->rect);
1444         }
1445         return new_rpass;
1446 }
1447
1448 static RenderLayer *duplicate_render_layer(RenderLayer *rl)
1449 {
1450         RenderLayer *new_rl = MEM_mallocN(sizeof(RenderLayer), "new render layer");
1451         *new_rl = *rl;
1452         new_rl->next = new_rl->prev = NULL;
1453         new_rl->passes.first = new_rl->passes.last = NULL;
1454         new_rl->exrhandle = NULL;
1455         if (new_rl->acolrect != NULL) {
1456                 new_rl->acolrect = MEM_dupallocN(new_rl->acolrect);
1457         }
1458         if (new_rl->scolrect != NULL) {
1459                 new_rl->scolrect = MEM_dupallocN(new_rl->scolrect);
1460         }
1461         if (new_rl->display_buffer != NULL) {
1462                 new_rl->display_buffer = MEM_dupallocN(new_rl->display_buffer);
1463         }
1464         for (RenderPass *rpass = rl->passes.first; rpass != NULL; rpass = rpass->next) {
1465                 RenderPass  *new_rpass = duplicate_render_pass(rpass);
1466                 BLI_addtail(&new_rl->passes, new_rpass);
1467         }
1468         return new_rl;
1469 }
1470
1471 static RenderView *duplicate_render_view(RenderView *rview)
1472 {
1473         RenderView *new_rview = MEM_mallocN(sizeof(RenderView), "new render view");
1474         *new_rview = *rview;
1475         if (new_rview->rectf != NULL) {
1476                 new_rview->rectf = MEM_dupallocN(new_rview->rectf);
1477         }
1478         if (new_rview->rectf != NULL) {
1479                 new_rview->rectf = MEM_dupallocN(new_rview->rectf);
1480         }
1481         if (new_rview->rectz != NULL) {
1482                 new_rview->rectz = MEM_dupallocN(new_rview->rectz);
1483         }
1484         if (new_rview->rect32 != NULL) {
1485                 new_rview->rect32 = MEM_dupallocN(new_rview->rect32);
1486         }
1487         return new_rview;
1488 }
1489
1490 RenderResult *RE_DuplicateRenderResult(RenderResult *rr)
1491 {
1492         RenderResult *new_rr = MEM_mallocN(sizeof(RenderResult), "new duplicated render result");
1493         *new_rr = *rr;
1494         new_rr->next = new_rr->prev = NULL;
1495         new_rr->layers.first = new_rr->layers.last = NULL;
1496         new_rr->views.first = new_rr->views.last = NULL;
1497         for (RenderLayer *rl = rr->layers.first; rl != NULL; rl = rl->next) {
1498                 RenderLayer *new_rl = duplicate_render_layer(rl);
1499                 BLI_addtail(&new_rr->layers, new_rl);
1500         }
1501         for (RenderView *rview = rr->views.first; rview != NULL; rview = rview->next) {
1502                 RenderView *new_rview = duplicate_render_view(rview);
1503                 BLI_addtail(&new_rr->views, new_rview);
1504         }
1505         if (new_rr->rect32 != NULL) {
1506                 new_rr->rect32 = MEM_dupallocN(new_rr->rect32);
1507         }
1508         if (new_rr->rectf != NULL) {
1509                 new_rr->rectf = MEM_dupallocN(new_rr->rectf);
1510         }
1511         if (new_rr->rectz != NULL) {
1512                 new_rr->rectz = MEM_dupallocN(new_rr->rectz);
1513         }
1514         new_rr->stamp_data = MEM_dupallocN(new_rr->stamp_data);
1515         return new_rr;
1516 }