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