patch [#34103]
[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
36 #include "MEM_guardedalloc.h"
37
38 #include "BLI_utildefines.h"
39 #include "BLI_fileops.h"
40 #include "BLI_listbase.h"
41 #include "BLI_path_util.h"
42 #include "BLI_rect.h"
43 #include "BLI_string.h"
44 #include "BLI_threads.h"
45
46 #include "BKE_image.h"
47 #include "BKE_global.h"
48 #include "BKE_main.h"
49 #include "BKE_report.h"
50
51 #include "IMB_imbuf.h"
52 #include "IMB_imbuf_types.h"
53 #include "IMB_colormanagement.h"
54
55 #include "intern/openexr/openexr_multi.h"
56
57 #include "render_result.h"
58 #include "render_types.h"
59
60 /********************************** Free *************************************/
61
62 void render_result_free(RenderResult *res)
63 {
64         if (res == NULL) return;
65
66         while (res->layers.first) {
67                 RenderLayer *rl = res->layers.first;
68                 
69                 if (rl->rectf) MEM_freeN(rl->rectf);
70                 /* acolrect and scolrect are optionally allocated in shade_tile, only free here since it can be used for drawing */
71                 if (rl->acolrect) MEM_freeN(rl->acolrect);
72                 if (rl->scolrect) MEM_freeN(rl->scolrect);
73                 
74                 while (rl->passes.first) {
75                         RenderPass *rpass = rl->passes.first;
76                         if (rpass->rect) MEM_freeN(rpass->rect);
77                         BLI_remlink(&rl->passes, rpass);
78                         MEM_freeN(rpass);
79                 }
80                 BLI_remlink(&res->layers, rl);
81                 MEM_freeN(rl);
82         }
83         
84         if (res->rect32)
85                 MEM_freeN(res->rect32);
86         if (res->rectz)
87                 MEM_freeN(res->rectz);
88         if (res->rectf)
89                 MEM_freeN(res->rectf);
90         if (res->text)
91                 MEM_freeN(res->text);
92         
93         MEM_freeN(res);
94 }
95
96 /* version that's compatible with fullsample buffers */
97 void render_result_free_list(ListBase *lb, RenderResult *rr)
98 {
99         RenderResult *rrnext;
100         
101         for (; rr; rr = rrnext) {
102                 rrnext = rr->next;
103                 
104                 if (lb && lb->first)
105                         BLI_remlink(lb, rr);
106                 
107                 render_result_free(rr);
108         }
109 }
110
111 /********************************* Names *************************************/
112
113 /* NOTE: OpenEXR only supports 32 chars for layer+pass names
114  * In blender we now use max 10 chars for pass, max 20 for layer */
115 static const char *get_pass_name(int passtype, int channel)
116 {
117         
118         if (passtype == SCE_PASS_COMBINED) {
119                 if (channel == -1) return "Combined";
120                 if (channel == 0) return "Combined.R";
121                 if (channel == 1) return "Combined.G";
122                 if (channel == 2) return "Combined.B";
123                 return "Combined.A";
124         }
125         if (passtype == SCE_PASS_Z) {
126                 if (channel == -1) return "Depth";
127                 return "Depth.Z";
128         }
129         if (passtype == SCE_PASS_VECTOR) {
130                 if (channel == -1) return "Vector";
131                 if (channel == 0) return "Vector.X";
132                 if (channel == 1) return "Vector.Y";
133                 if (channel == 2) return "Vector.Z";
134                 return "Vector.W";
135         }
136         if (passtype == SCE_PASS_NORMAL) {
137                 if (channel == -1) return "Normal";
138                 if (channel == 0) return "Normal.X";
139                 if (channel == 1) return "Normal.Y";
140                 return "Normal.Z";
141         }
142         if (passtype == SCE_PASS_UV) {
143                 if (channel == -1) return "UV";
144                 if (channel == 0) return "UV.U";
145                 if (channel == 1) return "UV.V";
146                 return "UV.A";
147         }
148         if (passtype == SCE_PASS_RGBA) {
149                 if (channel == -1) return "Color";
150                 if (channel == 0) return "Color.R";
151                 if (channel == 1) return "Color.G";
152                 if (channel == 2) return "Color.B";
153                 return "Color.A";
154         }
155         if (passtype == SCE_PASS_EMIT) {
156                 if (channel == -1) return "Emit";
157                 if (channel == 0) return "Emit.R";
158                 if (channel == 1) return "Emit.G";
159                 return "Emit.B";
160         }
161         if (passtype == SCE_PASS_DIFFUSE) {
162                 if (channel == -1) return "Diffuse";
163                 if (channel == 0) return "Diffuse.R";
164                 if (channel == 1) return "Diffuse.G";
165                 return "Diffuse.B";
166         }
167         if (passtype == SCE_PASS_SPEC) {
168                 if (channel == -1) return "Spec";
169                 if (channel == 0) return "Spec.R";
170                 if (channel == 1) return "Spec.G";
171                 return "Spec.B";
172         }
173         if (passtype == SCE_PASS_SHADOW) {
174                 if (channel == -1) return "Shadow";
175                 if (channel == 0) return "Shadow.R";
176                 if (channel == 1) return "Shadow.G";
177                 return "Shadow.B";
178         }
179         if (passtype == SCE_PASS_AO) {
180                 if (channel == -1) return "AO";
181                 if (channel == 0) return "AO.R";
182                 if (channel == 1) return "AO.G";
183                 return "AO.B";
184         }
185         if (passtype == SCE_PASS_ENVIRONMENT) {
186                 if (channel == -1) return "Env";
187                 if (channel == 0) return "Env.R";
188                 if (channel == 1) return "Env.G";
189                 return "Env.B";
190         }
191         if (passtype == SCE_PASS_INDIRECT) {
192                 if (channel == -1) return "Indirect";
193                 if (channel == 0) return "Indirect.R";
194                 if (channel == 1) return "Indirect.G";
195                 return "Indirect.B";
196         }
197         if (passtype == SCE_PASS_REFLECT) {
198                 if (channel == -1) return "Reflect";
199                 if (channel == 0) return "Reflect.R";
200                 if (channel == 1) return "Reflect.G";
201                 return "Reflect.B";
202         }
203         if (passtype == SCE_PASS_REFRACT) {
204                 if (channel == -1) return "Refract";
205                 if (channel == 0) return "Refract.R";
206                 if (channel == 1) return "Refract.G";
207                 return "Refract.B";
208         }
209         if (passtype == SCE_PASS_INDEXOB) {
210                 if (channel == -1) return "IndexOB";
211                 return "IndexOB.X";
212         }
213         if (passtype == SCE_PASS_INDEXMA) {
214                 if (channel == -1) return "IndexMA";
215                 return "IndexMA.X";
216         }
217         if (passtype == SCE_PASS_MIST) {
218                 if (channel == -1) return "Mist";
219                 return "Mist.Z";
220         }
221         if (passtype == SCE_PASS_RAYHITS) {
222                 if (channel == -1) return "Rayhits";
223                 if (channel == 0) return "Rayhits.R";
224                 if (channel == 1) return "Rayhits.G";
225                 return "Rayhits.B";
226         }
227         if (passtype == SCE_PASS_DIFFUSE_DIRECT) {
228                 if (channel == -1) return "DiffDir";
229                 if (channel == 0) return "DiffDir.R";
230                 if (channel == 1) return "DiffDir.G";
231                 return "DiffDir.B";
232         }
233         if (passtype == SCE_PASS_DIFFUSE_INDIRECT) {
234                 if (channel == -1) return "DiffInd";
235                 if (channel == 0) return "DiffInd.R";
236                 if (channel == 1) return "DiffInd.G";
237                 return "DiffInd.B";
238         }
239         if (passtype == SCE_PASS_DIFFUSE_COLOR) {
240                 if (channel == -1) return "DiffCol";
241                 if (channel == 0) return "DiffCol.R";
242                 if (channel == 1) return "DiffCol.G";
243                 return "DiffCol.B";
244         }
245         if (passtype == SCE_PASS_GLOSSY_DIRECT) {
246                 if (channel == -1) return "GlossDir";
247                 if (channel == 0) return "GlossDir.R";
248                 if (channel == 1) return "GlossDir.G";
249                 return "GlossDir.B";
250         }
251         if (passtype == SCE_PASS_GLOSSY_INDIRECT) {
252                 if (channel == -1) return "GlossInd";
253                 if (channel == 0) return "GlossInd.R";
254                 if (channel == 1) return "GlossInd.G";
255                 return "GlossInd.B";
256         }
257         if (passtype == SCE_PASS_GLOSSY_COLOR) {
258                 if (channel == -1) return "GlossCol";
259                 if (channel == 0) return "GlossCol.R";
260                 if (channel == 1) return "GlossCol.G";
261                 return "GlossCol.B";
262         }
263         if (passtype == SCE_PASS_TRANSM_DIRECT) {
264                 if (channel == -1) return "TransDir";
265                 if (channel == 0) return "TransDir.R";
266                 if (channel == 1) return "TransDir.G";
267                 return "TransDir.B";
268         }
269         if (passtype == SCE_PASS_TRANSM_INDIRECT) {
270                 if (channel == -1) return "TransInd";
271                 if (channel == 0) return "TransInd.R";
272                 if (channel == 1) return "TransInd.G";
273                 return "TransInd.B";
274         }
275         if (passtype == SCE_PASS_TRANSM_COLOR) {
276                 if (channel == -1) return "TransCol";
277                 if (channel == 0) return "TransCol.R";
278                 if (channel == 1) return "TransCol.G";
279                 return "TransCol.B";
280         }
281         return "Unknown";
282 }
283
284 static int passtype_from_name(const char *str)
285 {
286         
287         if (strcmp(str, "Combined") == 0)
288                 return SCE_PASS_COMBINED;
289
290         if (strcmp(str, "Depth") == 0)
291                 return SCE_PASS_Z;
292
293         if (strcmp(str, "Vector") == 0)
294                 return SCE_PASS_VECTOR;
295
296         if (strcmp(str, "Normal") == 0)
297                 return SCE_PASS_NORMAL;
298
299         if (strcmp(str, "UV") == 0)
300                 return SCE_PASS_UV;
301
302         if (strcmp(str, "Color") == 0)
303                 return SCE_PASS_RGBA;
304
305         if (strcmp(str, "Emit") == 0)
306                 return SCE_PASS_EMIT;
307
308         if (strcmp(str, "Diffuse") == 0)
309                 return SCE_PASS_DIFFUSE;
310
311         if (strcmp(str, "Spec") == 0)
312                 return SCE_PASS_SPEC;
313
314         if (strcmp(str, "Shadow") == 0)
315                 return SCE_PASS_SHADOW;
316         
317         if (strcmp(str, "AO") == 0)
318                 return SCE_PASS_AO;
319
320         if (strcmp(str, "Env") == 0)
321                 return SCE_PASS_ENVIRONMENT;
322
323         if (strcmp(str, "Indirect") == 0)
324                 return SCE_PASS_INDIRECT;
325
326         if (strcmp(str, "Reflect") == 0)
327                 return SCE_PASS_REFLECT;
328
329         if (strcmp(str, "Refract") == 0)
330                 return SCE_PASS_REFRACT;
331
332         if (strcmp(str, "IndexOB") == 0)
333                 return SCE_PASS_INDEXOB;
334
335         if (strcmp(str, "IndexMA") == 0)
336                 return SCE_PASS_INDEXMA;
337
338         if (strcmp(str, "Mist") == 0)
339                 return SCE_PASS_MIST;
340         
341         if (strcmp(str, "RayHits") == 0)
342                 return SCE_PASS_RAYHITS;
343
344         if (strcmp(str, "DiffDir") == 0)
345                 return SCE_PASS_DIFFUSE_DIRECT;
346
347         if (strcmp(str, "DiffInd") == 0)
348                 return SCE_PASS_DIFFUSE_INDIRECT;
349
350         if (strcmp(str, "DiffCol") == 0)
351                 return SCE_PASS_DIFFUSE_COLOR;
352
353         if (strcmp(str, "GlossDir") == 0)
354                 return SCE_PASS_GLOSSY_DIRECT;
355
356         if (strcmp(str, "GlossInd") == 0)
357                 return SCE_PASS_GLOSSY_INDIRECT;
358
359         if (strcmp(str, "GlossCol") == 0)
360                 return SCE_PASS_GLOSSY_COLOR;
361
362         if (strcmp(str, "TransDir") == 0)
363                 return SCE_PASS_TRANSM_DIRECT;
364
365         if (strcmp(str, "TransInd") == 0)
366                 return SCE_PASS_TRANSM_INDIRECT;
367
368         if (strcmp(str, "TransCol") == 0)
369                 return SCE_PASS_TRANSM_COLOR;
370
371         return 0;
372 }
373
374 /********************************** New **************************************/
375
376 static void render_layer_add_pass(RenderResult *rr, RenderLayer *rl, int channels, int passtype)
377 {
378         const char *typestr = get_pass_name(passtype, 0);
379         RenderPass *rpass = MEM_callocN(sizeof(RenderPass), typestr);
380         int rectsize = rr->rectx * rr->recty * channels;
381         
382         BLI_addtail(&rl->passes, rpass);
383         rpass->passtype = passtype;
384         rpass->channels = channels;
385         rpass->rectx = rl->rectx;
386         rpass->recty = rl->recty;
387         BLI_strncpy(rpass->name, get_pass_name(rpass->passtype, -1), sizeof(rpass->name));
388         
389         if (rl->exrhandle) {
390                 int a;
391                 for (a = 0; a < channels; a++)
392                         IMB_exr_add_channel(rl->exrhandle, rl->name, get_pass_name(passtype, a), 0, 0, NULL);
393         }
394         else {
395                 float *rect;
396                 int x;
397                 
398                 rpass->rect = MEM_mapallocN(sizeof(float) * rectsize, typestr);
399                 
400                 if (passtype == SCE_PASS_VECTOR) {
401                         /* initialize to max speed */
402                         rect = rpass->rect;
403                         for (x = rectsize - 1; x >= 0; x--)
404                                 rect[x] = PASS_VECTOR_MAX;
405                 }
406                 else if (passtype == SCE_PASS_Z) {
407                         rect = rpass->rect;
408                         for (x = rectsize - 1; x >= 0; x--)
409                                 rect[x] = 10e10;
410                 }
411         }
412 }
413
414 /* called by main render as well for parts */
415 /* will read info from Render *re to define layers */
416 /* called in threads */
417 /* re->winx,winy is coordinate space of entire image, partrct the part within */
418 RenderResult *render_result_new(Render *re, rcti *partrct, int crop, int savebuffers, const char *layername)
419 {
420         RenderResult *rr;
421         RenderLayer *rl;
422         SceneRenderLayer *srl;
423         int rectx, recty, nr;
424         
425         rectx = BLI_rcti_size_x(partrct);
426         recty = BLI_rcti_size_y(partrct);
427         
428         if (rectx <= 0 || recty <= 0)
429                 return NULL;
430         
431         rr = MEM_callocN(sizeof(RenderResult), "new render result");
432         rr->rectx = rectx;
433         rr->recty = recty;
434         rr->renrect.xmin = 0; rr->renrect.xmax = rectx - 2 * crop;
435         /* crop is one or two extra pixels rendered for filtering, is used for merging and display too */
436         rr->crop = crop;
437
438         /* tilerect is relative coordinates within render disprect. do not subtract crop yet */
439         rr->tilerect.xmin = partrct->xmin - re->disprect.xmin;
440         rr->tilerect.xmax = partrct->xmax - re->disprect.xmin;
441         rr->tilerect.ymin = partrct->ymin - re->disprect.ymin;
442         rr->tilerect.ymax = partrct->ymax - re->disprect.ymin;
443         
444         if (savebuffers) {
445                 rr->do_exr_tile = TRUE;
446         }
447
448         /* check renderdata for amount of layers */
449         for (nr = 0, srl = re->r.layers.first; srl; srl = srl->next, nr++) {
450
451                 if (layername && layername[0])
452                         if (strcmp(srl->name, layername) != 0)
453                                 continue;
454
455                 if ((re->r.scemode & R_SINGLE_LAYER) && nr != re->r.actlay)
456                         continue;
457                 if (srl->layflag & SCE_LAY_DISABLE)
458                         continue;
459                 
460                 rl = MEM_callocN(sizeof(RenderLayer), "new render layer");
461                 BLI_addtail(&rr->layers, rl);
462                 
463                 BLI_strncpy(rl->name, srl->name, sizeof(rl->name));
464                 rl->lay = srl->lay;
465                 rl->lay_zmask = srl->lay_zmask;
466                 rl->lay_exclude = srl->lay_exclude;
467                 rl->layflag = srl->layflag;
468                 rl->passflag = srl->passflag; /* for debugging: srl->passflag | SCE_PASS_RAYHITS; */
469                 rl->pass_xor = srl->pass_xor;
470                 rl->light_override = srl->light_override;
471                 rl->mat_override = srl->mat_override;
472                 rl->rectx = rectx;
473                 rl->recty = recty;
474                 
475                 if (rr->do_exr_tile) {
476                         rl->exrhandle = IMB_exr_get_handle();
477
478                         IMB_exr_add_channel(rl->exrhandle, rl->name, "Combined.R", 0, 0, NULL);
479                         IMB_exr_add_channel(rl->exrhandle, rl->name, "Combined.G", 0, 0, NULL);
480                         IMB_exr_add_channel(rl->exrhandle, rl->name, "Combined.B", 0, 0, NULL);
481                         IMB_exr_add_channel(rl->exrhandle, rl->name, "Combined.A", 0, 0, NULL);
482                 }
483                 else
484                         rl->rectf = MEM_mapallocN(rectx * recty * sizeof(float) * 4, "Combined rgba");
485                 
486                 if (srl->passflag  & SCE_PASS_Z)
487                         render_layer_add_pass(rr, rl, 1, SCE_PASS_Z);
488                 if (srl->passflag  & SCE_PASS_VECTOR)
489                         render_layer_add_pass(rr, rl, 4, SCE_PASS_VECTOR);
490                 if (srl->passflag  & SCE_PASS_NORMAL)
491                         render_layer_add_pass(rr, rl, 3, SCE_PASS_NORMAL);
492                 if (srl->passflag  & SCE_PASS_UV) 
493                         render_layer_add_pass(rr, rl, 3, SCE_PASS_UV);
494                 if (srl->passflag  & SCE_PASS_RGBA)
495                         render_layer_add_pass(rr, rl, 4, SCE_PASS_RGBA);
496                 if (srl->passflag  & SCE_PASS_EMIT)
497                         render_layer_add_pass(rr, rl, 3, SCE_PASS_EMIT);
498                 if (srl->passflag  & SCE_PASS_DIFFUSE)
499                         render_layer_add_pass(rr, rl, 3, SCE_PASS_DIFFUSE);
500                 if (srl->passflag  & SCE_PASS_SPEC)
501                         render_layer_add_pass(rr, rl, 3, SCE_PASS_SPEC);
502                 if (srl->passflag  & SCE_PASS_AO)
503                         render_layer_add_pass(rr, rl, 3, SCE_PASS_AO);
504                 if (srl->passflag  & SCE_PASS_ENVIRONMENT)
505                         render_layer_add_pass(rr, rl, 3, SCE_PASS_ENVIRONMENT);
506                 if (srl->passflag  & SCE_PASS_INDIRECT)
507                         render_layer_add_pass(rr, rl, 3, SCE_PASS_INDIRECT);
508                 if (srl->passflag  & SCE_PASS_SHADOW)
509                         render_layer_add_pass(rr, rl, 3, SCE_PASS_SHADOW);
510                 if (srl->passflag  & SCE_PASS_REFLECT)
511                         render_layer_add_pass(rr, rl, 3, SCE_PASS_REFLECT);
512                 if (srl->passflag  & SCE_PASS_REFRACT)
513                         render_layer_add_pass(rr, rl, 3, SCE_PASS_REFRACT);
514                 if (srl->passflag  & SCE_PASS_INDEXOB)
515                         render_layer_add_pass(rr, rl, 1, SCE_PASS_INDEXOB);
516                 if (srl->passflag  & SCE_PASS_INDEXMA)
517                         render_layer_add_pass(rr, rl, 1, SCE_PASS_INDEXMA);
518                 if (srl->passflag  & SCE_PASS_MIST)
519                         render_layer_add_pass(rr, rl, 1, SCE_PASS_MIST);
520                 if (rl->passflag & SCE_PASS_RAYHITS)
521                         render_layer_add_pass(rr, rl, 4, SCE_PASS_RAYHITS);
522                 if (srl->passflag  & SCE_PASS_DIFFUSE_DIRECT)
523                         render_layer_add_pass(rr, rl, 3, SCE_PASS_DIFFUSE_DIRECT);
524                 if (srl->passflag  & SCE_PASS_DIFFUSE_INDIRECT)
525                         render_layer_add_pass(rr, rl, 3, SCE_PASS_DIFFUSE_INDIRECT);
526                 if (srl->passflag  & SCE_PASS_DIFFUSE_COLOR)
527                         render_layer_add_pass(rr, rl, 3, SCE_PASS_DIFFUSE_COLOR);
528                 if (srl->passflag  & SCE_PASS_GLOSSY_DIRECT)
529                         render_layer_add_pass(rr, rl, 3, SCE_PASS_GLOSSY_DIRECT);
530                 if (srl->passflag  & SCE_PASS_GLOSSY_INDIRECT)
531                         render_layer_add_pass(rr, rl, 3, SCE_PASS_GLOSSY_INDIRECT);
532                 if (srl->passflag  & SCE_PASS_GLOSSY_COLOR)
533                         render_layer_add_pass(rr, rl, 3, SCE_PASS_GLOSSY_COLOR);
534                 if (srl->passflag  & SCE_PASS_TRANSM_DIRECT)
535                         render_layer_add_pass(rr, rl, 3, SCE_PASS_TRANSM_DIRECT);
536                 if (srl->passflag  & SCE_PASS_TRANSM_INDIRECT)
537                         render_layer_add_pass(rr, rl, 3, SCE_PASS_TRANSM_INDIRECT);
538                 if (srl->passflag  & SCE_PASS_TRANSM_COLOR)
539                         render_layer_add_pass(rr, rl, 3, SCE_PASS_TRANSM_COLOR);
540                 
541         }
542         /* sss, previewrender and envmap don't do layers, so we make a default one */
543         if (rr->layers.first == NULL && !(layername && layername[0])) {
544                 rl = MEM_callocN(sizeof(RenderLayer), "new render layer");
545                 BLI_addtail(&rr->layers, rl);
546                 
547                 rl->rectx = rectx;
548                 rl->recty = recty;
549
550                 /* duplicate code... */
551                 if (rr->do_exr_tile) {
552                         rl->exrhandle = IMB_exr_get_handle();
553
554                         IMB_exr_add_channel(rl->exrhandle, rl->name, "Combined.R", 0, 0, NULL);
555                         IMB_exr_add_channel(rl->exrhandle, rl->name, "Combined.G", 0, 0, NULL);
556                         IMB_exr_add_channel(rl->exrhandle, rl->name, "Combined.B", 0, 0, NULL);
557                         IMB_exr_add_channel(rl->exrhandle, rl->name, "Combined.A", 0, 0, NULL);
558                 }
559                 else {
560                         rl->rectf = MEM_mapallocN(rectx * recty * sizeof(float) * 4, "Combined rgba");
561                 }
562                 
563                 /* note, this has to be in sync with scene.c */
564                 rl->lay = (1 << 20) - 1;
565                 rl->layflag = 0x7FFF;    /* solid ztra halo strand */
566                 rl->passflag = SCE_PASS_COMBINED;
567                 
568                 re->r.actlay = 0;
569         }
570         
571         /* border render; calculate offset for use in compositor. compo is centralized coords */
572         rr->xof = re->disprect.xmin + BLI_rcti_cent_x(&re->disprect) - (re->winx / 2);
573         rr->yof = re->disprect.ymin + BLI_rcti_cent_y(&re->disprect) - (re->winy / 2);
574         
575         return rr;
576 }
577
578 /* allocate osa new results for samples */
579 RenderResult *render_result_new_full_sample(Render *re, ListBase *lb, rcti *partrct, int crop, int savebuffers)
580 {
581         int a;
582         
583         if (re->osa == 0)
584                 return render_result_new(re, partrct, crop, savebuffers, RR_ALL_LAYERS);
585         
586         for (a = 0; a < re->osa; a++) {
587                 RenderResult *rr = render_result_new(re, partrct, crop, savebuffers, RR_ALL_LAYERS);
588                 BLI_addtail(lb, rr);
589                 rr->sample_nr = a;
590         }
591         
592         return lb->first;
593 }
594
595 /* callbacks for render_result_new_from_exr */
596 static void *ml_addlayer_cb(void *base, const char *str)
597 {
598         RenderResult *rr = base;
599         RenderLayer *rl;
600         
601         rl = MEM_callocN(sizeof(RenderLayer), "new render layer");
602         BLI_addtail(&rr->layers, rl);
603         
604         BLI_strncpy(rl->name, str, EXR_LAY_MAXNAME);
605         return rl;
606 }
607
608 static void ml_addpass_cb(void *UNUSED(base), void *lay, const char *str, float *rect, int totchan, const char *chan_id)
609 {
610         RenderLayer *rl = lay;
611         RenderPass *rpass = MEM_callocN(sizeof(RenderPass), "loaded pass");
612         int a;
613         
614         BLI_addtail(&rl->passes, rpass);
615         rpass->channels = totchan;
616
617         rpass->passtype = passtype_from_name(str);
618         if (rpass->passtype == 0) printf("unknown pass %s\n", str);
619         rl->passflag |= rpass->passtype;
620         
621         BLI_strncpy(rpass->name, str, EXR_PASS_MAXNAME);
622         /* channel id chars */
623         for (a = 0; a < totchan; a++)
624                 rpass->chan_id[a] = chan_id[a];
625         
626         rpass->rect = rect;
627 }
628
629 /* from imbuf, if a handle was returned we convert this to render result */
630 RenderResult *render_result_new_from_exr(void *exrhandle, const char *colorspace, int predivide, int rectx, int recty)
631 {
632         RenderResult *rr = MEM_callocN(sizeof(RenderResult), __func__);
633         RenderLayer *rl;
634         RenderPass *rpass;
635         const char *to_colorspace = IMB_colormanagement_role_colorspace_name_get(COLOR_ROLE_SCENE_LINEAR);
636
637         rr->rectx = rectx;
638         rr->recty = recty;
639         
640         IMB_exr_multilayer_convert(exrhandle, rr, ml_addlayer_cb, ml_addpass_cb);
641
642         for (rl = rr->layers.first; rl; rl = rl->next) {
643                 rl->rectx = rectx;
644                 rl->recty = recty;
645
646                 for (rpass = rl->passes.first; rpass; rpass = rpass->next) {
647                         rpass->rectx = rectx;
648                         rpass->recty = recty;
649
650                         if (rpass->channels >= 3) {
651                                 IMB_colormanagement_transform(rpass->rect, rpass->rectx, rpass->recty, rpass->channels,
652                                                               colorspace, to_colorspace, predivide);
653                         }
654                 }
655         }
656         
657         return rr;
658 }
659
660 /*********************************** Merge ***********************************/
661
662 static void do_merge_tile(RenderResult *rr, RenderResult *rrpart, float *target, float *tile, int pixsize)
663 {
664         int y, ofs, copylen, tilex, tiley;
665         
666         copylen = tilex = rrpart->rectx;
667         tiley = rrpart->recty;
668         
669         if (rrpart->crop) { /* filters add pixel extra */
670                 tile += pixsize * (rrpart->crop + rrpart->crop * tilex);
671                 
672                 copylen = tilex - 2 * rrpart->crop;
673                 tiley -= 2 * rrpart->crop;
674                 
675                 ofs = (rrpart->tilerect.ymin + rrpart->crop) * rr->rectx + (rrpart->tilerect.xmin + rrpart->crop);
676                 target += pixsize * ofs;
677         }
678         else {
679                 ofs = (rrpart->tilerect.ymin * rr->rectx + rrpart->tilerect.xmin);
680                 target += pixsize * ofs;
681         }
682
683         copylen *= sizeof(float) * pixsize;
684         tilex *= pixsize;
685         ofs = pixsize * rr->rectx;
686
687         for (y = 0; y < tiley; y++) {
688                 memcpy(target, tile, copylen);
689                 target += ofs;
690                 tile += tilex;
691         }
692 }
693
694 /* used when rendering to a full buffer, or when reading the exr part-layer-pass file */
695 /* no test happens here if it fits... we also assume layers are in sync */
696 /* is used within threads */
697 void render_result_merge(RenderResult *rr, RenderResult *rrpart)
698 {
699         RenderLayer *rl, *rlp;
700         RenderPass *rpass, *rpassp;
701         
702         for (rl = rr->layers.first; rl; rl = rl->next) {
703                 rlp = RE_GetRenderLayer(rrpart, rl->name);
704                 if (rlp) {
705                         /* combined */
706                         if (rl->rectf && rlp->rectf)
707                                 do_merge_tile(rr, rrpart, rl->rectf, rlp->rectf, 4);
708
709                         /* passes are allocated in sync */
710                         for (rpass = rl->passes.first, rpassp = rlp->passes.first;
711                              rpass && rpassp;
712                              rpass = rpass->next, rpassp = rpassp->next)
713                         {
714                                 do_merge_tile(rr, rrpart, rpass->rect, rpassp->rect, rpass->channels);
715                         }
716                 }
717         }
718 }
719
720 /* for passes read from files, these have names stored */
721 static char *make_pass_name(RenderPass *rpass, int chan)
722 {
723         static char name[16];
724         int len;
725         
726         BLI_strncpy(name, rpass->name, EXR_PASS_MAXNAME);
727         len = strlen(name);
728         name[len] = '.';
729         name[len + 1] = rpass->chan_id[chan];
730         name[len + 2] = 0;
731
732         return name;
733 }
734
735 /* filename already made absolute */
736 /* called from within UI, saves both rendered result as a file-read result */
737 int RE_WriteRenderResult(ReportList *reports, RenderResult *rr, const char *filename, int compress)
738 {
739         RenderLayer *rl;
740         RenderPass *rpass;
741         void *exrhandle = IMB_exr_get_handle();
742         int success;
743
744         BLI_make_existing_file(filename);
745         
746         /* composite result */
747         if (rr->rectf) {
748                 IMB_exr_add_channel(exrhandle, "Composite", "Combined.R", 4, 4 * rr->rectx, rr->rectf);
749                 IMB_exr_add_channel(exrhandle, "Composite", "Combined.G", 4, 4 * rr->rectx, rr->rectf + 1);
750                 IMB_exr_add_channel(exrhandle, "Composite", "Combined.B", 4, 4 * rr->rectx, rr->rectf + 2);
751                 IMB_exr_add_channel(exrhandle, "Composite", "Combined.A", 4, 4 * rr->rectx, rr->rectf + 3);
752         }
753         
754         /* add layers/passes and assign channels */
755         for (rl = rr->layers.first; rl; rl = rl->next) {
756                 
757                 /* combined */
758                 if (rl->rectf) {
759                         int a, xstride = 4;
760                         for (a = 0; a < xstride; a++) {
761                                 IMB_exr_add_channel(exrhandle, rl->name, get_pass_name(SCE_PASS_COMBINED, a),
762                                                     xstride, xstride * rr->rectx, rl->rectf + a);
763                         }
764                 }
765                 
766                 /* passes are allocated in sync */
767                 for (rpass = rl->passes.first; rpass; rpass = rpass->next) {
768                         int a, xstride = rpass->channels;
769                         for (a = 0; a < xstride; a++) {
770                                 if (rpass->passtype) {
771                                         IMB_exr_add_channel(exrhandle, rl->name, get_pass_name(rpass->passtype, a),
772                                                             xstride, xstride * rr->rectx, rpass->rect + a);
773                                 }
774                                 else {
775                                         IMB_exr_add_channel(exrhandle, rl->name, make_pass_name(rpass, a),
776                                                             xstride, xstride * rr->rectx, rpass->rect + a);
777                                 }
778                         }
779                 }
780         }
781
782         /* when the filename has no permissions, this can fail */
783         if (IMB_exr_begin_write(exrhandle, filename, rr->rectx, rr->recty, compress)) {
784                 IMB_exr_write_channels(exrhandle);
785                 success = TRUE;
786         }
787         else {
788                 /* TODO, get the error from openexr's exception */
789                 BKE_report(reports, RPT_ERROR, "Error writing render result (see console)");
790                 success = FALSE;
791         }
792         IMB_exr_close(exrhandle);
793
794         return success;
795 }
796
797 /**************************** Single Layer Rendering *************************/
798
799 void render_result_single_layer_begin(Render *re)
800 {
801         /* all layers except the active one get temporally pushed away */
802
803         /* officially pushed result should be NULL... error can happen with do_seq */
804         RE_FreeRenderResult(re->pushedresult);
805         
806         re->pushedresult = re->result;
807         re->result = NULL;
808 }
809
810 /* if scemode is R_SINGLE_LAYER, at end of rendering, merge the both render results */
811 void render_result_single_layer_end(Render *re)
812 {
813         SceneRenderLayer *srl;
814         RenderLayer *rlpush;
815         RenderLayer *rl;
816         int nr;
817
818         if (re->result == NULL) {
819                 printf("pop render result error; no current result!\n");
820                 return;
821         }
822
823         if (!re->pushedresult)
824                 return;
825
826         if (re->pushedresult->rectx == re->result->rectx && re->pushedresult->recty == re->result->recty) {
827                 /* find which layer in re->pushedresult should be replaced */
828                 rl = re->result->layers.first;
829                 
830                 /* render result should be empty after this */
831                 BLI_remlink(&re->result->layers, rl);
832                 
833                 /* reconstruct render result layers */
834                 for (nr = 0, srl = re->scene->r.layers.first; srl; srl = srl->next, nr++) {
835                         if (nr == re->r.actlay) {
836                                 BLI_addtail(&re->result->layers, rl);
837                         }
838                         else {
839                                 rlpush = RE_GetRenderLayer(re->pushedresult, srl->name);
840                                 if (rlpush) {
841                                         BLI_remlink(&re->pushedresult->layers, rlpush);
842                                         BLI_addtail(&re->result->layers, rlpush);
843                                 }
844                         }
845                 }
846         }
847
848         RE_FreeRenderResult(re->pushedresult);
849         re->pushedresult = NULL;
850 }
851
852 /************************* EXR Tile File Rendering ***************************/
853
854 static void save_render_result_tile(RenderResult *rr, RenderResult *rrpart)
855 {
856         RenderLayer *rlp, *rl;
857         RenderPass *rpassp;
858         int offs, partx, party;
859         
860         BLI_lock_thread(LOCK_IMAGE);
861         
862         for (rlp = rrpart->layers.first; rlp; rlp = rlp->next) {
863                 rl = RE_GetRenderLayer(rr, rlp->name);
864
865                 /* should never happen but prevents crash if it does */
866                 BLI_assert(rl);
867                 if (UNLIKELY(rl == NULL)) {
868                         continue;
869                 }
870
871                 if (rrpart->crop) { /* filters add pixel extra */
872                         offs = (rrpart->crop + rrpart->crop * rrpart->rectx);
873                 }
874                 else {
875                         offs = 0;
876                 }
877                 
878                 /* combined */
879                 if (rlp->rectf) {
880                         int a, xstride = 4;
881                         for (a = 0; a < xstride; a++) {
882                                 IMB_exr_set_channel(rl->exrhandle, rlp->name, get_pass_name(SCE_PASS_COMBINED, a), 
883                                                     xstride, xstride * rrpart->rectx, rlp->rectf + a + xstride * offs);
884                         }
885                 }
886                 
887                 /* passes are allocated in sync */
888                 for (rpassp = rlp->passes.first; rpassp; rpassp = rpassp->next) {
889                         int a, xstride = rpassp->channels;
890                         for (a = 0; a < xstride; a++) {
891                                 IMB_exr_set_channel(rl->exrhandle, rlp->name, get_pass_name(rpassp->passtype, a), 
892                                                     xstride, xstride * rrpart->rectx, rpassp->rect + a + xstride * offs);
893                         }
894                 }
895                 
896         }
897
898         party = rrpart->tilerect.ymin + rrpart->crop;
899         partx = rrpart->tilerect.xmin + rrpart->crop;
900
901         for (rlp = rrpart->layers.first; rlp; rlp = rlp->next) {
902                 rl = RE_GetRenderLayer(rr, rlp->name);
903
904                 /* should never happen but prevents crash if it does */
905                 BLI_assert(rl);
906                 if (UNLIKELY(rl == NULL)) {
907                         continue;
908                 }
909
910                 IMB_exrtile_write_channels(rl->exrhandle, partx, party, 0);
911         }
912
913         BLI_unlock_thread(LOCK_IMAGE);
914 }
915
916 static void save_empty_result_tiles(Render *re)
917 {
918         RenderPart *pa;
919         RenderResult *rr;
920         RenderLayer *rl;
921         
922         for (rr = re->result; rr; rr = rr->next) {
923                 for (rl = rr->layers.first; rl; rl = rl->next) {
924                         IMB_exrtile_clear_channels(rl->exrhandle);
925                 
926                         for (pa = re->parts.first; pa; pa = pa->next) {
927                                 if (pa->status != PART_STATUS_READY) {
928                                         int party = pa->disprect.ymin - re->disprect.ymin + pa->crop;
929                                         int partx = pa->disprect.xmin - re->disprect.xmin + pa->crop;
930                                         IMB_exrtile_write_channels(rl->exrhandle, partx, party, 0);
931                                 }
932                         }
933                 }
934         }
935 }
936
937 /* begin write of exr tile file */
938 void render_result_exr_file_begin(Render *re)
939 {
940         RenderResult *rr;
941         RenderLayer *rl;
942         char str[FILE_MAX];
943
944         for (rr = re->result; rr; rr = rr->next) {
945                 for (rl = rr->layers.first; rl; rl = rl->next) {
946                         render_result_exr_file_path(re->scene, rl->name, rr->sample_nr, str);
947                         printf("write exr tmp file, %dx%d, %s\n", rr->rectx, rr->recty, str);
948                         IMB_exrtile_begin_write(rl->exrhandle, str, 0, rr->rectx, rr->recty, re->partx, re->party);
949                 }
950         }
951 }
952
953 /* end write of exr tile file, read back first sample */
954 void render_result_exr_file_end(Render *re)
955 {
956         RenderResult *rr;
957         RenderLayer *rl;
958
959         save_empty_result_tiles(re);
960         
961         for (rr = re->result; rr; rr = rr->next) {
962                 for (rl = rr->layers.first; rl; rl = rl->next) {
963                         IMB_exr_close(rl->exrhandle);
964                         rl->exrhandle = NULL;
965                 }
966
967                 rr->do_exr_tile = FALSE;
968         }
969         
970         render_result_free_list(&re->fullresult, re->result);
971         re->result = NULL;
972
973         render_result_exr_file_read(re, 0);
974 }
975
976 /* save part into exr file */
977 void render_result_exr_file_merge(RenderResult *rr, RenderResult *rrpart)
978 {
979         for (; rr && rrpart; rr = rr->next, rrpart = rrpart->next)
980                 save_render_result_tile(rr, rrpart);
981 }
982
983 /* path to temporary exr file */
984 void render_result_exr_file_path(Scene *scene, const char *layname, int sample, char *filepath)
985 {
986         char di[FILE_MAX], name[FILE_MAXFILE + MAX_ID_NAME + MAX_ID_NAME + 100], fi[FILE_MAXFILE];
987         
988         BLI_strncpy(di, G.main->name, FILE_MAX);
989         BLI_splitdirstring(di, fi);
990         
991         if (sample == 0)
992                 BLI_snprintf(name, sizeof(name), "%s_%s_%s.exr", fi, scene->id.name + 2, layname);
993         else
994                 BLI_snprintf(name, sizeof(name), "%s_%s_%s%d.exr", fi, scene->id.name + 2, layname, sample);
995
996         BLI_make_file_string("/", filepath, BLI_temporary_dir(), name);
997 }
998
999 /* only for temp buffer files, makes exact copy of render result */
1000 int render_result_exr_file_read(Render *re, int sample)
1001 {
1002         RenderLayer *rl;
1003         char str[FILE_MAX];
1004         int success = TRUE;
1005
1006         RE_FreeRenderResult(re->result);
1007         re->result = render_result_new(re, &re->disprect, 0, RR_USE_MEM, RR_ALL_LAYERS);
1008
1009         for (rl = re->result->layers.first; rl; rl = rl->next) {
1010
1011                 render_result_exr_file_path(re->scene, rl->name, sample, str);
1012                 printf("read exr tmp file: %s\n", str);
1013
1014                 if (!render_result_exr_file_read_path(re->result, rl, str)) {
1015                         printf("cannot read: %s\n", str);
1016                         success = FALSE;
1017
1018                 }
1019         }
1020
1021         return success;
1022 }
1023
1024 /* called for reading temp files, and for external engines */
1025 int render_result_exr_file_read_path(RenderResult *rr, RenderLayer *rl_single, const char *filepath)
1026 {
1027         RenderLayer *rl;
1028         RenderPass *rpass;
1029         void *exrhandle = IMB_exr_get_handle();
1030         int rectx, recty;
1031
1032         if (IMB_exr_begin_read(exrhandle, filepath, &rectx, &recty) == 0) {
1033                 printf("failed being read %s\n", filepath);
1034                 IMB_exr_close(exrhandle);
1035                 return 0;
1036         }
1037
1038         if (rr == NULL || rectx != rr->rectx || recty != rr->recty) {
1039                 if (rr)
1040                         printf("error in reading render result: dimensions don't match\n");
1041                 else
1042                         printf("error in reading render result: NULL result pointer\n");
1043                 IMB_exr_close(exrhandle);
1044                 return 0;
1045         }
1046
1047         for (rl = rr->layers.first; rl; rl = rl->next) {
1048                 if (rl_single && rl_single != rl)
1049                         continue;
1050
1051                 /* combined */
1052                 if (rl->rectf) {
1053                         int a, xstride = 4;
1054                         for (a = 0; a < xstride; a++)
1055                                 IMB_exr_set_channel(exrhandle, rl->name, get_pass_name(SCE_PASS_COMBINED, a), 
1056                                                     xstride, xstride * rectx, rl->rectf + a);
1057                 }
1058                 
1059                 /* passes are allocated in sync */
1060                 for (rpass = rl->passes.first; rpass; rpass = rpass->next) {
1061                         int a, xstride = rpass->channels;
1062                         for (a = 0; a < xstride; a++)
1063                                 IMB_exr_set_channel(exrhandle, rl->name, get_pass_name(rpass->passtype, a), 
1064                                                     xstride, xstride * rectx, rpass->rect + a);
1065
1066                         BLI_strncpy(rpass->name, get_pass_name(rpass->passtype, -1), sizeof(rpass->name));
1067                 }
1068         }
1069
1070         IMB_exr_read_channels(exrhandle);
1071         IMB_exr_close(exrhandle);
1072
1073         return 1;
1074 }
1075
1076 /*************************** Combined Pixel Rect *****************************/
1077
1078 ImBuf *render_result_rect_to_ibuf(RenderResult *rr, RenderData *rd)
1079 {
1080         ImBuf *ibuf = IMB_allocImBuf(rr->rectx, rr->recty, rd->im_format.planes, 0);
1081         
1082         /* if not exists, BKE_imbuf_write makes one */
1083         ibuf->rect = (unsigned int *)rr->rect32;
1084         ibuf->rect_float = rr->rectf;
1085         ibuf->zbuf_float = rr->rectz;
1086         
1087         /* float factor for random dither, imbuf takes care of it */
1088         ibuf->dither = rd->dither_intensity;
1089         
1090         /* prepare to gamma correct to sRGB color space
1091          * note that sequence editor can generate 8bpc render buffers
1092          */
1093         if (ibuf->rect) {
1094                 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)) {
1095                         IMB_float_from_rect(ibuf);
1096                 }
1097                 else  {
1098                         /* ensure no float buffer remained from previous frame */
1099                         ibuf->rect_float = NULL;
1100                 }
1101         }
1102
1103         /* color -> grayscale */
1104         /* editing directly would alter the render view */
1105         if (rd->im_format.planes == R_IMF_PLANES_BW) {
1106                 ImBuf *ibuf_bw = IMB_dupImBuf(ibuf);
1107                 IMB_color_to_bw(ibuf_bw);
1108                 IMB_freeImBuf(ibuf);
1109                 ibuf = ibuf_bw;
1110         }
1111
1112         return ibuf;
1113 }
1114
1115 void render_result_rect_from_ibuf(RenderResult *rr, RenderData *UNUSED(rd), ImBuf *ibuf)
1116 {
1117         if (ibuf->rect_float) {
1118                 if (!rr->rectf)
1119                         rr->rectf = MEM_mallocN(4 * sizeof(float) * rr->rectx * rr->recty, "render_seq rectf");
1120                 
1121                 memcpy(rr->rectf, ibuf->rect_float, 4 * sizeof(float) * rr->rectx * rr->recty);
1122
1123                 /* TSK! Since sequence render doesn't free the *rr render result, the old rect32
1124                  * can hang around when sequence render has rendered a 32 bits one before */
1125                 if (rr->rect32) {
1126                         MEM_freeN(rr->rect32);
1127                         rr->rect32 = NULL;
1128                 }
1129         }
1130         else if (ibuf->rect) {
1131                 if (!rr->rect32)
1132                         rr->rect32 = MEM_mallocN(sizeof(int) * rr->rectx * rr->recty, "render_seq rect");
1133
1134                 memcpy(rr->rect32, ibuf->rect, 4 * rr->rectx * rr->recty);
1135
1136                 /* Same things as above, old rectf can hang around from previous render. */
1137                 if (rr->rectf) {
1138                         MEM_freeN(rr->rectf);
1139                         rr->rectf = NULL;
1140                 }
1141         }
1142 }
1143
1144 void render_result_rect_fill_zero(RenderResult *rr)
1145 {
1146         if (rr->rectf)
1147                 memset(rr->rectf, 0, 4 * sizeof(float) * rr->rectx * rr->recty);
1148         else if (rr->rect32)
1149                 memset(rr->rect32, 0, 4 * rr->rectx * rr->recty);
1150         else
1151                 rr->rect32 = MEM_callocN(sizeof(int) * rr->rectx * rr->recty, "render_seq rect");
1152 }
1153
1154 void render_result_rect_get_pixels(RenderResult *rr, unsigned int *rect, int rectx, int recty,
1155                                    const ColorManagedViewSettings *view_settings, const ColorManagedDisplaySettings *display_settings)
1156 {
1157         if (rr->rect32) {
1158                 memcpy(rect, rr->rect32, sizeof(int) * rr->rectx * rr->recty);
1159         }
1160         else if (rr->rectf) {
1161                 IMB_display_buffer_transform_apply((unsigned char *) rect, rr->rectf, rr->rectx, rr->recty, 4,
1162                                                    view_settings, display_settings, TRUE);
1163         }
1164         else
1165                 /* else fill with black */
1166                 memset(rect, 0, sizeof(int) * rectx * recty);
1167 }
1168