mirrored from https://projects.blender.org/blender/blender.git
/
pipeline.c
2943 lines (2443 loc) · 82.5 KB
/
pipeline.c
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
/*
* 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.
*/
/** \file
* \ingroup render
*/
#include <errno.h>
#include <limits.h>
#include <math.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include "DNA_anim_types.h"
#include "DNA_collection_types.h"
#include "DNA_image_types.h"
#include "DNA_node_types.h"
#include "DNA_object_types.h"
#include "DNA_particle_types.h"
#include "DNA_scene_types.h"
#include "DNA_sequence_types.h"
#include "DNA_space_types.h"
#include "DNA_userdef_types.h"
#include "MEM_guardedalloc.h"
#include "BLI_fileops.h"
#include "BLI_listbase.h"
#include "BLI_math.h"
#include "BLI_path_util.h"
#include "BLI_rect.h"
#include "BLI_string.h"
#include "BLI_threads.h"
#include "BLI_timecode.h"
#include "BLT_translation.h"
#include "BKE_anim_data.h"
#include "BKE_animsys.h" /* <------ should this be here?, needed for sequencer update */
#include "BKE_callbacks.h"
#include "BKE_camera.h"
#include "BKE_colortools.h"
#include "BKE_context.h" /* XXX needed by wm_window.h */
#include "BKE_global.h"
#include "BKE_image.h"
#include "BKE_layer.h"
#include "BKE_lib_id.h"
#include "BKE_lib_remap.h"
#include "BKE_mask.h"
#include "BKE_modifier.h"
#include "BKE_node.h"
#include "BKE_object.h"
#include "BKE_pointcache.h"
#include "BKE_report.h"
#include "BKE_scene.h"
#include "BKE_sequencer.h"
#include "BKE_sound.h"
#include "BKE_writeavi.h" /* <------ should be replaced once with generic movie module */
#include "DEG_depsgraph.h"
#include "DEG_depsgraph_build.h"
#include "DEG_depsgraph_debug.h"
#include "DEG_depsgraph_query.h"
#include "IMB_colormanagement.h"
#include "IMB_imbuf.h"
#include "IMB_imbuf_types.h"
#include "IMB_metadata.h"
#include "PIL_time.h"
#include "RE_engine.h"
#include "RE_pipeline.h"
#include "RE_render_ext.h"
#include "../../../windowmanager/WM_api.h" /* XXX */
#include "../../../windowmanager/wm_window.h" /* XXX */
#include "GPU_context.h"
#ifdef WITH_FREESTYLE
# include "FRS_freestyle.h"
#endif
#include "DEG_depsgraph.h"
/* internal */
#include "initrender.h"
#include "render_result.h"
#include "render_types.h"
#include "renderpipeline.h"
/* render flow
*
* 1) Initialize state
* - state data, tables
* - movie/image file init
* - everything that doesn't change during animation
*
* 2) Initialize data
* - camera, world, matrices
* - make render verts, faces, halos, strands
* - everything can change per frame/field
*
* 3) Render Processor
* - multiple layers
* - tiles, rect, baking
* - layers/tiles optionally to disk or directly in Render Result
*
* 4) Composite Render Result
* - also read external files etc
*
* 5) Image Files
* - save file or append in movie
*/
/* ********* globals ******** */
/* here we store all renders */
static struct {
ListBase renderlist;
} RenderGlobal = {{NULL, NULL}};
/* ********* callbacks ******** */
static void render_callback_exec_null(Render *re, Main *bmain, eCbEvent evt)
{
if (re->r.scemode & R_BUTS_PREVIEW) {
return;
}
BKE_callback_exec_null(bmain, evt);
}
static void render_callback_exec_id(Render *re, Main *bmain, ID *id, eCbEvent evt)
{
if (re->r.scemode & R_BUTS_PREVIEW) {
return;
}
BKE_callback_exec_id(bmain, id, evt);
}
/* ********* alloc and free ******** */
static int do_write_image_or_movie(Render *re,
Main *bmain,
Scene *scene,
bMovieHandle *mh,
const int totvideos,
const char *name_override);
/* default callbacks, set in each new render */
static void result_nothing(void *UNUSED(arg), RenderResult *UNUSED(rr))
{
}
static void result_rcti_nothing(void *UNUSED(arg),
RenderResult *UNUSED(rr),
volatile struct rcti *UNUSED(rect))
{
}
static void current_scene_nothing(void *UNUSED(arg), Scene *UNUSED(scene))
{
}
static void stats_nothing(void *UNUSED(arg), RenderStats *UNUSED(rs))
{
}
static void float_nothing(void *UNUSED(arg), float UNUSED(val))
{
}
static int default_break(void *UNUSED(arg))
{
return G.is_break == true;
}
static void stats_background(void *UNUSED(arg), RenderStats *rs)
{
uintptr_t mem_in_use, peak_memory;
float megs_used_memory, megs_peak_memory;
char info_time_str[32];
mem_in_use = MEM_get_memory_in_use();
peak_memory = MEM_get_peak_memory();
megs_used_memory = (mem_in_use) / (1024.0 * 1024.0);
megs_peak_memory = (peak_memory) / (1024.0 * 1024.0);
fprintf(stdout,
TIP_("Fra:%d Mem:%.2fM (Peak %.2fM) "),
rs->cfra,
megs_used_memory,
megs_peak_memory);
BLI_timecode_string_from_time_simple(
info_time_str, sizeof(info_time_str), PIL_check_seconds_timer() - rs->starttime);
fprintf(stdout, TIP_("| Time:%s | "), info_time_str);
if (rs->infostr) {
fprintf(stdout, "%s", rs->infostr);
}
else {
fprintf(stdout,
TIP_("Sce: %s Ve:%d Fa:%d La:%d"),
rs->scene_name,
rs->totvert,
rs->totface,
rs->totlamp);
}
/* Flush stdout to be sure python callbacks are printing stuff after blender. */
fflush(stdout);
/* NOTE: using G_MAIN seems valid here???
* Not sure it's actually even used anyway, we could as well pass NULL? */
BKE_callback_exec_null(G_MAIN, BKE_CB_EVT_RENDER_STATS);
fputc('\n', stdout);
fflush(stdout);
}
static void render_print_save_message(ReportList *reports, const char *name, int ok, int err)
{
if (ok) {
/* no need to report, just some helpful console info */
printf("Saved: '%s'\n", name);
}
else {
/* report on error since users will want to know what failed */
BKE_reportf(reports, RPT_ERROR, "Render error (%s) cannot save: '%s'", strerror(err), name);
}
}
static int render_imbuf_write_stamp_test(ReportList *reports,
Scene *scene,
struct RenderResult *rr,
ImBuf *ibuf,
const char *name,
const ImageFormatData *imf,
bool stamp)
{
int ok;
if (stamp) {
/* writes the name of the individual cameras */
ok = BKE_imbuf_write_stamp(scene, rr, ibuf, name, imf);
}
else {
ok = BKE_imbuf_write(ibuf, name, imf);
}
render_print_save_message(reports, name, ok, errno);
return ok;
}
void RE_FreeRenderResult(RenderResult *rr)
{
render_result_free(rr);
}
float *RE_RenderLayerGetPass(volatile RenderLayer *rl, const char *name, const char *viewname)
{
RenderPass *rpass = RE_pass_find_by_name(rl, name, viewname);
return rpass ? rpass->rect : NULL;
}
RenderLayer *RE_GetRenderLayer(RenderResult *rr, const char *name)
{
if (rr == NULL) {
return NULL;
}
return BLI_findstring(&rr->layers, name, offsetof(RenderLayer, name));
}
bool RE_HasSingleLayer(Render *re)
{
return (re->r.scemode & R_SINGLE_LAYER);
}
RenderResult *RE_MultilayerConvert(
void *exrhandle, const char *colorspace, bool predivide, int rectx, int recty)
{
return render_result_new_from_exr(exrhandle, colorspace, predivide, rectx, recty);
}
RenderLayer *render_get_active_layer(Render *re, RenderResult *rr)
{
ViewLayer *view_layer = BLI_findlink(&re->view_layers, re->active_view_layer);
if (view_layer) {
RenderLayer *rl = BLI_findstring(&rr->layers, view_layer->name, offsetof(RenderLayer, name));
if (rl) {
return rl;
}
}
return rr->layers.first;
}
static bool render_scene_has_layers_to_render(Scene *scene, ViewLayer *single_layer)
{
if (single_layer) {
return true;
}
ViewLayer *view_layer;
for (view_layer = scene->view_layers.first; view_layer; view_layer = view_layer->next) {
if (view_layer->flag & VIEW_LAYER_RENDER) {
return true;
}
}
return false;
}
/* *************************************************** */
Render *RE_GetRender(const char *name)
{
Render *re;
/* search for existing renders */
for (re = RenderGlobal.renderlist.first; re; re = re->next) {
if (STREQLEN(re->name, name, RE_MAXNAME)) {
break;
}
}
return re;
}
/* if you want to know exactly what has been done */
RenderResult *RE_AcquireResultRead(Render *re)
{
if (re) {
BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_READ);
return re->result;
}
return NULL;
}
RenderResult *RE_AcquireResultWrite(Render *re)
{
if (re) {
BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
return re->result;
}
return NULL;
}
void RE_ClearResult(Render *re)
{
if (re) {
render_result_free(re->result);
re->result = NULL;
}
}
void RE_SwapResult(Render *re, RenderResult **rr)
{
/* for keeping render buffers */
if (re) {
SWAP(RenderResult *, re->result, *rr);
}
}
void RE_ReleaseResult(Render *re)
{
if (re) {
BLI_rw_mutex_unlock(&re->resultmutex);
}
}
/* displist.c util.... */
Scene *RE_GetScene(Render *re)
{
if (re) {
return re->scene;
}
return NULL;
}
void RE_SetScene(Render *re, Scene *sce)
{
if (re) {
re->scene = sce;
}
}
/**
* Same as #RE_AcquireResultImage but creating the necessary views to store the result
* fill provided result struct with a copy of thew views of what is done so far the
* #RenderResult.views #ListBase needs to be freed after with #RE_ReleaseResultImageViews
*/
void RE_AcquireResultImageViews(Render *re, RenderResult *rr)
{
memset(rr, 0, sizeof(RenderResult));
if (re) {
BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_READ);
if (re->result) {
RenderLayer *rl;
RenderView *rv, *rview;
rr->rectx = re->result->rectx;
rr->recty = re->result->recty;
/* creates a temporary duplication of views */
render_result_views_shallowcopy(rr, re->result);
rv = rr->views.first;
rr->have_combined = (rv->rectf != NULL);
/* active layer */
rl = render_get_active_layer(re, re->result);
if (rl) {
if (rv->rectf == NULL) {
for (rview = (RenderView *)rr->views.first; rview; rview = rview->next) {
rview->rectf = RE_RenderLayerGetPass(rl, RE_PASSNAME_COMBINED, rview->name);
}
}
if (rv->rectz == NULL) {
for (rview = (RenderView *)rr->views.first; rview; rview = rview->next) {
rview->rectz = RE_RenderLayerGetPass(rl, RE_PASSNAME_Z, rview->name);
}
}
}
rr->layers = re->result->layers;
rr->xof = re->disprect.xmin;
rr->yof = re->disprect.ymin;
rr->stamp_data = re->result->stamp_data;
}
}
}
/* clear temporary renderresult struct */
void RE_ReleaseResultImageViews(Render *re, RenderResult *rr)
{
if (re) {
if (rr) {
render_result_views_shallowdelete(rr);
}
BLI_rw_mutex_unlock(&re->resultmutex);
}
}
/* fill provided result struct with what's currently active or done */
/* this RenderResult struct is the only exception to the rule of a RenderResult */
/* always having at least one RenderView */
void RE_AcquireResultImage(Render *re, RenderResult *rr, const int view_id)
{
memset(rr, 0, sizeof(RenderResult));
if (re) {
BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_READ);
if (re->result) {
RenderLayer *rl;
RenderView *rv;
rr->rectx = re->result->rectx;
rr->recty = re->result->recty;
/* actview view */
rv = RE_RenderViewGetById(re->result, view_id);
rr->have_combined = (rv->rectf != NULL);
rr->rectf = rv->rectf;
rr->rectz = rv->rectz;
rr->rect32 = rv->rect32;
/* active layer */
rl = render_get_active_layer(re, re->result);
if (rl) {
if (rv->rectf == NULL) {
rr->rectf = RE_RenderLayerGetPass(rl, RE_PASSNAME_COMBINED, rv->name);
}
if (rv->rectz == NULL) {
rr->rectz = RE_RenderLayerGetPass(rl, RE_PASSNAME_Z, rv->name);
}
}
rr->layers = re->result->layers;
rr->views = re->result->views;
rr->xof = re->disprect.xmin;
rr->yof = re->disprect.ymin;
rr->stamp_data = re->result->stamp_data;
}
}
}
void RE_ReleaseResultImage(Render *re)
{
if (re) {
BLI_rw_mutex_unlock(&re->resultmutex);
}
}
/* caller is responsible for allocating rect in correct size! */
void RE_ResultGet32(Render *re, unsigned int *rect)
{
RenderResult rres;
const int view_id = BKE_scene_multiview_view_id_get(&re->r, re->viewname);
RE_AcquireResultImageViews(re, &rres);
render_result_rect_get_pixels(&rres,
rect,
re->rectx,
re->recty,
&re->scene->view_settings,
&re->scene->display_settings,
view_id);
RE_ReleaseResultImageViews(re, &rres);
}
/* caller is responsible for allocating rect in correct size! */
/* Only for acquired results, for lock */
void RE_AcquiredResultGet32(Render *re,
RenderResult *result,
unsigned int *rect,
const int view_id)
{
render_result_rect_get_pixels(result,
rect,
re->rectx,
re->recty,
&re->scene->view_settings,
&re->scene->display_settings,
view_id);
}
RenderStats *RE_GetStats(Render *re)
{
return &re->i;
}
Render *RE_NewRender(const char *name)
{
Render *re;
/* only one render per name exists */
re = RE_GetRender(name);
if (re == NULL) {
/* new render data struct */
re = MEM_callocN(sizeof(Render), "new render");
BLI_addtail(&RenderGlobal.renderlist, re);
BLI_strncpy(re->name, name, RE_MAXNAME);
BLI_rw_mutex_init(&re->resultmutex);
BLI_rw_mutex_init(&re->partsmutex);
}
RE_InitRenderCB(re);
return re;
}
/* MAX_ID_NAME + sizeof(Library->name) + space + null-terminator. */
#define MAX_SCENE_RENDER_NAME (MAX_ID_NAME + 1024 + 2)
static void scene_render_name_get(const Scene *scene, const size_t max_size, char *render_name)
{
if (ID_IS_LINKED(scene)) {
BLI_snprintf(render_name, max_size, "%s %s", scene->id.lib->id.name, scene->id.name);
}
else {
BLI_snprintf(render_name, max_size, "%s", scene->id.name);
}
}
Render *RE_GetSceneRender(const Scene *scene)
{
char render_name[MAX_SCENE_RENDER_NAME];
scene_render_name_get(scene, sizeof(render_name), render_name);
return RE_GetRender(render_name);
}
Render *RE_NewSceneRender(const Scene *scene)
{
char render_name[MAX_SCENE_RENDER_NAME];
scene_render_name_get(scene, sizeof(render_name), render_name);
return RE_NewRender(render_name);
}
/* called for new renders and when finishing rendering so
* we always have valid callbacks on a render */
void RE_InitRenderCB(Render *re)
{
/* set default empty callbacks */
re->display_init = result_nothing;
re->display_clear = result_nothing;
re->display_update = result_rcti_nothing;
re->current_scene_update = current_scene_nothing;
re->progress = float_nothing;
re->test_break = default_break;
if (G.background) {
re->stats_draw = stats_background;
}
else {
re->stats_draw = stats_nothing;
}
/* clear callback handles */
re->dih = re->dch = re->duh = re->sdh = re->prh = re->tbh = NULL;
}
/* only call this while you know it will remove the link too */
void RE_FreeRender(Render *re)
{
if (re->engine) {
RE_engine_free(re->engine);
}
BLI_rw_mutex_end(&re->resultmutex);
BLI_rw_mutex_end(&re->partsmutex);
BLI_freelistN(&re->view_layers);
BLI_freelistN(&re->r.views);
BKE_curvemapping_free_data(&re->r.mblur_shutter_curve);
/* main dbase can already be invalid now, some database-free code checks it */
re->main = NULL;
re->scene = NULL;
render_result_free(re->result);
render_result_free(re->pushedresult);
BLI_remlink(&RenderGlobal.renderlist, re);
MEM_freeN(re);
}
/* exit blender */
void RE_FreeAllRender(void)
{
while (RenderGlobal.renderlist.first) {
RE_FreeRender(RenderGlobal.renderlist.first);
}
#ifdef WITH_FREESTYLE
/* finalize Freestyle */
FRS_exit();
#endif
}
void RE_FreeAllPersistentData(void)
{
Render *re;
for (re = RenderGlobal.renderlist.first; re != NULL; re = re->next) {
if ((re->r.mode & R_PERSISTENT_DATA) != 0 && re->engine != NULL) {
RE_engine_free(re->engine);
re->engine = NULL;
}
}
}
/* on file load, free all re */
void RE_FreeAllRenderResults(void)
{
Render *re;
for (re = RenderGlobal.renderlist.first; re; re = re->next) {
render_result_free(re->result);
render_result_free(re->pushedresult);
re->result = NULL;
re->pushedresult = NULL;
}
}
void RE_FreePersistentData(void)
{
Render *re;
/* render engines can be kept around for quick re-render, this clears all */
for (re = RenderGlobal.renderlist.first; re; re = re->next) {
if (re->engine) {
/* if engine is currently rendering, just tag it to be freed when render is finished */
if (!(re->engine->flag & RE_ENGINE_RENDERING)) {
RE_engine_free(re->engine);
}
re->engine = NULL;
}
}
}
/* ********* initialize state ******** */
/* clear full sample and tile flags if needed */
static int check_mode_full_sample(RenderData *rd)
{
int scemode = rd->scemode;
/* not supported by any current renderer */
scemode &= ~R_FULL_SAMPLE;
#ifdef WITH_OPENEXR
if (scemode & R_FULL_SAMPLE) {
scemode |= R_EXR_TILE_FILE; /* enable automatic */
}
#else
/* can't do this without openexr support */
scemode &= ~(R_EXR_TILE_FILE | R_FULL_SAMPLE);
#endif
return scemode;
}
static void re_init_resolution(Render *re, Render *source, int winx, int winy, rcti *disprect)
{
re->winx = winx;
re->winy = winy;
if (source && (source->r.mode & R_BORDER)) {
/* eeh, doesn't seem original bordered disprect is storing anywhere
* after insertion on black happening in do_render(),
* so for now simply re-calculate disprect using border from source
* renderer (sergey)
*/
re->disprect.xmin = source->r.border.xmin * winx;
re->disprect.xmax = source->r.border.xmax * winx;
re->disprect.ymin = source->r.border.ymin * winy;
re->disprect.ymax = source->r.border.ymax * winy;
re->rectx = BLI_rcti_size_x(&re->disprect);
re->recty = BLI_rcti_size_y(&re->disprect);
/* copy border itself, since it could be used by external engines */
re->r.border = source->r.border;
}
else if (disprect) {
re->disprect = *disprect;
re->rectx = BLI_rcti_size_x(&re->disprect);
re->recty = BLI_rcti_size_y(&re->disprect);
}
else {
re->disprect.xmin = re->disprect.ymin = 0;
re->disprect.xmax = winx;
re->disprect.ymax = winy;
re->rectx = winx;
re->recty = winy;
}
}
void render_copy_renderdata(RenderData *to, RenderData *from)
{
BLI_freelistN(&to->views);
BKE_curvemapping_free_data(&to->mblur_shutter_curve);
*to = *from;
BLI_duplicatelist(&to->views, &from->views);
BKE_curvemapping_copy_data(&to->mblur_shutter_curve, &from->mblur_shutter_curve);
}
/* what doesn't change during entire render sequence */
/* disprect is optional, if NULL it assumes full window render */
void RE_InitState(Render *re,
Render *source,
RenderData *rd,
ListBase *render_layers,
ViewLayer *single_layer,
int winx,
int winy,
rcti *disprect)
{
bool had_freestyle = (re->r.mode & R_EDGE_FRS) != 0;
re->ok = true; /* maybe flag */
re->i.starttime = PIL_check_seconds_timer();
/* copy render data and render layers for thread safety */
render_copy_renderdata(&re->r, rd);
BLI_freelistN(&re->view_layers);
BLI_duplicatelist(&re->view_layers, render_layers);
re->active_view_layer = 0;
if (source) {
/* reuse border flags from source renderer */
re->r.mode &= ~(R_BORDER | R_CROP);
re->r.mode |= source->r.mode & (R_BORDER | R_CROP);
/* dimensions shall be shared between all renderers */
re->r.xsch = source->r.xsch;
re->r.ysch = source->r.ysch;
re->r.size = source->r.size;
}
re_init_resolution(re, source, winx, winy, disprect);
/* disable border if it's a full render anyway */
if (re->r.border.xmin == 0.0f && re->r.border.xmax == 1.0f && re->r.border.ymin == 0.0f &&
re->r.border.ymax == 1.0f) {
re->r.mode &= ~R_BORDER;
}
if (re->rectx < 1 || re->recty < 1 ||
(BKE_imtype_is_movie(rd->im_format.imtype) && (re->rectx < 16 || re->recty < 16))) {
BKE_report(re->reports, RPT_ERROR, "Image too small");
re->ok = 0;
return;
}
re->r.scemode = check_mode_full_sample(&re->r);
if (single_layer) {
int index = BLI_findindex(render_layers, single_layer);
if (index != -1) {
re->active_view_layer = index;
re->r.scemode |= R_SINGLE_LAYER;
}
}
/* if preview render, we try to keep old result */
BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
if (re->r.scemode & R_BUTS_PREVIEW) {
if (had_freestyle || (re->r.mode & R_EDGE_FRS)) {
/* freestyle manipulates render layers so always have to free */
render_result_free(re->result);
re->result = NULL;
}
else if (re->result) {
ViewLayer *active_render_layer = BLI_findlink(&re->view_layers, re->active_view_layer);
RenderLayer *rl;
bool have_layer = false;
for (rl = re->result->layers.first; rl; rl = rl->next) {
if (STREQ(rl->name, active_render_layer->name)) {
have_layer = true;
}
}
if (re->result->rectx == re->rectx && re->result->recty == re->recty && have_layer) {
/* keep render result, this avoids flickering black tiles
* when the preview changes */
}
else {
/* free because resolution changed */
render_result_free(re->result);
re->result = NULL;
}
}
}
else {
/* make empty render result, so display callbacks can initialize */
render_result_free(re->result);
re->result = MEM_callocN(sizeof(RenderResult), "new render result");
re->result->rectx = re->rectx;
re->result->recty = re->recty;
render_result_view_new(re->result, "");
}
/* ensure renderdatabase can use part settings correct */
RE_parts_clamp(re);
BLI_rw_mutex_unlock(&re->resultmutex);
RE_init_threadcount(re);
RE_point_density_fix_linking();
}
/* This function is only called by view3d rendering, which doesn't support
* multiview at the moment. so handle only one view here */
static void render_result_rescale(Render *re)
{
RenderResult *result = re->result;
RenderView *rv;
int x, y;
float scale_x, scale_y;
float *src_rectf;
rv = RE_RenderViewGetById(result, 0);
src_rectf = rv->rectf;
if (src_rectf == NULL) {
RenderLayer *rl = render_get_active_layer(re, re->result);
if (rl != NULL) {
src_rectf = RE_RenderLayerGetPass(rl, RE_PASSNAME_COMBINED, NULL);
}
}
if (src_rectf != NULL) {
float *dst_rectf = NULL;
re->result = render_result_new(re, &re->disprect, 0, RR_USE_MEM, RR_ALL_LAYERS, "");
if (re->result != NULL) {
dst_rectf = RE_RenderViewGetById(re->result, 0)->rectf;
if (dst_rectf == NULL) {
RenderLayer *rl;
rl = render_get_active_layer(re, re->result);
if (rl != NULL) {
dst_rectf = RE_RenderLayerGetPass(rl, RE_PASSNAME_COMBINED, NULL);
}
}
scale_x = (float)result->rectx / re->result->rectx;
scale_y = (float)result->recty / re->result->recty;
for (x = 0; x < re->result->rectx; x++) {
for (y = 0; y < re->result->recty; y++) {
int src_x = x * scale_x;
int src_y = y * scale_y;
int dst_index = y * re->result->rectx + x;
int src_index = src_y * result->rectx + src_x;
copy_v4_v4(dst_rectf + dst_index * 4, src_rectf + src_index * 4);
}
}
}
render_result_free(result);
}
}
void RE_ChangeResolution(Render *re, int winx, int winy, rcti *disprect)
{
re_init_resolution(re, NULL, winx, winy, disprect);
RE_parts_clamp(re);
if (re->result) {
BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
render_result_rescale(re);
BLI_rw_mutex_unlock(&re->resultmutex);
}
}
/* TODO(sergey): This is a bit hackish, used to temporary disable freestyle when
* doing viewport render. Needs some better integration of BI viewport rendering
* into the pipeline.
*/
void RE_ChangeModeFlag(Render *re, int flag, bool clear)
{
if (clear) {
re->r.mode &= ~flag;
}
else {
re->r.mode |= flag;
}
}
/* update some variables that can be animated, and otherwise wouldn't be due to
* RenderData getting copied once at the start of animation render */
void render_update_anim_renderdata(Render *re, RenderData *rd, ListBase *render_layers)
{
/* filter */
re->r.gauss = rd->gauss;
/* motion blur */
re->r.blurfac = rd->blurfac;
/* freestyle */
re->r.line_thickness_mode = rd->line_thickness_mode;
re->r.unit_line_thickness = rd->unit_line_thickness;
/* render layers */
BLI_freelistN(&re->view_layers);
BLI_duplicatelist(&re->view_layers, render_layers);
/* render views */
BLI_freelistN(&re->r.views);
BLI_duplicatelist(&re->r.views, &rd->views);
}
void RE_SetWindow(Render *re, const rctf *viewplane, float clip_start, float clip_end)
{
/* re->ok flag? */
re->viewplane = *viewplane;
re->clip_start = clip_start;
re->clip_end = clip_end;
perspective_m4(re->winmat,
re->viewplane.xmin,
re->viewplane.xmax,