1 /*
2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License
4 * as published by the Free Software Foundation; either version 2
5 * of the License, or (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software Foundation,
14 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15 *
16 * The Original Code is Copyright (C) 2006 Blender Foundation.
17 * All rights reserved.
18 */
19
20 /** \file
21 * \ingroup render
22 */
23
24 #include <errno.h>
25 #include <limits.h>
26 #include <math.h>
27 #include <stddef.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include "DNA_anim_types.h"
32 #include "DNA_collection_types.h"
33 #include "DNA_image_types.h"
34 #include "DNA_node_types.h"
35 #include "DNA_object_types.h"
36 #include "DNA_particle_types.h"
37 #include "DNA_scene_types.h"
38 #include "DNA_sequence_types.h"
39 #include "DNA_space_types.h"
40 #include "DNA_userdef_types.h"
41
42 #include "MEM_guardedalloc.h"
43
44 #include "BLI_fileops.h"
45 #include "BLI_listbase.h"
46 #include "BLI_math.h"
47 #include "BLI_path_util.h"
48 #include "BLI_rect.h"
49 #include "BLI_string.h"
50 #include "BLI_threads.h"
51 #include "BLI_timecode.h"
52
53 #include "BLT_translation.h"
54
55 #include "BKE_anim_data.h"
56 #include "BKE_animsys.h" /* <------ should this be here?, needed for sequencer update */
57 #include "BKE_callbacks.h"
58 #include "BKE_camera.h"
59 #include "BKE_colortools.h"
60 #include "BKE_context.h" /* XXX needed by wm_window.h */
61 #include "BKE_global.h"
62 #include "BKE_image.h"
63 #include "BKE_layer.h"
64 #include "BKE_lib_id.h"
65 #include "BKE_lib_remap.h"
66 #include "BKE_mask.h"
67 #include "BKE_modifier.h"
68 #include "BKE_node.h"
69 #include "BKE_object.h"
70 #include "BKE_pointcache.h"
71 #include "BKE_report.h"
72 #include "BKE_scene.h"
73 #include "BKE_sequencer.h"
74 #include "BKE_sound.h"
75 #include "BKE_writeavi.h" /* <------ should be replaced once with generic movie module */
76
77 #include "DEG_depsgraph.h"
78 #include "DEG_depsgraph_build.h"
79 #include "DEG_depsgraph_debug.h"
80 #include "DEG_depsgraph_query.h"
81
82 #include "IMB_colormanagement.h"
83 #include "IMB_imbuf.h"
84 #include "IMB_imbuf_types.h"
85 #include "IMB_metadata.h"
86 #include "PIL_time.h"
87
88 #include "RE_engine.h"
89 #include "RE_pipeline.h"
90 #include "RE_render_ext.h"
91
92 #include "../../../windowmanager/WM_api.h" /* XXX */
93 #include "../../../windowmanager/wm_window.h" /* XXX */
94 #include "GPU_context.h"
95
96 #ifdef WITH_FREESTYLE
97 # include "FRS_freestyle.h"
98 #endif
99
100 #include "DEG_depsgraph.h"
101
102 /* internal */
103 #include "initrender.h"
104 #include "render_result.h"
105 #include "render_types.h"
106 #include "renderpipeline.h"
107
108 /* render flow
109 *
110 * 1) Initialize state
111 * - state data, tables
112 * - movie/image file init
113 * - everything that doesn't change during animation
114 *
115 * 2) Initialize data
116 * - camera, world, matrices
117 * - make render verts, faces, halos, strands
118 * - everything can change per frame/field
119 *
120 * 3) Render Processor
121 * - multiple layers
122 * - tiles, rect, baking
123 * - layers/tiles optionally to disk or directly in Render Result
124 *
125 * 4) Composite Render Result
126 * - also read external files etc
127 *
128 * 5) Image Files
129 * - save file or append in movie
130 */
131
132 /* ********* globals ******** */
133
134 /* here we store all renders */
135 static struct {
136 ListBase renderlist;
137 } RenderGlobal = {{NULL, NULL}};
138
139 /* ********* callbacks ******** */
140
render_callback_exec_null(Render * re,Main * bmain,eCbEvent evt)141 static void render_callback_exec_null(Render *re, Main *bmain, eCbEvent evt)
142 {
143 if (re->r.scemode & R_BUTS_PREVIEW) {
144 return;
145 }
146 BKE_callback_exec_null(bmain, evt);
147 }
148
render_callback_exec_id(Render * re,Main * bmain,ID * id,eCbEvent evt)149 static void render_callback_exec_id(Render *re, Main *bmain, ID *id, eCbEvent evt)
150 {
151 if (re->r.scemode & R_BUTS_PREVIEW) {
152 return;
153 }
154 BKE_callback_exec_id(bmain, id, evt);
155 }
156
157 /* ********* alloc and free ******** */
158
159 static int do_write_image_or_movie(Render *re,
160 Main *bmain,
161 Scene *scene,
162 bMovieHandle *mh,
163 const int totvideos,
164 const char *name_override);
165
166 /* default callbacks, set in each new render */
result_nothing(void * UNUSED (arg),RenderResult * UNUSED (rr))167 static void result_nothing(void *UNUSED(arg), RenderResult *UNUSED(rr))
168 {
169 }
result_rcti_nothing(void * UNUSED (arg),RenderResult * UNUSED (rr),volatile struct rcti * UNUSED (rect))170 static void result_rcti_nothing(void *UNUSED(arg),
171 RenderResult *UNUSED(rr),
172 volatile struct rcti *UNUSED(rect))
173 {
174 }
current_scene_nothing(void * UNUSED (arg),Scene * UNUSED (scene))175 static void current_scene_nothing(void *UNUSED(arg), Scene *UNUSED(scene))
176 {
177 }
stats_nothing(void * UNUSED (arg),RenderStats * UNUSED (rs))178 static void stats_nothing(void *UNUSED(arg), RenderStats *UNUSED(rs))
179 {
180 }
float_nothing(void * UNUSED (arg),float UNUSED (val))181 static void float_nothing(void *UNUSED(arg), float UNUSED(val))
182 {
183 }
default_break(void * UNUSED (arg))184 static int default_break(void *UNUSED(arg))
185 {
186 return G.is_break == true;
187 }
188
stats_background(void * UNUSED (arg),RenderStats * rs)189 static void stats_background(void *UNUSED(arg), RenderStats *rs)
190 {
191 uintptr_t mem_in_use, peak_memory;
192 float megs_used_memory, megs_peak_memory;
193 char info_time_str[32];
194
195 mem_in_use = MEM_get_memory_in_use();
196 peak_memory = MEM_get_peak_memory();
197
198 megs_used_memory = (mem_in_use) / (1024.0 * 1024.0);
199 megs_peak_memory = (peak_memory) / (1024.0 * 1024.0);
200
201 fprintf(stdout,
202 TIP_("Fra:%d Mem:%.2fM (Peak %.2fM) "),
203 rs->cfra,
204 megs_used_memory,
205 megs_peak_memory);
206
207 BLI_timecode_string_from_time_simple(
208 info_time_str, sizeof(info_time_str), PIL_check_seconds_timer() - rs->starttime);
209 fprintf(stdout, TIP_("| Time:%s | "), info_time_str);
210
211 if (rs->infostr) {
212 fprintf(stdout, "%s", rs->infostr);
213 }
214 else {
215 fprintf(stdout,
216 TIP_("Sce: %s Ve:%d Fa:%d La:%d"),
217 rs->scene_name,
218 rs->totvert,
219 rs->totface,
220 rs->totlamp);
221 }
222
223 /* Flush stdout to be sure python callbacks are printing stuff after blender. */
224 fflush(stdout);
225
226 /* NOTE: using G_MAIN seems valid here???
227 * Not sure it's actually even used anyway, we could as well pass NULL? */
228 BKE_callback_exec_null(G_MAIN, BKE_CB_EVT_RENDER_STATS);
229
230 fputc('\n', stdout);
231 fflush(stdout);
232 }
233
render_print_save_message(ReportList * reports,const char * name,int ok,int err)234 static void render_print_save_message(ReportList *reports, const char *name, int ok, int err)
235 {
236 if (ok) {
237 /* no need to report, just some helpful console info */
238 printf("Saved: '%s'\n", name);
239 }
240 else {
241 /* report on error since users will want to know what failed */
242 BKE_reportf(reports, RPT_ERROR, "Render error (%s) cannot save: '%s'", strerror(err), name);
243 }
244 }
245
render_imbuf_write_stamp_test(ReportList * reports,Scene * scene,struct RenderResult * rr,ImBuf * ibuf,const char * name,const ImageFormatData * imf,bool stamp)246 static int render_imbuf_write_stamp_test(ReportList *reports,
247 Scene *scene,
248 struct RenderResult *rr,
249 ImBuf *ibuf,
250 const char *name,
251 const ImageFormatData *imf,
252 bool stamp)
253 {
254 int ok;
255
256 if (stamp) {
257 /* writes the name of the individual cameras */
258 ok = BKE_imbuf_write_stamp(scene, rr, ibuf, name, imf);
259 }
260 else {
261 ok = BKE_imbuf_write(ibuf, name, imf);
262 }
263
264 render_print_save_message(reports, name, ok, errno);
265
266 return ok;
267 }
268
RE_FreeRenderResult(RenderResult * rr)269 void RE_FreeRenderResult(RenderResult *rr)
270 {
271 render_result_free(rr);
272 }
273
RE_RenderLayerGetPass(volatile RenderLayer * rl,const char * name,const char * viewname)274 float *RE_RenderLayerGetPass(volatile RenderLayer *rl, const char *name, const char *viewname)
275 {
276 RenderPass *rpass = RE_pass_find_by_name(rl, name, viewname);
277 return rpass ? rpass->rect : NULL;
278 }
279
RE_GetRenderLayer(RenderResult * rr,const char * name)280 RenderLayer *RE_GetRenderLayer(RenderResult *rr, const char *name)
281 {
282 if (rr == NULL) {
283 return NULL;
284 }
285
286 return BLI_findstring(&rr->layers, name, offsetof(RenderLayer, name));
287 }
288
RE_HasSingleLayer(Render * re)289 bool RE_HasSingleLayer(Render *re)
290 {
291 return (re->r.scemode & R_SINGLE_LAYER);
292 }
293
RE_MultilayerConvert(void * exrhandle,const char * colorspace,bool predivide,int rectx,int recty)294 RenderResult *RE_MultilayerConvert(
295 void *exrhandle, const char *colorspace, bool predivide, int rectx, int recty)
296 {
297 return render_result_new_from_exr(exrhandle, colorspace, predivide, rectx, recty);
298 }
299
render_get_active_layer(Render * re,RenderResult * rr)300 RenderLayer *render_get_active_layer(Render *re, RenderResult *rr)
301 {
302 ViewLayer *view_layer = BLI_findlink(&re->view_layers, re->active_view_layer);
303
304 if (view_layer) {
305 RenderLayer *rl = BLI_findstring(&rr->layers, view_layer->name, offsetof(RenderLayer, name));
306
307 if (rl) {
308 return rl;
309 }
310 }
311
312 return rr->layers.first;
313 }
314
render_scene_has_layers_to_render(Scene * scene,ViewLayer * single_layer)315 static bool render_scene_has_layers_to_render(Scene *scene, ViewLayer *single_layer)
316 {
317 if (single_layer) {
318 return true;
319 }
320
321 ViewLayer *view_layer;
322 for (view_layer = scene->view_layers.first; view_layer; view_layer = view_layer->next) {
323 if (view_layer->flag & VIEW_LAYER_RENDER) {
324 return true;
325 }
326 }
327 return false;
328 }
329
330 /* *************************************************** */
331
RE_GetRender(const char * name)332 Render *RE_GetRender(const char *name)
333 {
334 Render *re;
335
336 /* search for existing renders */
337 for (re = RenderGlobal.renderlist.first; re; re = re->next) {
338 if (STREQLEN(re->name, name, RE_MAXNAME)) {
339 break;
340 }
341 }
342
343 return re;
344 }
345
346 /* if you want to know exactly what has been done */
RE_AcquireResultRead(Render * re)347 RenderResult *RE_AcquireResultRead(Render *re)
348 {
349 if (re) {
350 BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_READ);
351 return re->result;
352 }
353
354 return NULL;
355 }
356
RE_AcquireResultWrite(Render * re)357 RenderResult *RE_AcquireResultWrite(Render *re)
358 {
359 if (re) {
360 BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
361 return re->result;
362 }
363
364 return NULL;
365 }
366
RE_ClearResult(Render * re)367 void RE_ClearResult(Render *re)
368 {
369 if (re) {
370 render_result_free(re->result);
371 re->result = NULL;
372 }
373 }
374
RE_SwapResult(Render * re,RenderResult ** rr)375 void RE_SwapResult(Render *re, RenderResult **rr)
376 {
377 /* for keeping render buffers */
378 if (re) {
379 SWAP(RenderResult *, re->result, *rr);
380 }
381 }
382
RE_ReleaseResult(Render * re)383 void RE_ReleaseResult(Render *re)
384 {
385 if (re) {
386 BLI_rw_mutex_unlock(&re->resultmutex);
387 }
388 }
389
390 /* displist.c util.... */
RE_GetScene(Render * re)391 Scene *RE_GetScene(Render *re)
392 {
393 if (re) {
394 return re->scene;
395 }
396 return NULL;
397 }
398
RE_SetScene(Render * re,Scene * sce)399 void RE_SetScene(Render *re, Scene *sce)
400 {
401 if (re) {
402 re->scene = sce;
403 }
404 }
405
406 /**
407 * Same as #RE_AcquireResultImage but creating the necessary views to store the result
408 * fill provided result struct with a copy of thew views of what is done so far the
409 * #RenderResult.views #ListBase needs to be freed after with #RE_ReleaseResultImageViews
410 */
RE_AcquireResultImageViews(Render * re,RenderResult * rr)411 void RE_AcquireResultImageViews(Render *re, RenderResult *rr)
412 {
413 memset(rr, 0, sizeof(RenderResult));
414
415 if (re) {
416 BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_READ);
417
418 if (re->result) {
419 RenderLayer *rl;
420 RenderView *rv, *rview;
421
422 rr->rectx = re->result->rectx;
423 rr->recty = re->result->recty;
424
425 /* creates a temporary duplication of views */
426 render_result_views_shallowcopy(rr, re->result);
427
428 rv = rr->views.first;
429 rr->have_combined = (rv->rectf != NULL);
430
431 /* active layer */
432 rl = render_get_active_layer(re, re->result);
433
434 if (rl) {
435 if (rv->rectf == NULL) {
436 for (rview = (RenderView *)rr->views.first; rview; rview = rview->next) {
437 rview->rectf = RE_RenderLayerGetPass(rl, RE_PASSNAME_COMBINED, rview->name);
438 }
439 }
440
441 if (rv->rectz == NULL) {
442 for (rview = (RenderView *)rr->views.first; rview; rview = rview->next) {
443 rview->rectz = RE_RenderLayerGetPass(rl, RE_PASSNAME_Z, rview->name);
444 }
445 }
446 }
447
448 rr->layers = re->result->layers;
449 rr->xof = re->disprect.xmin;
450 rr->yof = re->disprect.ymin;
451 rr->stamp_data = re->result->stamp_data;
452 }
453 }
454 }
455
456 /* clear temporary renderresult struct */
RE_ReleaseResultImageViews(Render * re,RenderResult * rr)457 void RE_ReleaseResultImageViews(Render *re, RenderResult *rr)
458 {
459 if (re) {
460 if (rr) {
461 render_result_views_shallowdelete(rr);
462 }
463 BLI_rw_mutex_unlock(&re->resultmutex);
464 }
465 }
466
467 /* fill provided result struct with what's currently active or done */
468 /* this RenderResult struct is the only exception to the rule of a RenderResult */
469 /* always having at least one RenderView */
RE_AcquireResultImage(Render * re,RenderResult * rr,const int view_id)470 void RE_AcquireResultImage(Render *re, RenderResult *rr, const int view_id)
471 {
472 memset(rr, 0, sizeof(RenderResult));
473
474 if (re) {
475 BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_READ);
476
477 if (re->result) {
478 RenderLayer *rl;
479 RenderView *rv;
480
481 rr->rectx = re->result->rectx;
482 rr->recty = re->result->recty;
483
484 /* actview view */
485 rv = RE_RenderViewGetById(re->result, view_id);
486 rr->have_combined = (rv->rectf != NULL);
487
488 rr->rectf = rv->rectf;
489 rr->rectz = rv->rectz;
490 rr->rect32 = rv->rect32;
491
492 /* active layer */
493 rl = render_get_active_layer(re, re->result);
494
495 if (rl) {
496 if (rv->rectf == NULL) {
497 rr->rectf = RE_RenderLayerGetPass(rl, RE_PASSNAME_COMBINED, rv->name);
498 }
499
500 if (rv->rectz == NULL) {
501 rr->rectz = RE_RenderLayerGetPass(rl, RE_PASSNAME_Z, rv->name);
502 }
503 }
504
505 rr->layers = re->result->layers;
506 rr->views = re->result->views;
507
508 rr->xof = re->disprect.xmin;
509 rr->yof = re->disprect.ymin;
510
511 rr->stamp_data = re->result->stamp_data;
512 }
513 }
514 }
515
RE_ReleaseResultImage(Render * re)516 void RE_ReleaseResultImage(Render *re)
517 {
518 if (re) {
519 BLI_rw_mutex_unlock(&re->resultmutex);
520 }
521 }
522
523 /* caller is responsible for allocating rect in correct size! */
RE_ResultGet32(Render * re,unsigned int * rect)524 void RE_ResultGet32(Render *re, unsigned int *rect)
525 {
526 RenderResult rres;
527 const int view_id = BKE_scene_multiview_view_id_get(&re->r, re->viewname);
528
529 RE_AcquireResultImageViews(re, &rres);
530 render_result_rect_get_pixels(&rres,
531 rect,
532 re->rectx,
533 re->recty,
534 &re->scene->view_settings,
535 &re->scene->display_settings,
536 view_id);
537 RE_ReleaseResultImageViews(re, &rres);
538 }
539
540 /* caller is responsible for allocating rect in correct size! */
541 /* Only for acquired results, for lock */
RE_AcquiredResultGet32(Render * re,RenderResult * result,unsigned int * rect,const int view_id)542 void RE_AcquiredResultGet32(Render *re,
543 RenderResult *result,
544 unsigned int *rect,
545 const int view_id)
546 {
547 render_result_rect_get_pixels(result,
548 rect,
549 re->rectx,
550 re->recty,
551 &re->scene->view_settings,
552 &re->scene->display_settings,
553 view_id);
554 }
555
RE_GetStats(Render * re)556 RenderStats *RE_GetStats(Render *re)
557 {
558 return &re->i;
559 }
560
RE_NewRender(const char * name)561 Render *RE_NewRender(const char *name)
562 {
563 Render *re;
564
565 /* only one render per name exists */
566 re = RE_GetRender(name);
567 if (re == NULL) {
568
569 /* new render data struct */
570 re = MEM_callocN(sizeof(Render), "new render");
571 BLI_addtail(&RenderGlobal.renderlist, re);
572 BLI_strncpy(re->name, name, RE_MAXNAME);
573 BLI_rw_mutex_init(&re->resultmutex);
574 BLI_rw_mutex_init(&re->partsmutex);
575 }
576
577 RE_InitRenderCB(re);
578
579 return re;
580 }
581
582 /* MAX_ID_NAME + sizeof(Library->name) + space + null-terminator. */
583 #define MAX_SCENE_RENDER_NAME (MAX_ID_NAME + 1024 + 2)
584
scene_render_name_get(const Scene * scene,const size_t max_size,char * render_name)585 static void scene_render_name_get(const Scene *scene, const size_t max_size, char *render_name)
586 {
587 if (ID_IS_LINKED(scene)) {
588 BLI_snprintf(render_name, max_size, "%s %s", scene->id.lib->id.name, scene->id.name);
589 }
590 else {
591 BLI_snprintf(render_name, max_size, "%s", scene->id.name);
592 }
593 }
594
RE_GetSceneRender(const Scene * scene)595 Render *RE_GetSceneRender(const Scene *scene)
596 {
597 char render_name[MAX_SCENE_RENDER_NAME];
598 scene_render_name_get(scene, sizeof(render_name), render_name);
599 return RE_GetRender(render_name);
600 }
601
RE_NewSceneRender(const Scene * scene)602 Render *RE_NewSceneRender(const Scene *scene)
603 {
604 char render_name[MAX_SCENE_RENDER_NAME];
605 scene_render_name_get(scene, sizeof(render_name), render_name);
606 return RE_NewRender(render_name);
607 }
608
609 /* called for new renders and when finishing rendering so
610 * we always have valid callbacks on a render */
RE_InitRenderCB(Render * re)611 void RE_InitRenderCB(Render *re)
612 {
613 /* set default empty callbacks */
614 re->display_init = result_nothing;
615 re->display_clear = result_nothing;
616 re->display_update = result_rcti_nothing;
617 re->current_scene_update = current_scene_nothing;
618 re->progress = float_nothing;
619 re->test_break = default_break;
620 if (G.background) {
621 re->stats_draw = stats_background;
622 }
623 else {
624 re->stats_draw = stats_nothing;
625 }
626 /* clear callback handles */
627 re->dih = re->dch = re->duh = re->sdh = re->prh = re->tbh = NULL;
628 }
629
630 /* only call this while you know it will remove the link too */
RE_FreeRender(Render * re)631 void RE_FreeRender(Render *re)
632 {
633 if (re->engine) {
634 RE_engine_free(re->engine);
635 }
636
637 BLI_rw_mutex_end(&re->resultmutex);
638 BLI_rw_mutex_end(&re->partsmutex);
639
640 BLI_freelistN(&re->view_layers);
641 BLI_freelistN(&re->r.views);
642
643 BKE_curvemapping_free_data(&re->r.mblur_shutter_curve);
644
645 /* main dbase can already be invalid now, some database-free code checks it */
646 re->main = NULL;
647 re->scene = NULL;
648
649 render_result_free(re->result);
650 render_result_free(re->pushedresult);
651
652 BLI_remlink(&RenderGlobal.renderlist, re);
653 MEM_freeN(re);
654 }
655
656 /* exit blender */
RE_FreeAllRender(void)657 void RE_FreeAllRender(void)
658 {
659 while (RenderGlobal.renderlist.first) {
660 RE_FreeRender(RenderGlobal.renderlist.first);
661 }
662
663 #ifdef WITH_FREESTYLE
664 /* finalize Freestyle */
665 FRS_exit();
666 #endif
667 }
668
RE_FreeAllPersistentData(void)669 void RE_FreeAllPersistentData(void)
670 {
671 Render *re;
672 for (re = RenderGlobal.renderlist.first; re != NULL; re = re->next) {
673 if ((re->r.mode & R_PERSISTENT_DATA) != 0 && re->engine != NULL) {
674 RE_engine_free(re->engine);
675 re->engine = NULL;
676 }
677 }
678 }
679
680 /* on file load, free all re */
RE_FreeAllRenderResults(void)681 void RE_FreeAllRenderResults(void)
682 {
683 Render *re;
684
685 for (re = RenderGlobal.renderlist.first; re; re = re->next) {
686 render_result_free(re->result);
687 render_result_free(re->pushedresult);
688
689 re->result = NULL;
690 re->pushedresult = NULL;
691 }
692 }
693
RE_FreePersistentData(void)694 void RE_FreePersistentData(void)
695 {
696 Render *re;
697
698 /* render engines can be kept around for quick re-render, this clears all */
699 for (re = RenderGlobal.renderlist.first; re; re = re->next) {
700 if (re->engine) {
701 /* if engine is currently rendering, just tag it to be freed when render is finished */
702 if (!(re->engine->flag & RE_ENGINE_RENDERING)) {
703 RE_engine_free(re->engine);
704 }
705
706 re->engine = NULL;
707 }
708 }
709 }
710
711 /* ********* initialize state ******** */
712
713 /* clear full sample and tile flags if needed */
check_mode_full_sample(RenderData * rd)714 static int check_mode_full_sample(RenderData *rd)
715 {
716 int scemode = rd->scemode;
717
718 /* not supported by any current renderer */
719 scemode &= ~R_FULL_SAMPLE;
720
721 #ifdef WITH_OPENEXR
722 if (scemode & R_FULL_SAMPLE) {
723 scemode |= R_EXR_TILE_FILE; /* enable automatic */
724 }
725 #else
726 /* can't do this without openexr support */
727 scemode &= ~(R_EXR_TILE_FILE | R_FULL_SAMPLE);
728 #endif
729
730 return scemode;
731 }
732
re_init_resolution(Render * re,Render * source,int winx,int winy,rcti * disprect)733 static void re_init_resolution(Render *re, Render *source, int winx, int winy, rcti *disprect)
734 {
735 re->winx = winx;
736 re->winy = winy;
737 if (source && (source->r.mode & R_BORDER)) {
738 /* eeh, doesn't seem original bordered disprect is storing anywhere
739 * after insertion on black happening in do_render(),
740 * so for now simply re-calculate disprect using border from source
741 * renderer (sergey)
742 */
743
744 re->disprect.xmin = source->r.border.xmin * winx;
745 re->disprect.xmax = source->r.border.xmax * winx;
746
747 re->disprect.ymin = source->r.border.ymin * winy;
748 re->disprect.ymax = source->r.border.ymax * winy;
749
750 re->rectx = BLI_rcti_size_x(&re->disprect);
751 re->recty = BLI_rcti_size_y(&re->disprect);
752
753 /* copy border itself, since it could be used by external engines */
754 re->r.border = source->r.border;
755 }
756 else if (disprect) {
757 re->disprect = *disprect;
758 re->rectx = BLI_rcti_size_x(&re->disprect);
759 re->recty = BLI_rcti_size_y(&re->disprect);
760 }
761 else {
762 re->disprect.xmin = re->disprect.ymin = 0;
763 re->disprect.xmax = winx;
764 re->disprect.ymax = winy;
765 re->rectx = winx;
766 re->recty = winy;
767 }
768 }
769
render_copy_renderdata(RenderData * to,RenderData * from)770 void render_copy_renderdata(RenderData *to, RenderData *from)
771 {
772 BLI_freelistN(&to->views);
773 BKE_curvemapping_free_data(&to->mblur_shutter_curve);
774
775 *to = *from;
776
777 BLI_duplicatelist(&to->views, &from->views);
778 BKE_curvemapping_copy_data(&to->mblur_shutter_curve, &from->mblur_shutter_curve);
779 }
780
781 /* what doesn't change during entire render sequence */
782 /* disprect is optional, if NULL it assumes full window render */
RE_InitState(Render * re,Render * source,RenderData * rd,ListBase * render_layers,ViewLayer * single_layer,int winx,int winy,rcti * disprect)783 void RE_InitState(Render *re,
784 Render *source,
785 RenderData *rd,
786 ListBase *render_layers,
787 ViewLayer *single_layer,
788 int winx,
789 int winy,
790 rcti *disprect)
791 {
792 bool had_freestyle = (re->r.mode & R_EDGE_FRS) != 0;
793
794 re->ok = true; /* maybe flag */
795
796 re->i.starttime = PIL_check_seconds_timer();
797
798 /* copy render data and render layers for thread safety */
799 render_copy_renderdata(&re->r, rd);
800 BLI_freelistN(&re->view_layers);
801 BLI_duplicatelist(&re->view_layers, render_layers);
802 re->active_view_layer = 0;
803
804 if (source) {
805 /* reuse border flags from source renderer */
806 re->r.mode &= ~(R_BORDER | R_CROP);
807 re->r.mode |= source->r.mode & (R_BORDER | R_CROP);
808
809 /* dimensions shall be shared between all renderers */
810 re->r.xsch = source->r.xsch;
811 re->r.ysch = source->r.ysch;
812 re->r.size = source->r.size;
813 }
814
815 re_init_resolution(re, source, winx, winy, disprect);
816
817 /* disable border if it's a full render anyway */
818 if (re->r.border.xmin == 0.0f && re->r.border.xmax == 1.0f && re->r.border.ymin == 0.0f &&
819 re->r.border.ymax == 1.0f) {
820 re->r.mode &= ~R_BORDER;
821 }
822
823 if (re->rectx < 1 || re->recty < 1 ||
824 (BKE_imtype_is_movie(rd->im_format.imtype) && (re->rectx < 16 || re->recty < 16))) {
825 BKE_report(re->reports, RPT_ERROR, "Image too small");
826 re->ok = 0;
827 return;
828 }
829
830 re->r.scemode = check_mode_full_sample(&re->r);
831
832 if (single_layer) {
833 int index = BLI_findindex(render_layers, single_layer);
834 if (index != -1) {
835 re->active_view_layer = index;
836 re->r.scemode |= R_SINGLE_LAYER;
837 }
838 }
839
840 /* if preview render, we try to keep old result */
841 BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
842
843 if (re->r.scemode & R_BUTS_PREVIEW) {
844 if (had_freestyle || (re->r.mode & R_EDGE_FRS)) {
845 /* freestyle manipulates render layers so always have to free */
846 render_result_free(re->result);
847 re->result = NULL;
848 }
849 else if (re->result) {
850 ViewLayer *active_render_layer = BLI_findlink(&re->view_layers, re->active_view_layer);
851 RenderLayer *rl;
852 bool have_layer = false;
853
854 for (rl = re->result->layers.first; rl; rl = rl->next) {
855 if (STREQ(rl->name, active_render_layer->name)) {
856 have_layer = true;
857 }
858 }
859
860 if (re->result->rectx == re->rectx && re->result->recty == re->recty && have_layer) {
861 /* keep render result, this avoids flickering black tiles
862 * when the preview changes */
863 }
864 else {
865 /* free because resolution changed */
866 render_result_free(re->result);
867 re->result = NULL;
868 }
869 }
870 }
871 else {
872
873 /* make empty render result, so display callbacks can initialize */
874 render_result_free(re->result);
875 re->result = MEM_callocN(sizeof(RenderResult), "new render result");
876 re->result->rectx = re->rectx;
877 re->result->recty = re->recty;
878 render_result_view_new(re->result, "");
879 }
880
881 /* ensure renderdatabase can use part settings correct */
882 RE_parts_clamp(re);
883
884 BLI_rw_mutex_unlock(&re->resultmutex);
885
886 RE_init_threadcount(re);
887
888 RE_point_density_fix_linking();
889 }
890
891 /* This function is only called by view3d rendering, which doesn't support
892 * multiview at the moment. so handle only one view here */
render_result_rescale(Render * re)893 static void render_result_rescale(Render *re)
894 {
895 RenderResult *result = re->result;
896 RenderView *rv;
897 int x, y;
898 float scale_x, scale_y;
899 float *src_rectf;
900
901 rv = RE_RenderViewGetById(result, 0);
902 src_rectf = rv->rectf;
903
904 if (src_rectf == NULL) {
905 RenderLayer *rl = render_get_active_layer(re, re->result);
906 if (rl != NULL) {
907 src_rectf = RE_RenderLayerGetPass(rl, RE_PASSNAME_COMBINED, NULL);
908 }
909 }
910
911 if (src_rectf != NULL) {
912 float *dst_rectf = NULL;
913 re->result = render_result_new(re, &re->disprect, 0, RR_USE_MEM, RR_ALL_LAYERS, "");
914
915 if (re->result != NULL) {
916 dst_rectf = RE_RenderViewGetById(re->result, 0)->rectf;
917 if (dst_rectf == NULL) {
918 RenderLayer *rl;
919 rl = render_get_active_layer(re, re->result);
920 if (rl != NULL) {
921 dst_rectf = RE_RenderLayerGetPass(rl, RE_PASSNAME_COMBINED, NULL);
922 }
923 }
924
925 scale_x = (float)result->rectx / re->result->rectx;
926 scale_y = (float)result->recty / re->result->recty;
927 for (x = 0; x < re->result->rectx; x++) {
928 for (y = 0; y < re->result->recty; y++) {
929 int src_x = x * scale_x;
930 int src_y = y * scale_y;
931 int dst_index = y * re->result->rectx + x;
932 int src_index = src_y * result->rectx + src_x;
933 copy_v4_v4(dst_rectf + dst_index * 4, src_rectf + src_index * 4);
934 }
935 }
936 }
937 render_result_free(result);
938 }
939 }
940
RE_ChangeResolution(Render * re,int winx,int winy,rcti * disprect)941 void RE_ChangeResolution(Render *re, int winx, int winy, rcti *disprect)
942 {
943 re_init_resolution(re, NULL, winx, winy, disprect);
944 RE_parts_clamp(re);
945
946 if (re->result) {
947 BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
948 render_result_rescale(re);
949 BLI_rw_mutex_unlock(&re->resultmutex);
950 }
951 }
952
953 /* TODO(sergey): This is a bit hackish, used to temporary disable freestyle when
954 * doing viewport render. Needs some better integration of BI viewport rendering
955 * into the pipeline.
956 */
RE_ChangeModeFlag(Render * re,int flag,bool clear)957 void RE_ChangeModeFlag(Render *re, int flag, bool clear)
958 {
959 if (clear) {
960 re->r.mode &= ~flag;
961 }
962 else {
963 re->r.mode |= flag;
964 }
965 }
966
967 /* update some variables that can be animated, and otherwise wouldn't be due to
968 * RenderData getting copied once at the start of animation render */
render_update_anim_renderdata(Render * re,RenderData * rd,ListBase * render_layers)969 void render_update_anim_renderdata(Render *re, RenderData *rd, ListBase *render_layers)
970 {
971 /* filter */
972 re->r.gauss = rd->gauss;
973
974 /* motion blur */
975 re->r.blurfac = rd->blurfac;
976
977 /* freestyle */
978 re->r.line_thickness_mode = rd->line_thickness_mode;
979 re->r.unit_line_thickness = rd->unit_line_thickness;
980
981 /* render layers */
982 BLI_freelistN(&re->view_layers);
983 BLI_duplicatelist(&re->view_layers, render_layers);
984
985 /* render views */
986 BLI_freelistN(&re->r.views);
987 BLI_duplicatelist(&re->r.views, &rd->views);
988 }
989
RE_SetWindow(Render * re,const rctf * viewplane,float clip_start,float clip_end)990 void RE_SetWindow(Render *re, const rctf *viewplane, float clip_start, float clip_end)
991 {
992 /* re->ok flag? */
993
994 re->viewplane = *viewplane;
995 re->clip_start = clip_start;
996 re->clip_end = clip_end;
997
998 perspective_m4(re->winmat,
999 re->viewplane.xmin,
1000 re->viewplane.xmax,
1001 re->viewplane.ymin,
1002 re->viewplane.ymax,
1003 re->clip_start,
1004 re->clip_end);
1005 }
1006
RE_SetOrtho(Render * re,const rctf * viewplane,float clip_start,float clip_end)1007 void RE_SetOrtho(Render *re, const rctf *viewplane, float clip_start, float clip_end)
1008 {
1009 /* re->ok flag? */
1010
1011 re->viewplane = *viewplane;
1012 re->clip_start = clip_start;
1013 re->clip_end = clip_end;
1014
1015 orthographic_m4(re->winmat,
1016 re->viewplane.xmin,
1017 re->viewplane.xmax,
1018 re->viewplane.ymin,
1019 re->viewplane.ymax,
1020 re->clip_start,
1021 re->clip_end);
1022 }
1023
RE_GetViewPlane(Render * re,rctf * r_viewplane,rcti * r_disprect)1024 void RE_GetViewPlane(Render *re, rctf *r_viewplane, rcti *r_disprect)
1025 {
1026 *r_viewplane = re->viewplane;
1027
1028 /* make disprect zero when no border render, is needed to detect changes in 3d view render */
1029 if (re->r.mode & R_BORDER) {
1030 *r_disprect = re->disprect;
1031 }
1032 else {
1033 BLI_rcti_init(r_disprect, 0, 0, 0, 0);
1034 }
1035 }
1036
1037 /* image and movie output has to move to either imbuf or kernel */
RE_display_init_cb(Render * re,void * handle,void (* f)(void * handle,RenderResult * rr))1038 void RE_display_init_cb(Render *re, void *handle, void (*f)(void *handle, RenderResult *rr))
1039 {
1040 re->display_init = f;
1041 re->dih = handle;
1042 }
RE_display_clear_cb(Render * re,void * handle,void (* f)(void * handle,RenderResult * rr))1043 void RE_display_clear_cb(Render *re, void *handle, void (*f)(void *handle, RenderResult *rr))
1044 {
1045 re->display_clear = f;
1046 re->dch = handle;
1047 }
RE_display_update_cb(Render * re,void * handle,void (* f)(void * handle,RenderResult * rr,volatile rcti * rect))1048 void RE_display_update_cb(Render *re,
1049 void *handle,
1050 void (*f)(void *handle, RenderResult *rr, volatile rcti *rect))
1051 {
1052 re->display_update = f;
1053 re->duh = handle;
1054 }
RE_current_scene_update_cb(Render * re,void * handle,void (* f)(void * handle,Scene * scene))1055 void RE_current_scene_update_cb(Render *re, void *handle, void (*f)(void *handle, Scene *scene))
1056 {
1057 re->current_scene_update = f;
1058 re->suh = handle;
1059 }
RE_stats_draw_cb(Render * re,void * handle,void (* f)(void * handle,RenderStats * rs))1060 void RE_stats_draw_cb(Render *re, void *handle, void (*f)(void *handle, RenderStats *rs))
1061 {
1062 re->stats_draw = f;
1063 re->sdh = handle;
1064 }
RE_progress_cb(Render * re,void * handle,void (* f)(void * handle,float))1065 void RE_progress_cb(Render *re, void *handle, void (*f)(void *handle, float))
1066 {
1067 re->progress = f;
1068 re->prh = handle;
1069 }
1070
RE_draw_lock_cb(Render * re,void * handle,void (* f)(void * handle,int i))1071 void RE_draw_lock_cb(Render *re, void *handle, void (*f)(void *handle, int i))
1072 {
1073 re->draw_lock = f;
1074 re->dlh = handle;
1075 }
1076
RE_test_break_cb(Render * re,void * handle,int (* f)(void * handle))1077 void RE_test_break_cb(Render *re, void *handle, int (*f)(void *handle))
1078 {
1079 re->test_break = f;
1080 re->tbh = handle;
1081 }
1082
1083 /* ********* GL Context ******** */
1084
RE_gl_context_create(Render * re)1085 void RE_gl_context_create(Render *re)
1086 {
1087 /* Needs to be created in the main ogl thread. */
1088 re->gl_context = WM_opengl_context_create();
1089 /* So we activate the window's one afterwards. */
1090 wm_window_reset_drawable();
1091 }
1092
RE_gl_context_destroy(Render * re)1093 void RE_gl_context_destroy(Render *re)
1094 {
1095 /* Needs to be called from the thread which used the ogl context for rendering. */
1096 if (re->gl_context) {
1097 if (re->gpu_context) {
1098 WM_opengl_context_activate(re->gl_context);
1099 GPU_context_active_set(re->gpu_context);
1100 GPU_context_discard(re->gpu_context);
1101 re->gpu_context = NULL;
1102 }
1103
1104 WM_opengl_context_dispose(re->gl_context);
1105 re->gl_context = NULL;
1106 }
1107 }
1108
RE_gl_context_get(Render * re)1109 void *RE_gl_context_get(Render *re)
1110 {
1111 return re->gl_context;
1112 }
1113
RE_gpu_context_get(Render * re)1114 void *RE_gpu_context_get(Render *re)
1115 {
1116 if (re->gpu_context == NULL) {
1117 re->gpu_context = GPU_context_create(NULL);
1118 }
1119 return re->gpu_context;
1120 }
1121
1122 /* ********* add object data (later) ******** */
1123
1124 /* object is considered fully prepared on correct time etc */
1125 /* includes lights */
1126 #if 0
1127 void RE_AddObject(Render *UNUSED(re), Object *UNUSED(ob))
1128 {
1129 }
1130 #endif
1131
1132 /* ************ This part uses API, for rendering Blender scenes ********** */
1133
do_render_3d(Render * re)1134 static void do_render_3d(Render *re)
1135 {
1136 re->current_scene_update(re->suh, re->scene);
1137 RE_engine_render(re, 0);
1138 }
1139
1140 /* make sure disprect is not affected by the render border */
render_result_disprect_to_full_resolution(Render * re)1141 static void render_result_disprect_to_full_resolution(Render *re)
1142 {
1143 re->disprect.xmin = re->disprect.ymin = 0;
1144 re->disprect.xmax = re->winx;
1145 re->disprect.ymax = re->winy;
1146 re->rectx = re->winx;
1147 re->recty = re->winy;
1148 }
1149
render_result_uncrop(Render * re)1150 static void render_result_uncrop(Render *re)
1151 {
1152 /* when using border render with crop disabled, insert render result into
1153 * full size with black pixels outside */
1154 if (re->result && (re->r.mode & R_BORDER)) {
1155 if ((re->r.mode & R_CROP) == 0) {
1156 RenderResult *rres;
1157
1158 /* backup */
1159 const rcti orig_disprect = re->disprect;
1160 const int orig_rectx = re->rectx, orig_recty = re->recty;
1161
1162 BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
1163
1164 /* sub-rect for merge call later on */
1165 re->result->tilerect = re->disprect;
1166
1167 /* weak is: it chances disprect from border */
1168 render_result_disprect_to_full_resolution(re);
1169
1170 rres = render_result_new(re, &re->disprect, 0, RR_USE_MEM, RR_ALL_LAYERS, RR_ALL_VIEWS);
1171 rres->stamp_data = BKE_stamp_data_copy(re->result->stamp_data);
1172
1173 render_result_clone_passes(re, rres, NULL);
1174
1175 render_result_merge(rres, re->result);
1176 render_result_free(re->result);
1177 re->result = rres;
1178
1179 /* weak... the display callback wants an active renderlayer pointer... */
1180 re->result->renlay = render_get_active_layer(re, re->result);
1181
1182 BLI_rw_mutex_unlock(&re->resultmutex);
1183
1184 re->display_init(re->dih, re->result);
1185 re->display_update(re->duh, re->result, NULL);
1186
1187 /* restore the disprect from border */
1188 re->disprect = orig_disprect;
1189 re->rectx = orig_rectx;
1190 re->recty = orig_recty;
1191 }
1192 else {
1193 /* set offset (again) for use in compositor, disprect was manipulated. */
1194 re->result->xof = 0;
1195 re->result->yof = 0;
1196 }
1197 }
1198 }
1199
1200 /* main render routine, no compositing */
do_render(Render * re)1201 static void do_render(Render *re)
1202 {
1203 Object *camera = RE_GetCamera(re);
1204 /* also check for camera here */
1205 if (camera == NULL) {
1206 BKE_report(re->reports, RPT_ERROR, "Cannot render, no camera");
1207 G.is_break = true;
1208 return;
1209 }
1210
1211 /* now use renderdata and camera to set viewplane */
1212 RE_SetCamera(re, camera);
1213
1214 do_render_3d(re);
1215
1216 /* when border render, check if we have to insert it in black */
1217 render_result_uncrop(re);
1218 }
1219
1220 /* within context of current Render *re, render another scene.
1221 * it uses current render image size and disprect, but doesn't execute composite
1222 */
render_scene(Render * re,Scene * sce,int cfra)1223 static void render_scene(Render *re, Scene *sce, int cfra)
1224 {
1225 Render *resc = RE_NewSceneRender(sce);
1226 int winx = re->winx, winy = re->winy;
1227
1228 sce->r.cfra = cfra;
1229
1230 BKE_scene_camera_switch_update(sce);
1231
1232 /* exception: scene uses own size (unfinished code) */
1233 if (0) {
1234 winx = (sce->r.size * sce->r.xsch) / 100;
1235 winy = (sce->r.size * sce->r.ysch) / 100;
1236 }
1237
1238 /* initial setup */
1239 RE_InitState(resc, re, &sce->r, &sce->view_layers, NULL, winx, winy, &re->disprect);
1240
1241 /* We still want to use 'rendercache' setting from org (main) scene... */
1242 resc->r.scemode = (resc->r.scemode & ~R_EXR_CACHE_FILE) | (re->r.scemode & R_EXR_CACHE_FILE);
1243
1244 /* still unsure entity this... */
1245 resc->main = re->main;
1246 resc->scene = sce;
1247
1248 /* copy callbacks */
1249 resc->display_update = re->display_update;
1250 resc->duh = re->duh;
1251 resc->test_break = re->test_break;
1252 resc->tbh = re->tbh;
1253 resc->stats_draw = re->stats_draw;
1254 resc->sdh = re->sdh;
1255 resc->current_scene_update = re->current_scene_update;
1256 resc->suh = re->suh;
1257
1258 do_render(resc);
1259 }
1260
1261 /* helper call to detect if this scene needs a render,
1262 * or if there's a any render layer to render. */
composite_needs_render(Scene * sce,int this_scene)1263 static int composite_needs_render(Scene *sce, int this_scene)
1264 {
1265 bNodeTree *ntree = sce->nodetree;
1266 bNode *node;
1267
1268 if (ntree == NULL) {
1269 return 1;
1270 }
1271 if (sce->use_nodes == false) {
1272 return 1;
1273 }
1274 if ((sce->r.scemode & R_DOCOMP) == 0) {
1275 return 1;
1276 }
1277
1278 for (node = ntree->nodes.first; node; node = node->next) {
1279 if (node->type == CMP_NODE_R_LAYERS && (node->flag & NODE_MUTED) == 0) {
1280 if (this_scene == 0 || node->id == NULL || node->id == &sce->id) {
1281 return 1;
1282 }
1283 }
1284 }
1285 return 0;
1286 }
1287
RE_allow_render_generic_object(Object * ob)1288 bool RE_allow_render_generic_object(Object *ob)
1289 {
1290 /* override not showing object when duplis are used with particles */
1291 if (ob->transflag & OB_DUPLIPARTS) {
1292 /* pass */ /* let particle system(s) handle showing vs. not showing */
1293 }
1294 else if (ob->transflag & OB_DUPLI) {
1295 return false;
1296 }
1297 return true;
1298 }
1299
ntree_render_scenes(Render * re)1300 static void ntree_render_scenes(Render *re)
1301 {
1302 bNode *node;
1303 int cfra = re->scene->r.cfra;
1304 Scene *restore_scene = re->scene;
1305
1306 if (re->scene->nodetree == NULL) {
1307 return;
1308 }
1309
1310 /* now foreach render-result node we do a full render */
1311 /* results are stored in a way compositor will find it */
1312 GSet *scenes_rendered = BLI_gset_ptr_new(__func__);
1313 for (node = re->scene->nodetree->nodes.first; node; node = node->next) {
1314 if (node->type == CMP_NODE_R_LAYERS && (node->flag & NODE_MUTED) == 0) {
1315 if (node->id && node->id != (ID *)re->scene) {
1316 Scene *scene = (Scene *)node->id;
1317 if (!BLI_gset_haskey(scenes_rendered, scene) &&
1318 render_scene_has_layers_to_render(scene, false)) {
1319 render_scene(re, scene, cfra);
1320 BLI_gset_add(scenes_rendered, scene);
1321 nodeUpdate(restore_scene->nodetree, node);
1322 }
1323 }
1324 }
1325 }
1326 BLI_gset_free(scenes_rendered, NULL);
1327 }
1328
1329 /* bad call... need to think over proper method still */
render_composit_stats(void * arg,const char * str)1330 static void render_composit_stats(void *arg, const char *str)
1331 {
1332 Render *re = (Render *)arg;
1333
1334 RenderStats i;
1335 memcpy(&i, &re->i, sizeof(i));
1336 i.infostr = str;
1337 re->stats_draw(re->sdh, &i);
1338 }
1339
1340 /* returns fully composited render-result on given time step (in RenderData) */
do_render_composite(Render * re)1341 static void do_render_composite(Render *re)
1342 {
1343 bNodeTree *ntree = re->pipeline_scene_eval->nodetree;
1344 int update_newframe = 0;
1345
1346 if (composite_needs_render(re->pipeline_scene_eval, 1)) {
1347 /* save memory... free all cached images */
1348 ntreeFreeCache(ntree);
1349
1350 /* render the frames
1351 * it could be optimized to render only the needed view
1352 * but what if a scene has a different number of views
1353 * than the main scene? */
1354 do_render(re);
1355 }
1356 else {
1357 re->i.cfra = re->r.cfra;
1358
1359 /* ensure new result gets added, like for regular renders */
1360 BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
1361
1362 render_result_free(re->result);
1363 if ((re->r.mode & R_CROP) == 0) {
1364 render_result_disprect_to_full_resolution(re);
1365 }
1366 re->result = render_result_new(re, &re->disprect, 0, RR_USE_MEM, RR_ALL_LAYERS, RR_ALL_VIEWS);
1367
1368 BLI_rw_mutex_unlock(&re->resultmutex);
1369
1370 /* scene render process already updates animsys */
1371 update_newframe = 1;
1372 }
1373
1374 /* swap render result */
1375 if (re->r.scemode & R_SINGLE_LAYER) {
1376 BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
1377 render_result_single_layer_end(re);
1378 BLI_rw_mutex_unlock(&re->resultmutex);
1379 }
1380
1381 if (!re->test_break(re->tbh)) {
1382
1383 if (ntree) {
1384 ntreeCompositTagRender(re->pipeline_scene_eval);
1385 }
1386
1387 if (ntree && re->scene->use_nodes && re->r.scemode & R_DOCOMP) {
1388 /* checks if there are render-result nodes that need scene */
1389 if ((re->r.scemode & R_SINGLE_LAYER) == 0) {
1390 ntree_render_scenes(re);
1391 }
1392
1393 if (!re->test_break(re->tbh)) {
1394 ntree->stats_draw = render_composit_stats;
1395 ntree->test_break = re->test_break;
1396 ntree->progress = re->progress;
1397 ntree->sdh = re;
1398 ntree->tbh = re->tbh;
1399 ntree->prh = re->prh;
1400
1401 if (update_newframe) {
1402 /* If we have consistent depsgraph now would be a time to update them. */
1403 }
1404
1405 RenderView *rv;
1406 for (rv = re->result->views.first; rv; rv = rv->next) {
1407 ntreeCompositExecTree(re->pipeline_scene_eval,
1408 ntree,
1409 &re->r,
1410 true,
1411 G.background == 0,
1412 &re->scene->view_settings,
1413 &re->scene->display_settings,
1414 rv->name);
1415 }
1416
1417 ntree->stats_draw = NULL;
1418 ntree->test_break = NULL;
1419 ntree->progress = NULL;
1420 ntree->tbh = ntree->sdh = ntree->prh = NULL;
1421 }
1422 }
1423 }
1424
1425 /* weak... the display callback wants an active renderlayer pointer... */
1426 if (re->result != NULL) {
1427 re->result->renlay = render_get_active_layer(re, re->result);
1428 re->display_update(re->duh, re->result, NULL);
1429 }
1430 }
1431
renderresult_stampinfo(Render * re)1432 static void renderresult_stampinfo(Render *re)
1433 {
1434 RenderResult rres;
1435 RenderView *rv;
1436 int nr;
1437
1438 /* this is the basic trick to get the displayed float or char rect from render result */
1439 nr = 0;
1440 for (rv = re->result->views.first; rv; rv = rv->next, nr++) {
1441 RE_SetActiveRenderView(re, rv->name);
1442 RE_AcquireResultImage(re, &rres, nr);
1443
1444 Object *ob_camera_eval = DEG_get_evaluated_object(re->pipeline_depsgraph, RE_GetCamera(re));
1445 BKE_image_stamp_buf(re->scene,
1446 ob_camera_eval,
1447 (re->r.stamp & R_STAMP_STRIPMETA) ? rres.stamp_data : NULL,
1448 (unsigned char *)rres.rect32,
1449 rres.rectf,
1450 rres.rectx,
1451 rres.recty,
1452 4);
1453 RE_ReleaseResultImage(re);
1454 }
1455 }
1456
RE_seq_render_active(Scene * scene,RenderData * rd)1457 int RE_seq_render_active(Scene *scene, RenderData *rd)
1458 {
1459 Editing *ed;
1460 Sequence *seq;
1461
1462 ed = scene->ed;
1463
1464 if (!(rd->scemode & R_DOSEQ) || !ed || !ed->seqbase.first) {
1465 return 0;
1466 }
1467
1468 for (seq = ed->seqbase.first; seq; seq = seq->next) {
1469 if (seq->type != SEQ_TYPE_SOUND_RAM) {
1470 return 1;
1471 }
1472 }
1473
1474 return 0;
1475 }
1476
do_render_seq(Render * re)1477 static void do_render_seq(Render *re)
1478 {
1479 static int recurs_depth = 0;
1480 struct ImBuf *out;
1481 RenderResult *rr; /* don't assign re->result here as it might change during give_ibuf_seq */
1482 int cfra = re->r.cfra;
1483 SeqRenderData context;
1484 int view_id, tot_views;
1485 struct ImBuf **ibuf_arr;
1486 int re_x, re_y;
1487
1488 re->i.cfra = cfra;
1489
1490 recurs_depth++;
1491
1492 if ((re->r.mode & R_BORDER) && (re->r.mode & R_CROP) == 0) {
1493 /* if border rendering is used and cropping is disabled, final buffer should
1494 * be as large as the whole frame */
1495 re_x = re->winx;
1496 re_y = re->winy;
1497 }
1498 else {
1499 re_x = re->result->rectx;
1500 re_y = re->result->recty;
1501 }
1502
1503 tot_views = BKE_scene_multiview_num_views_get(&re->r);
1504 ibuf_arr = MEM_mallocN(sizeof(ImBuf *) * tot_views, "Sequencer Views ImBufs");
1505
1506 BKE_sequencer_new_render_data(re->main,
1507 re->pipeline_depsgraph,
1508 re->scene,
1509 re_x,
1510 re_y,
1511 SEQ_RENDER_SIZE_SCENE,
1512 true,
1513 &context);
1514
1515 /* the renderresult gets destroyed during the rendering, so we first collect all ibufs
1516 * and then we populate the final renderesult */
1517
1518 for (view_id = 0; view_id < tot_views; view_id++) {
1519 context.view_id = view_id;
1520 out = BKE_sequencer_give_ibuf(&context, cfra, 0);
1521
1522 if (out) {
1523 ibuf_arr[view_id] = IMB_dupImBuf(out);
1524 IMB_metadata_copy(ibuf_arr[view_id], out);
1525 IMB_freeImBuf(out);
1526 BKE_sequencer_imbuf_from_sequencer_space(re->pipeline_scene_eval, ibuf_arr[view_id]);
1527 }
1528 else {
1529 ibuf_arr[view_id] = NULL;
1530 }
1531 }
1532
1533 rr = re->result;
1534
1535 BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
1536 render_result_views_new(rr, &re->r);
1537 BLI_rw_mutex_unlock(&re->resultmutex);
1538
1539 for (view_id = 0; view_id < tot_views; view_id++) {
1540 RenderView *rv = RE_RenderViewGetById(rr, view_id);
1541 BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
1542
1543 if (ibuf_arr[view_id]) {
1544 /* copy ibuf into combined pixel rect */
1545 RE_render_result_rect_from_ibuf(rr, &re->r, ibuf_arr[view_id], view_id);
1546
1547 if (ibuf_arr[view_id]->metadata && (re->r.stamp & R_STAMP_STRIPMETA)) {
1548 /* ensure render stamp info first */
1549 BKE_render_result_stamp_info(NULL, NULL, rr, true);
1550 BKE_stamp_info_from_imbuf(rr, ibuf_arr[view_id]);
1551 }
1552
1553 if (recurs_depth == 0) { /* With nested scenes, only free on top-level. */
1554 Editing *ed = re->pipeline_scene_eval->ed;
1555 if (ed) {
1556 BKE_sequencer_free_imbuf(re->pipeline_scene_eval, &ed->seqbase, true);
1557 }
1558 }
1559 IMB_freeImBuf(ibuf_arr[view_id]);
1560 }
1561 else {
1562 /* render result is delivered empty in most cases, nevertheless we handle all cases */
1563 render_result_rect_fill_zero(rr, view_id);
1564 }
1565
1566 BLI_rw_mutex_unlock(&re->resultmutex);
1567
1568 /* would mark display buffers as invalid */
1569 RE_SetActiveRenderView(re, rv->name);
1570 re->display_update(re->duh, re->result, NULL);
1571 }
1572
1573 MEM_freeN(ibuf_arr);
1574
1575 recurs_depth--;
1576
1577 /* just in case this flag went missing at some point */
1578 re->r.scemode |= R_DOSEQ;
1579
1580 /* set overall progress of sequence rendering */
1581 if (re->r.efra != re->r.sfra) {
1582 re->progress(re->prh, (float)(cfra - re->r.sfra) / (re->r.efra - re->r.sfra));
1583 }
1584 else {
1585 re->progress(re->prh, 1.0f);
1586 }
1587 }
1588
1589 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
1590
1591 /* main loop: doing sequence + 3d render + compositing */
do_render_all_options(Render * re)1592 static void do_render_all_options(Render *re)
1593 {
1594 bool render_seq = false;
1595
1596 re->current_scene_update(re->suh, re->scene);
1597
1598 BKE_scene_camera_switch_update(re->scene);
1599
1600 re->i.starttime = PIL_check_seconds_timer();
1601
1602 /* ensure no images are in memory from previous animated sequences */
1603 BKE_image_all_free_anim_ibufs(re->main, re->r.cfra);
1604 BKE_sequencer_all_free_anim_ibufs(re->scene, re->r.cfra);
1605
1606 if (RE_engine_render(re, 1)) {
1607 /* in this case external render overrides all */
1608 }
1609 else if (RE_seq_render_active(re->scene, &re->r)) {
1610 /* note: do_render_seq() frees rect32 when sequencer returns float images */
1611 if (!re->test_break(re->tbh)) {
1612 do_render_seq(re);
1613 render_seq = true;
1614 }
1615
1616 re->stats_draw(re->sdh, &re->i);
1617 re->display_update(re->duh, re->result, NULL);
1618 }
1619 else {
1620 do_render_composite(re);
1621 }
1622
1623 re->i.lastframetime = PIL_check_seconds_timer() - re->i.starttime;
1624
1625 re->stats_draw(re->sdh, &re->i);
1626
1627 /* save render result stamp if needed */
1628 if (re->result != NULL) {
1629 /* sequence rendering should have taken care of that already */
1630 if (!(render_seq && (re->r.stamp & R_STAMP_STRIPMETA))) {
1631 Object *ob_camera_eval = DEG_get_evaluated_object(re->pipeline_depsgraph, RE_GetCamera(re));
1632 BKE_render_result_stamp_info(re->scene, ob_camera_eval, re->result, false);
1633 }
1634
1635 /* stamp image info here */
1636 if ((re->r.stamp & R_STAMP_ALL) && (re->r.stamp & R_STAMP_DRAW)) {
1637 renderresult_stampinfo(re);
1638 re->display_update(re->duh, re->result, NULL);
1639 }
1640 }
1641 }
1642
check_valid_compositing_camera(Scene * scene,Object * camera_override)1643 static bool check_valid_compositing_camera(Scene *scene, Object *camera_override)
1644 {
1645 if (scene->r.scemode & R_DOCOMP && scene->use_nodes) {
1646 bNode *node = scene->nodetree->nodes.first;
1647
1648 while (node) {
1649 if (node->type == CMP_NODE_R_LAYERS && (node->flag & NODE_MUTED) == 0) {
1650 Scene *sce = node->id ? (Scene *)node->id : scene;
1651 if (sce->camera == NULL) {
1652 sce->camera = BKE_view_layer_camera_find(BKE_view_layer_default_render(sce));
1653 }
1654 if (sce->camera == NULL) {
1655 /* all render layers nodes need camera */
1656 return false;
1657 }
1658 }
1659 node = node->next;
1660 }
1661
1662 return true;
1663 }
1664
1665 return (camera_override != NULL || scene->camera != NULL);
1666 }
1667
check_valid_camera_multiview(Scene * scene,Object * camera,ReportList * reports)1668 static bool check_valid_camera_multiview(Scene *scene, Object *camera, ReportList *reports)
1669 {
1670 SceneRenderView *srv;
1671 bool active_view = false;
1672
1673 if (camera == NULL || (scene->r.scemode & R_MULTIVIEW) == 0) {
1674 return true;
1675 }
1676
1677 for (srv = scene->r.views.first; srv; srv = srv->next) {
1678 if (BKE_scene_multiview_is_render_view_active(&scene->r, srv)) {
1679 active_view = true;
1680
1681 if (scene->r.views_format == SCE_VIEWS_FORMAT_MULTIVIEW) {
1682 Object *view_camera;
1683 view_camera = BKE_camera_multiview_render(scene, camera, srv->name);
1684
1685 if (view_camera == camera) {
1686 /* if the suffix is not in the camera, means we are using the fallback camera */
1687 if (!BLI_str_endswith(view_camera->id.name + 2, srv->suffix)) {
1688 BKE_reportf(reports,
1689 RPT_ERROR,
1690 "Camera \"%s\" is not a multi-view camera",
1691 camera->id.name + 2);
1692 return false;
1693 }
1694 }
1695 }
1696 }
1697 }
1698
1699 if (!active_view) {
1700 BKE_reportf(reports, RPT_ERROR, "No active view found in scene \"%s\"", scene->id.name + 2);
1701 return false;
1702 }
1703
1704 return true;
1705 }
1706
check_valid_camera(Scene * scene,Object * camera_override,ReportList * reports)1707 static int check_valid_camera(Scene *scene, Object *camera_override, ReportList *reports)
1708 {
1709 const char *err_msg = "No camera found in scene \"%s\"";
1710
1711 if (camera_override == NULL && scene->camera == NULL) {
1712 scene->camera = BKE_view_layer_camera_find(BKE_view_layer_default_render(scene));
1713 }
1714
1715 if (!check_valid_camera_multiview(scene, scene->camera, reports)) {
1716 return false;
1717 }
1718
1719 if (RE_seq_render_active(scene, &scene->r)) {
1720 if (scene->ed) {
1721 Sequence *seq = scene->ed->seqbase.first;
1722
1723 while (seq) {
1724 if ((seq->type == SEQ_TYPE_SCENE) && ((seq->flag & SEQ_SCENE_STRIPS) == 0) &&
1725 (seq->scene != NULL)) {
1726 if (!seq->scene_camera) {
1727 if (!seq->scene->camera &&
1728 !BKE_view_layer_camera_find(BKE_view_layer_default_render(seq->scene))) {
1729 /* camera could be unneeded due to composite nodes */
1730 Object *override = (seq->scene == scene) ? camera_override : NULL;
1731
1732 if (!check_valid_compositing_camera(seq->scene, override)) {
1733 BKE_reportf(reports, RPT_ERROR, err_msg, seq->scene->id.name + 2);
1734 return false;
1735 }
1736 }
1737 }
1738 else if (!check_valid_camera_multiview(seq->scene, seq->scene_camera, reports)) {
1739 return false;
1740 }
1741 }
1742
1743 seq = seq->next;
1744 }
1745 }
1746 }
1747 else if (!check_valid_compositing_camera(scene, camera_override)) {
1748 BKE_reportf(reports, RPT_ERROR, err_msg, scene->id.name + 2);
1749 return false;
1750 }
1751
1752 return true;
1753 }
1754
node_tree_has_composite_output(bNodeTree * ntree)1755 static bool node_tree_has_composite_output(bNodeTree *ntree)
1756 {
1757 bNode *node;
1758
1759 for (node = ntree->nodes.first; node; node = node->next) {
1760 if (ELEM(node->type, CMP_NODE_COMPOSITE, CMP_NODE_OUTPUT_FILE)) {
1761 return true;
1762 }
1763 if (ELEM(node->type, NODE_GROUP, NODE_CUSTOM_GROUP)) {
1764 if (node->id) {
1765 if (node_tree_has_composite_output((bNodeTree *)node->id)) {
1766 return true;
1767 }
1768 }
1769 }
1770 }
1771
1772 return false;
1773 }
1774
check_composite_output(Scene * scene)1775 static int check_composite_output(Scene *scene)
1776 {
1777 return node_tree_has_composite_output(scene->nodetree);
1778 }
1779
RE_is_rendering_allowed(Scene * scene,ViewLayer * single_layer,Object * camera_override,ReportList * reports)1780 bool RE_is_rendering_allowed(Scene *scene,
1781 ViewLayer *single_layer,
1782 Object *camera_override,
1783 ReportList *reports)
1784 {
1785 int scemode = check_mode_full_sample(&scene->r);
1786
1787 if (scene->r.mode & R_BORDER) {
1788 if (scene->r.border.xmax <= scene->r.border.xmin ||
1789 scene->r.border.ymax <= scene->r.border.ymin) {
1790 BKE_report(reports, RPT_ERROR, "No border area selected");
1791 return 0;
1792 }
1793 }
1794
1795 if (scemode & (R_EXR_TILE_FILE | R_FULL_SAMPLE)) {
1796 char str[FILE_MAX];
1797
1798 render_result_exr_file_path(scene, "", 0, str);
1799
1800 if (!BLI_file_is_writable(str)) {
1801 BKE_report(reports, RPT_ERROR, "Cannot save render buffers, check the temp default path");
1802 return 0;
1803 }
1804 }
1805
1806 if (RE_seq_render_active(scene, &scene->r)) {
1807 /* Sequencer */
1808 if (scene->r.mode & R_BORDER) {
1809 BKE_report(reports, RPT_ERROR, "Border rendering is not supported by sequencer");
1810 return false;
1811 }
1812 }
1813 else if ((scemode & R_DOCOMP) && scene->use_nodes) {
1814 /* Compositor */
1815 if (!scene->nodetree) {
1816 BKE_report(reports, RPT_ERROR, "No node tree in scene");
1817 return 0;
1818 }
1819
1820 if (!check_composite_output(scene)) {
1821 BKE_report(reports, RPT_ERROR, "No render output node in scene");
1822 return 0;
1823 }
1824
1825 if (scemode & R_FULL_SAMPLE) {
1826 if (composite_needs_render(scene, 0) == 0) {
1827 BKE_report(reports, RPT_ERROR, "Full sample AA not supported without 3D rendering");
1828 return 0;
1829 }
1830 }
1831 }
1832 else {
1833 /* Regular Render */
1834 if (!render_scene_has_layers_to_render(scene, single_layer)) {
1835 BKE_report(reports, RPT_ERROR, "All render layers are disabled");
1836 return 0;
1837 }
1838 }
1839
1840 /* check valid camera, without camera render is OK (compo, seq) */
1841 if (!check_valid_camera(scene, camera_override, reports)) {
1842 return 0;
1843 }
1844
1845 return 1;
1846 }
1847
validate_render_settings(Render * re)1848 static void validate_render_settings(Render *re)
1849 {
1850 if (RE_engine_is_external(re)) {
1851 /* not supported yet */
1852 re->r.scemode &= ~R_FULL_SAMPLE;
1853 }
1854 }
1855
update_physics_cache(Render * re,Scene * scene,ViewLayer * view_layer,int UNUSED (anim_init))1856 static void update_physics_cache(Render *re,
1857 Scene *scene,
1858 ViewLayer *view_layer,
1859 int UNUSED(anim_init))
1860 {
1861 PTCacheBaker baker;
1862
1863 memset(&baker, 0, sizeof(baker));
1864 baker.bmain = re->main;
1865 baker.scene = scene;
1866 baker.view_layer = view_layer;
1867 baker.depsgraph = BKE_scene_ensure_depsgraph(re->main, scene, view_layer);
1868 baker.bake = 0;
1869 baker.render = 1;
1870 baker.anim_init = 1;
1871 baker.quick_step = 1;
1872
1873 BKE_ptcache_bake(&baker);
1874 }
1875
RE_SetActiveRenderView(Render * re,const char * viewname)1876 void RE_SetActiveRenderView(Render *re, const char *viewname)
1877 {
1878 BLI_strncpy(re->viewname, viewname, sizeof(re->viewname));
1879 }
1880
RE_GetActiveRenderView(Render * re)1881 const char *RE_GetActiveRenderView(Render *re)
1882 {
1883 return re->viewname;
1884 }
1885
1886 /* evaluating scene options for general Blender render */
render_init_from_main(Render * re,const RenderData * rd,Main * bmain,Scene * scene,ViewLayer * single_layer,Object * camera_override,int anim,int anim_init)1887 static int render_init_from_main(Render *re,
1888 const RenderData *rd,
1889 Main *bmain,
1890 Scene *scene,
1891 ViewLayer *single_layer,
1892 Object *camera_override,
1893 int anim,
1894 int anim_init)
1895 {
1896 int winx, winy;
1897 rcti disprect;
1898
1899 /* r.xsch and r.ysch has the actual view window size
1900 * r.border is the clipping rect */
1901
1902 /* calculate actual render result and display size */
1903 winx = (rd->size * rd->xsch) / 100;
1904 winy = (rd->size * rd->ysch) / 100;
1905
1906 /* We always render smaller part, inserting it in larger image is compositor business,
1907 * it uses 'disprect' for it. */
1908 if (scene->r.mode & R_BORDER) {
1909 disprect.xmin = rd->border.xmin * winx;
1910 disprect.xmax = rd->border.xmax * winx;
1911
1912 disprect.ymin = rd->border.ymin * winy;
1913 disprect.ymax = rd->border.ymax * winy;
1914 }
1915 else {
1916 disprect.xmin = disprect.ymin = 0;
1917 disprect.xmax = winx;
1918 disprect.ymax = winy;
1919 }
1920
1921 re->main = bmain;
1922 re->scene = scene;
1923 re->camera_override = camera_override;
1924 re->viewname[0] = '\0';
1925
1926 /* not too nice, but it survives anim-border render */
1927 if (anim) {
1928 render_update_anim_renderdata(re, &scene->r, &scene->view_layers);
1929 re->disprect = disprect;
1930 return 1;
1931 }
1932
1933 /*
1934 * Disabled completely for now,
1935 * can be later set as render profile option
1936 * and default for background render.
1937 */
1938 if (0) {
1939 /* make sure dynamics are up to date */
1940 ViewLayer *view_layer = BKE_view_layer_context_active_PLACEHOLDER(scene);
1941 update_physics_cache(re, scene, view_layer, anim_init);
1942 }
1943
1944 if (single_layer || scene->r.scemode & R_SINGLE_LAYER) {
1945 BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
1946 render_result_single_layer_begin(re);
1947 BLI_rw_mutex_unlock(&re->resultmutex);
1948 }
1949
1950 RE_InitState(re, NULL, &scene->r, &scene->view_layers, single_layer, winx, winy, &disprect);
1951 if (!re->ok) { /* if an error was printed, abort */
1952 return 0;
1953 }
1954
1955 /* initstate makes new result, have to send changed tags around */
1956 ntreeCompositTagRender(re->scene);
1957
1958 validate_render_settings(re);
1959
1960 re->display_init(re->dih, re->result);
1961 re->display_clear(re->dch, re->result);
1962
1963 return 1;
1964 }
1965
RE_SetReports(Render * re,ReportList * reports)1966 void RE_SetReports(Render *re, ReportList *reports)
1967 {
1968 re->reports = reports;
1969 }
1970
render_update_depsgraph(Render * re)1971 static void render_update_depsgraph(Render *re)
1972 {
1973 Scene *scene = re->scene;
1974 DEG_evaluate_on_framechange(re->pipeline_depsgraph, CFRA);
1975 BKE_scene_update_sound(re->pipeline_depsgraph, re->main);
1976 }
1977
render_init_depsgraph(Render * re)1978 static void render_init_depsgraph(Render *re)
1979 {
1980 Scene *scene = re->scene;
1981 ViewLayer *view_layer = BKE_view_layer_default_render(re->scene);
1982
1983 re->pipeline_depsgraph = DEG_graph_new(re->main, scene, view_layer, DAG_EVAL_RENDER);
1984 DEG_debug_name_set(re->pipeline_depsgraph, "RENDER PIPELINE");
1985
1986 /* Make sure there is a correct evaluated scene pointer. */
1987 DEG_graph_build_for_render_pipeline(re->pipeline_depsgraph);
1988
1989 /* Update immediately so we have proper evaluated scene. */
1990 render_update_depsgraph(re);
1991
1992 re->pipeline_scene_eval = DEG_get_evaluated_scene(re->pipeline_depsgraph);
1993 }
1994
1995 /* general Blender frame render call */
RE_RenderFrame(Render * re,Main * bmain,Scene * scene,ViewLayer * single_layer,Object * camera_override,int frame,const bool write_still)1996 void RE_RenderFrame(Render *re,
1997 Main *bmain,
1998 Scene *scene,
1999 ViewLayer *single_layer,
2000 Object *camera_override,
2001 int frame,
2002 const bool write_still)
2003 {
2004 render_callback_exec_id(re, re->main, &scene->id, BKE_CB_EVT_RENDER_INIT);
2005
2006 /* Ugly global still...
2007 * is to prevent preview events and signal subsurfs etc to make full resol. */
2008 G.is_rendering = true;
2009
2010 scene->r.cfra = frame;
2011
2012 if (render_init_from_main(re, &scene->r, bmain, scene, single_layer, camera_override, 0, 0)) {
2013 const RenderData rd = scene->r;
2014 MEM_reset_peak_memory();
2015
2016 render_callback_exec_id(re, re->main, &scene->id, BKE_CB_EVT_RENDER_PRE);
2017
2018 render_init_depsgraph(re);
2019
2020 do_render_all_options(re);
2021
2022 if (write_still && !G.is_break) {
2023 if (BKE_imtype_is_movie(rd.im_format.imtype)) {
2024 /* operator checks this but in case its called from elsewhere */
2025 printf("Error: cant write single images with a movie format!\n");
2026 }
2027 else {
2028 char name[FILE_MAX];
2029 BKE_image_path_from_imformat(name,
2030 rd.pic,
2031 BKE_main_blendfile_path(bmain),
2032 scene->r.cfra,
2033 &rd.im_format,
2034 (rd.scemode & R_EXTENSION) != 0,
2035 false,
2036 NULL);
2037
2038 /* reports only used for Movie */
2039 do_write_image_or_movie(re, bmain, scene, NULL, 0, name);
2040 }
2041 }
2042
2043 /* keep after file save */
2044 render_callback_exec_id(re, re->main, &scene->id, BKE_CB_EVT_RENDER_POST);
2045 if (write_still) {
2046 render_callback_exec_id(re, re->main, &scene->id, BKE_CB_EVT_RENDER_WRITE);
2047 }
2048 }
2049
2050 render_callback_exec_id(re,
2051 re->main,
2052 &scene->id,
2053 G.is_break ? BKE_CB_EVT_RENDER_CANCEL : BKE_CB_EVT_RENDER_COMPLETE);
2054
2055 RE_CleanAfterRender(re);
2056
2057 /* UGLY WARNING */
2058 G.is_rendering = false;
2059 }
2060
2061 #ifdef WITH_FREESTYLE
RE_RenderFreestyleStrokes(Render * re,Main * bmain,Scene * scene,int render)2062 void RE_RenderFreestyleStrokes(Render *re, Main *bmain, Scene *scene, int render)
2063 {
2064 re->result_ok = 0;
2065 if (render_init_from_main(re, &scene->r, bmain, scene, NULL, NULL, 0, 0)) {
2066 if (render) {
2067 do_render_3d(re);
2068 }
2069 }
2070 re->result_ok = 1;
2071 }
2072
RE_RenderFreestyleExternal(Render * re)2073 void RE_RenderFreestyleExternal(Render *re)
2074 {
2075 if (re->test_break(re->tbh)) {
2076 return;
2077 }
2078
2079 FRS_init_stroke_renderer(re);
2080
2081 LISTBASE_FOREACH (RenderView *, rv, &re->result->views) {
2082 RE_SetActiveRenderView(re, rv->name);
2083
2084 ViewLayer *active_view_layer = BLI_findlink(&re->view_layers, re->active_view_layer);
2085 FRS_begin_stroke_rendering(re);
2086
2087 LISTBASE_FOREACH (ViewLayer *, view_layer, &re->view_layers) {
2088 if ((re->r.scemode & R_SINGLE_LAYER) && view_layer != active_view_layer) {
2089 continue;
2090 }
2091
2092 if (FRS_is_freestyle_enabled(view_layer)) {
2093 FRS_do_stroke_rendering(re, view_layer);
2094 }
2095 }
2096
2097 FRS_end_stroke_rendering(re);
2098 }
2099 }
2100 #endif
2101
RE_WriteRenderViewsImage(ReportList * reports,RenderResult * rr,Scene * scene,const bool stamp,char * name)2102 bool RE_WriteRenderViewsImage(
2103 ReportList *reports, RenderResult *rr, Scene *scene, const bool stamp, char *name)
2104 {
2105 bool ok = true;
2106 RenderData *rd = &scene->r;
2107
2108 if (!rr) {
2109 return false;
2110 }
2111
2112 bool is_mono = BLI_listbase_count_at_most(&rr->views, 2) < 2;
2113 bool is_exr_rr = ELEM(rd->im_format.imtype, R_IMF_IMTYPE_OPENEXR, R_IMF_IMTYPE_MULTILAYER) &&
2114 RE_HasFloatPixels(rr);
2115
2116 if (rd->im_format.views_format == R_IMF_VIEWS_MULTIVIEW && is_exr_rr) {
2117 ok = RE_WriteRenderResult(reports, rr, name, &rd->im_format, NULL, -1);
2118 render_print_save_message(reports, name, ok, errno);
2119 }
2120
2121 /* mono, legacy code */
2122 else if (is_mono || (rd->im_format.views_format == R_IMF_VIEWS_INDIVIDUAL)) {
2123 RenderView *rv;
2124 int view_id;
2125 char filepath[FILE_MAX];
2126
2127 BLI_strncpy(filepath, name, sizeof(filepath));
2128
2129 for (view_id = 0, rv = rr->views.first; rv; rv = rv->next, view_id++) {
2130 if (!is_mono) {
2131 BKE_scene_multiview_view_filepath_get(&scene->r, filepath, rv->name, name);
2132 }
2133
2134 if (is_exr_rr) {
2135 ok = RE_WriteRenderResult(reports, rr, name, &rd->im_format, rv->name, -1);
2136 render_print_save_message(reports, name, ok, errno);
2137
2138 /* optional preview images for exr */
2139 if (ok && (rd->im_format.flag & R_IMF_FLAG_PREVIEW_JPG)) {
2140 ImageFormatData imf = rd->im_format;
2141 imf.imtype = R_IMF_IMTYPE_JPEG90;
2142
2143 if (BLI_path_extension_check(name, ".exr")) {
2144 name[strlen(name) - 4] = 0;
2145 }
2146 BKE_image_path_ensure_ext_from_imformat(name, &imf);
2147
2148 ImBuf *ibuf = render_result_rect_to_ibuf(rr, rd, view_id);
2149 ibuf->planes = 24;
2150 IMB_colormanagement_imbuf_for_write(
2151 ibuf, true, false, &scene->view_settings, &scene->display_settings, &imf);
2152
2153 ok = render_imbuf_write_stamp_test(reports, scene, rr, ibuf, name, &imf, stamp);
2154
2155 IMB_freeImBuf(ibuf);
2156 }
2157 }
2158 else {
2159 ImBuf *ibuf = render_result_rect_to_ibuf(rr, rd, view_id);
2160
2161 IMB_colormanagement_imbuf_for_write(
2162 ibuf, true, false, &scene->view_settings, &scene->display_settings, &rd->im_format);
2163
2164 ok = render_imbuf_write_stamp_test(reports, scene, rr, ibuf, name, &rd->im_format, stamp);
2165
2166 /* imbuf knows which rects are not part of ibuf */
2167 IMB_freeImBuf(ibuf);
2168 }
2169 }
2170 }
2171 else { /* R_IMF_VIEWS_STEREO_3D */
2172 BLI_assert(scene->r.im_format.views_format == R_IMF_VIEWS_STEREO_3D);
2173
2174 if (rd->im_format.imtype == R_IMF_IMTYPE_MULTILAYER) {
2175 printf("Stereo 3D not supported for MultiLayer image: %s\n", name);
2176 }
2177 else {
2178 ImBuf *ibuf_arr[3] = {NULL};
2179 const char *names[2] = {STEREO_LEFT_NAME, STEREO_RIGHT_NAME};
2180 int i;
2181
2182 for (i = 0; i < 2; i++) {
2183 int view_id = BLI_findstringindex(&rr->views, names[i], offsetof(RenderView, name));
2184 ibuf_arr[i] = render_result_rect_to_ibuf(rr, rd, view_id);
2185 IMB_colormanagement_imbuf_for_write(ibuf_arr[i],
2186 true,
2187 false,
2188 &scene->view_settings,
2189 &scene->display_settings,
2190 &scene->r.im_format);
2191 IMB_prepare_write_ImBuf(IMB_isfloat(ibuf_arr[i]), ibuf_arr[i]);
2192 }
2193
2194 ibuf_arr[2] = IMB_stereo3d_ImBuf(&scene->r.im_format, ibuf_arr[0], ibuf_arr[1]);
2195
2196 ok = render_imbuf_write_stamp_test(
2197 reports, scene, rr, ibuf_arr[2], name, &rd->im_format, stamp);
2198
2199 /* optional preview images for exr */
2200 if (ok && is_exr_rr && (rd->im_format.flag & R_IMF_FLAG_PREVIEW_JPG)) {
2201 ImageFormatData imf = rd->im_format;
2202 imf.imtype = R_IMF_IMTYPE_JPEG90;
2203
2204 if (BLI_path_extension_check(name, ".exr")) {
2205 name[strlen(name) - 4] = 0;
2206 }
2207
2208 BKE_image_path_ensure_ext_from_imformat(name, &imf);
2209 ibuf_arr[2]->planes = 24;
2210
2211 ok = render_imbuf_write_stamp_test(
2212 reports, scene, rr, ibuf_arr[2], name, &rd->im_format, stamp);
2213 }
2214
2215 /* imbuf knows which rects are not part of ibuf */
2216 for (i = 0; i < 3; i++) {
2217 IMB_freeImBuf(ibuf_arr[i]);
2218 }
2219 }
2220 }
2221
2222 return ok;
2223 }
2224
RE_WriteRenderViewsMovie(ReportList * reports,RenderResult * rr,Scene * scene,RenderData * rd,bMovieHandle * mh,void ** movie_ctx_arr,const int totvideos,bool preview)2225 bool RE_WriteRenderViewsMovie(ReportList *reports,
2226 RenderResult *rr,
2227 Scene *scene,
2228 RenderData *rd,
2229 bMovieHandle *mh,
2230 void **movie_ctx_arr,
2231 const int totvideos,
2232 bool preview)
2233 {
2234 bool is_mono;
2235 bool ok = true;
2236
2237 if (!rr) {
2238 return false;
2239 }
2240
2241 is_mono = BLI_listbase_count_at_most(&rr->views, 2) < 2;
2242
2243 if (is_mono || (scene->r.im_format.views_format == R_IMF_VIEWS_INDIVIDUAL)) {
2244 int view_id;
2245 for (view_id = 0; view_id < totvideos; view_id++) {
2246 const char *suffix = BKE_scene_multiview_view_id_suffix_get(&scene->r, view_id);
2247 ImBuf *ibuf = render_result_rect_to_ibuf(rr, &scene->r, view_id);
2248
2249 IMB_colormanagement_imbuf_for_write(
2250 ibuf, true, false, &scene->view_settings, &scene->display_settings, &scene->r.im_format);
2251
2252 ok &= mh->append_movie(movie_ctx_arr[view_id],
2253 rd,
2254 preview ? scene->r.psfra : scene->r.sfra,
2255 scene->r.cfra,
2256 (int *)ibuf->rect,
2257 ibuf->x,
2258 ibuf->y,
2259 suffix,
2260 reports);
2261
2262 /* imbuf knows which rects are not part of ibuf */
2263 IMB_freeImBuf(ibuf);
2264 }
2265 printf("Append frame %d\n", scene->r.cfra);
2266 }
2267 else { /* R_IMF_VIEWS_STEREO_3D */
2268 const char *names[2] = {STEREO_LEFT_NAME, STEREO_RIGHT_NAME};
2269 ImBuf *ibuf_arr[3] = {NULL};
2270 int i;
2271
2272 BLI_assert((totvideos == 1) && (scene->r.im_format.views_format == R_IMF_VIEWS_STEREO_3D));
2273
2274 for (i = 0; i < 2; i++) {
2275 int view_id = BLI_findstringindex(&rr->views, names[i], offsetof(RenderView, name));
2276 ibuf_arr[i] = render_result_rect_to_ibuf(rr, &scene->r, view_id);
2277
2278 IMB_colormanagement_imbuf_for_write(ibuf_arr[i],
2279 true,
2280 false,
2281 &scene->view_settings,
2282 &scene->display_settings,
2283 &scene->r.im_format);
2284 }
2285
2286 ibuf_arr[2] = IMB_stereo3d_ImBuf(&scene->r.im_format, ibuf_arr[0], ibuf_arr[1]);
2287
2288 ok = mh->append_movie(movie_ctx_arr[0],
2289 rd,
2290 preview ? scene->r.psfra : scene->r.sfra,
2291 scene->r.cfra,
2292 (int *)ibuf_arr[2]->rect,
2293 ibuf_arr[2]->x,
2294 ibuf_arr[2]->y,
2295 "",
2296 reports);
2297
2298 for (i = 0; i < 3; i++) {
2299 /* imbuf knows which rects are not part of ibuf */
2300 IMB_freeImBuf(ibuf_arr[i]);
2301 }
2302 }
2303
2304 return ok;
2305 }
2306
do_write_image_or_movie(Render * re,Main * bmain,Scene * scene,bMovieHandle * mh,const int totvideos,const char * name_override)2307 static int do_write_image_or_movie(Render *re,
2308 Main *bmain,
2309 Scene *scene,
2310 bMovieHandle *mh,
2311 const int totvideos,
2312 const char *name_override)
2313 {
2314 char name[FILE_MAX];
2315 RenderResult rres;
2316 double render_time;
2317 bool ok = true;
2318
2319 RE_AcquireResultImageViews(re, &rres);
2320
2321 /* write movie or image */
2322 if (BKE_imtype_is_movie(scene->r.im_format.imtype)) {
2323 RE_WriteRenderViewsMovie(
2324 re->reports, &rres, scene, &re->r, mh, re->movie_ctx_arr, totvideos, false);
2325 }
2326 else {
2327 if (name_override) {
2328 BLI_strncpy(name, name_override, sizeof(name));
2329 }
2330 else {
2331 BKE_image_path_from_imformat(name,
2332 scene->r.pic,
2333 BKE_main_blendfile_path(bmain),
2334 scene->r.cfra,
2335 &scene->r.im_format,
2336 (scene->r.scemode & R_EXTENSION) != 0,
2337 true,
2338 NULL);
2339 }
2340
2341 /* write images as individual images or stereo */
2342 ok = RE_WriteRenderViewsImage(re->reports, &rres, scene, true, name);
2343 }
2344
2345 RE_ReleaseResultImageViews(re, &rres);
2346
2347 render_time = re->i.lastframetime;
2348 re->i.lastframetime = PIL_check_seconds_timer() - re->i.starttime;
2349
2350 BLI_timecode_string_from_time_simple(name, sizeof(name), re->i.lastframetime);
2351 printf(" Time: %s", name);
2352
2353 /* Flush stdout to be sure python callbacks are printing stuff after blender. */
2354 fflush(stdout);
2355
2356 /* NOTE: using G_MAIN seems valid here???
2357 * Not sure it's actually even used anyway, we could as well pass NULL? */
2358 render_callback_exec_null(re, G_MAIN, BKE_CB_EVT_RENDER_STATS);
2359
2360 BLI_timecode_string_from_time_simple(name, sizeof(name), re->i.lastframetime - render_time);
2361 printf(" (Saving: %s)\n", name);
2362
2363 fputc('\n', stdout);
2364 fflush(stdout); /* needed for renderd !! (not anymore... (ton)) */
2365
2366 return ok;
2367 }
2368
get_videos_dimensions(const Render * re,const RenderData * rd,size_t * r_width,size_t * r_height)2369 static void get_videos_dimensions(const Render *re,
2370 const RenderData *rd,
2371 size_t *r_width,
2372 size_t *r_height)
2373 {
2374 size_t width, height;
2375 if (re->r.mode & R_BORDER) {
2376 if ((re->r.mode & R_CROP) == 0) {
2377 width = re->winx;
2378 height = re->winy;
2379 }
2380 else {
2381 width = re->rectx;
2382 height = re->recty;
2383 }
2384 }
2385 else {
2386 width = re->rectx;
2387 height = re->recty;
2388 }
2389
2390 BKE_scene_multiview_videos_dimensions_get(rd, width, height, r_width, r_height);
2391 }
2392
re_movie_free_all(Render * re,bMovieHandle * mh,int totvideos)2393 static void re_movie_free_all(Render *re, bMovieHandle *mh, int totvideos)
2394 {
2395 int i;
2396
2397 for (i = 0; i < totvideos; i++) {
2398 mh->end_movie(re->movie_ctx_arr[i]);
2399 mh->context_free(re->movie_ctx_arr[i]);
2400 }
2401
2402 MEM_SAFE_FREE(re->movie_ctx_arr);
2403 }
2404
2405 /* saves images to disk */
RE_RenderAnim(Render * re,Main * bmain,Scene * scene,ViewLayer * single_layer,Object * camera_override,int sfra,int efra,int tfra)2406 void RE_RenderAnim(Render *re,
2407 Main *bmain,
2408 Scene *scene,
2409 ViewLayer *single_layer,
2410 Object *camera_override,
2411 int sfra,
2412 int efra,
2413 int tfra)
2414 {
2415 /* Call hooks before taking a copy of scene->r, so user can alter the render settings prior to
2416 * copying (e.g. alter the output path). */
2417 render_callback_exec_id(re, re->main, &scene->id, BKE_CB_EVT_RENDER_INIT);
2418
2419 const RenderData rd = scene->r;
2420 bMovieHandle *mh = NULL;
2421 const int cfrao = rd.cfra;
2422 int nfra, totrendered = 0, totskipped = 0;
2423 const int totvideos = BKE_scene_multiview_num_videos_get(&rd);
2424 const bool is_movie = BKE_imtype_is_movie(rd.im_format.imtype);
2425 const bool is_multiview_name = ((rd.scemode & R_MULTIVIEW) != 0 &&
2426 (rd.im_format.views_format == R_IMF_VIEWS_INDIVIDUAL));
2427
2428 /* do not fully call for each frame, it initializes & pops output window */
2429 if (!render_init_from_main(re, &rd, bmain, scene, single_layer, camera_override, 0, 1)) {
2430 return;
2431 }
2432
2433 render_init_depsgraph(re);
2434
2435 if (is_movie) {
2436 size_t width, height;
2437 int i;
2438 bool is_error = false;
2439
2440 get_videos_dimensions(re, &rd, &width, &height);
2441
2442 mh = BKE_movie_handle_get(rd.im_format.imtype);
2443 if (mh == NULL) {
2444 BKE_report(re->reports, RPT_ERROR, "Movie format unsupported");
2445 return;
2446 }
2447
2448 re->movie_ctx_arr = MEM_mallocN(sizeof(void *) * totvideos, "Movies' Context");
2449
2450 for (i = 0; i < totvideos; i++) {
2451 const char *suffix = BKE_scene_multiview_view_id_suffix_get(&re->r, i);
2452
2453 re->movie_ctx_arr[i] = mh->context_create();
2454
2455 if (!mh->start_movie(re->movie_ctx_arr[i],
2456 re->pipeline_scene_eval,
2457 &re->r,
2458 width,
2459 height,
2460 re->reports,
2461 false,
2462 suffix)) {
2463 is_error = true;
2464 break;
2465 }
2466 }
2467
2468 if (is_error) {
2469 /* report is handled above */
2470 re_movie_free_all(re, mh, i + 1);
2471 RE_CleanAfterRender(re);
2472 return;
2473 }
2474 }
2475
2476 /* Ugly global still... is to prevent renderwin events and signal subsurfs etc to make full resol
2477 * is also set by caller renderwin.c */
2478 G.is_rendering = true;
2479
2480 re->flag |= R_ANIMATION;
2481
2482 {
2483 for (nfra = sfra, scene->r.cfra = sfra; scene->r.cfra <= efra; scene->r.cfra++) {
2484 char name[FILE_MAX];
2485
2486 /* A feedback loop exists here -- render initialization requires updated
2487 * render layers settings which could be animated, but scene evaluation for
2488 * the frame happens later because it depends on what layers are visible to
2489 * render engine.
2490 *
2491 * The idea here is to only evaluate animation data associated with the scene,
2492 * which will make sure render layer settings are up-to-date, initialize the
2493 * render database itself and then perform full scene update with only needed
2494 * layers.
2495 * -sergey-
2496 */
2497 {
2498 float ctime = BKE_scene_frame_get(scene);
2499 AnimData *adt = BKE_animdata_from_id(&scene->id);
2500 const AnimationEvalContext anim_eval_context = BKE_animsys_eval_context_construct(
2501 re->pipeline_depsgraph, ctime);
2502 BKE_animsys_evaluate_animdata(&scene->id, adt, &anim_eval_context, ADT_RECALC_ALL, false);
2503 }
2504
2505 render_update_depsgraph(re);
2506
2507 /* only border now, todo: camera lens. (ton) */
2508 render_init_from_main(re, &rd, bmain, scene, single_layer, camera_override, 1, 0);
2509
2510 if (nfra != scene->r.cfra) {
2511 /* Skip this frame, but could update for physics and particles system. */
2512 continue;
2513 }
2514
2515 nfra += tfra;
2516
2517 /* Touch/NoOverwrite options are only valid for image's */
2518 if (is_movie == false) {
2519 if (rd.mode & (R_NO_OVERWRITE | R_TOUCH)) {
2520 BKE_image_path_from_imformat(name,
2521 rd.pic,
2522 BKE_main_blendfile_path(bmain),
2523 scene->r.cfra,
2524 &rd.im_format,
2525 (rd.scemode & R_EXTENSION) != 0,
2526 true,
2527 NULL);
2528 }
2529
2530 if (rd.mode & R_NO_OVERWRITE) {
2531 if (!is_multiview_name) {
2532 if (BLI_exists(name)) {
2533 printf("skipping existing frame \"%s\"\n", name);
2534 totskipped++;
2535 continue;
2536 }
2537 }
2538 else {
2539 SceneRenderView *srv;
2540 bool is_skip = false;
2541 char filepath[FILE_MAX];
2542
2543 for (srv = scene->r.views.first; srv; srv = srv->next) {
2544 if (!BKE_scene_multiview_is_render_view_active(&scene->r, srv)) {
2545 continue;
2546 }
2547
2548 BKE_scene_multiview_filepath_get(srv, name, filepath);
2549
2550 if (BLI_exists(filepath)) {
2551 is_skip = true;
2552 printf("skipping existing frame \"%s\" for view \"%s\"\n", filepath, srv->name);
2553 }
2554 }
2555
2556 if (is_skip) {
2557 totskipped++;
2558 continue;
2559 }
2560 }
2561 }
2562
2563 if (rd.mode & R_TOUCH) {
2564 if (!is_multiview_name) {
2565 if (!BLI_exists(name)) {
2566 BLI_make_existing_file(name); /* makes the dir if its not there */
2567 BLI_file_touch(name);
2568 }
2569 }
2570 else {
2571 SceneRenderView *srv;
2572 char filepath[FILE_MAX];
2573
2574 for (srv = scene->r.views.first; srv; srv = srv->next) {
2575 if (!BKE_scene_multiview_is_render_view_active(&scene->r, srv)) {
2576 continue;
2577 }
2578
2579 BKE_scene_multiview_filepath_get(srv, name, filepath);
2580
2581 if (!BLI_exists(filepath)) {
2582 BLI_make_existing_file(filepath); /* makes the dir if its not there */
2583 BLI_file_touch(filepath);
2584 }
2585 }
2586 }
2587 }
2588 }
2589
2590 re->r.cfra = scene->r.cfra; /* weak.... */
2591
2592 /* run callbacks before rendering, before the scene is updated */
2593 render_callback_exec_id(re, re->main, &scene->id, BKE_CB_EVT_RENDER_PRE);
2594
2595 do_render_all_options(re);
2596 totrendered++;
2597
2598 if (re->test_break(re->tbh) == 0) {
2599 if (!G.is_break) {
2600 if (!do_write_image_or_movie(re, bmain, scene, mh, totvideos, NULL)) {
2601 G.is_break = true;
2602 }
2603 }
2604 }
2605 else {
2606 G.is_break = true;
2607 }
2608
2609 if (G.is_break == true) {
2610 /* remove touched file */
2611 if (is_movie == false) {
2612 if ((rd.mode & R_TOUCH)) {
2613 if (!is_multiview_name) {
2614 if ((BLI_file_size(name) == 0)) {
2615 /* BLI_exists(name) is implicit */
2616 BLI_delete(name, false, false);
2617 }
2618 }
2619 else {
2620 SceneRenderView *srv;
2621 char filepath[FILE_MAX];
2622
2623 for (srv = scene->r.views.first; srv; srv = srv->next) {
2624 if (!BKE_scene_multiview_is_render_view_active(&scene->r, srv)) {
2625 continue;
2626 }
2627
2628 BKE_scene_multiview_filepath_get(srv, name, filepath);
2629
2630 if ((BLI_file_size(filepath) == 0)) {
2631 /* BLI_exists(filepath) is implicit */
2632 BLI_delete(filepath, false, false);
2633 }
2634 }
2635 }
2636 }
2637 }
2638
2639 break;
2640 }
2641
2642 if (G.is_break == false) {
2643 /* keep after file save */
2644 render_callback_exec_id(re, re->main, &scene->id, BKE_CB_EVT_RENDER_POST);
2645 render_callback_exec_id(re, re->main, &scene->id, BKE_CB_EVT_RENDER_WRITE);
2646 }
2647 }
2648 }
2649
2650 /* end movie */
2651 if (is_movie) {
2652 re_movie_free_all(re, mh, totvideos);
2653 }
2654
2655 if (totskipped && totrendered == 0) {
2656 BKE_report(re->reports, RPT_INFO, "No frames rendered, skipped to not overwrite");
2657 }
2658
2659 scene->r.cfra = cfrao;
2660
2661 re->flag &= ~R_ANIMATION;
2662
2663 render_callback_exec_id(re,
2664 re->main,
2665 &scene->id,
2666 G.is_break ? BKE_CB_EVT_RENDER_CANCEL : BKE_CB_EVT_RENDER_COMPLETE);
2667 BKE_sound_reset_scene_specs(re->pipeline_scene_eval);
2668
2669 RE_CleanAfterRender(re);
2670
2671 /* UGLY WARNING */
2672 G.is_rendering = false;
2673 }
2674
RE_PreviewRender(Render * re,Main * bmain,Scene * sce)2675 void RE_PreviewRender(Render *re, Main *bmain, Scene *sce)
2676 {
2677 Object *camera;
2678 int winx, winy;
2679
2680 winx = (sce->r.size * sce->r.xsch) / 100;
2681 winy = (sce->r.size * sce->r.ysch) / 100;
2682
2683 RE_InitState(re, NULL, &sce->r, &sce->view_layers, NULL, winx, winy, NULL);
2684
2685 re->main = bmain;
2686 re->scene = sce;
2687
2688 camera = RE_GetCamera(re);
2689 RE_SetCamera(re, camera);
2690
2691 do_render_3d(re);
2692 }
2693
RE_CleanAfterRender(Render * re)2694 void RE_CleanAfterRender(Render *re)
2695 {
2696 /* Destroy the opengl context in the correct thread. */
2697 RE_gl_context_destroy(re);
2698 if (re->pipeline_depsgraph != NULL) {
2699 DEG_graph_free(re->pipeline_depsgraph);
2700 }
2701 re->pipeline_depsgraph = NULL;
2702 re->pipeline_scene_eval = NULL;
2703 }
2704
2705 /* note; repeated win/disprect calc... solve that nicer, also in compo */
2706
2707 /* only the temp file! */
RE_ReadRenderResult(Scene * scene,Scene * scenode)2708 bool RE_ReadRenderResult(Scene *scene, Scene *scenode)
2709 {
2710 Render *re;
2711 int winx, winy;
2712 bool success;
2713 rcti disprect;
2714
2715 /* calculate actual render result and display size */
2716 winx = (scene->r.size * scene->r.xsch) / 100;
2717 winy = (scene->r.size * scene->r.ysch) / 100;
2718
2719 /* only in movie case we render smaller part */
2720 if (scene->r.mode & R_BORDER) {
2721 disprect.xmin = scene->r.border.xmin * winx;
2722 disprect.xmax = scene->r.border.xmax * winx;
2723
2724 disprect.ymin = scene->r.border.ymin * winy;
2725 disprect.ymax = scene->r.border.ymax * winy;
2726 }
2727 else {
2728 disprect.xmin = disprect.ymin = 0;
2729 disprect.xmax = winx;
2730 disprect.ymax = winy;
2731 }
2732
2733 if (scenode) {
2734 scene = scenode;
2735 }
2736
2737 /* get render: it can be called from UI with draw callbacks */
2738 re = RE_GetSceneRender(scene);
2739 if (re == NULL) {
2740 re = RE_NewSceneRender(scene);
2741 }
2742 RE_InitState(re, NULL, &scene->r, &scene->view_layers, NULL, winx, winy, &disprect);
2743 re->scene = scene;
2744
2745 BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
2746 success = render_result_exr_file_cache_read(re);
2747 BLI_rw_mutex_unlock(&re->resultmutex);
2748
2749 render_result_uncrop(re);
2750
2751 return success;
2752 }
2753
RE_init_threadcount(Render * re)2754 void RE_init_threadcount(Render *re)
2755 {
2756 re->r.threads = BKE_render_num_threads(&re->r);
2757 }
2758
2759 /* loads in image into a result, size must match
2760 * x/y offsets are only used on a partial copy when dimensions don't match */
RE_layer_load_from_file(RenderLayer * layer,ReportList * reports,const char * filename,int x,int y)2761 void RE_layer_load_from_file(
2762 RenderLayer *layer, ReportList *reports, const char *filename, int x, int y)
2763 {
2764 /* OCIO_TODO: assume layer was saved in defaule color space */
2765 ImBuf *ibuf = IMB_loadiffname(filename, IB_rect, NULL);
2766 RenderPass *rpass = NULL;
2767
2768 /* multiview: since the API takes no 'view', we use the first combined pass found */
2769 for (rpass = layer->passes.first; rpass; rpass = rpass->next) {
2770 if (STREQ(rpass->name, RE_PASSNAME_COMBINED)) {
2771 break;
2772 }
2773 }
2774
2775 if (rpass == NULL) {
2776 BKE_reportf(reports,
2777 RPT_ERROR,
2778 "%s: no Combined pass found in the render layer '%s'",
2779 __func__,
2780 filename);
2781 }
2782
2783 if (ibuf && (ibuf->rect || ibuf->rect_float)) {
2784 if (ibuf->x == layer->rectx && ibuf->y == layer->recty) {
2785 if (ibuf->rect_float == NULL) {
2786 IMB_float_from_rect(ibuf);
2787 }
2788
2789 memcpy(rpass->rect, ibuf->rect_float, sizeof(float[4]) * layer->rectx * layer->recty);
2790 }
2791 else {
2792 if ((ibuf->x - x >= layer->rectx) && (ibuf->y - y >= layer->recty)) {
2793 ImBuf *ibuf_clip;
2794
2795 if (ibuf->rect_float == NULL) {
2796 IMB_float_from_rect(ibuf);
2797 }
2798
2799 ibuf_clip = IMB_allocImBuf(layer->rectx, layer->recty, 32, IB_rectfloat);
2800 if (ibuf_clip) {
2801 IMB_rectcpy(ibuf_clip, ibuf, 0, 0, x, y, layer->rectx, layer->recty);
2802
2803 memcpy(
2804 rpass->rect, ibuf_clip->rect_float, sizeof(float[4]) * layer->rectx * layer->recty);
2805 IMB_freeImBuf(ibuf_clip);
2806 }
2807 else {
2808 BKE_reportf(
2809 reports, RPT_ERROR, "%s: failed to allocate clip buffer '%s'", __func__, filename);
2810 }
2811 }
2812 else {
2813 BKE_reportf(reports,
2814 RPT_ERROR,
2815 "%s: incorrect dimensions for partial copy '%s'",
2816 __func__,
2817 filename);
2818 }
2819 }
2820
2821 IMB_freeImBuf(ibuf);
2822 }
2823 else {
2824 BKE_reportf(reports, RPT_ERROR, "%s: failed to load '%s'", __func__, filename);
2825 }
2826 }
2827
RE_result_load_from_file(RenderResult * result,ReportList * reports,const char * filename)2828 void RE_result_load_from_file(RenderResult *result, ReportList *reports, const char *filename)
2829 {
2830 if (!render_result_exr_file_read_path(result, NULL, filename)) {
2831 BKE_reportf(reports, RPT_ERROR, "%s: failed to load '%s'", __func__, filename);
2832 return;
2833 }
2834 }
2835
2836 /* Used in the interface to decide whether to show layers or passes. */
RE_layers_have_name(struct RenderResult * rr)2837 bool RE_layers_have_name(struct RenderResult *rr)
2838 {
2839 switch (BLI_listbase_count_at_most(&rr->layers, 2)) {
2840 case 0:
2841 return false;
2842 case 1:
2843 return (((RenderLayer *)rr->layers.first)->name[0] != '\0');
2844 default:
2845 return true;
2846 }
2847 return false;
2848 }
2849
RE_passes_have_name(struct RenderLayer * rl)2850 bool RE_passes_have_name(struct RenderLayer *rl)
2851 {
2852 LISTBASE_FOREACH (RenderPass *, rp, &rl->passes) {
2853 if (!STREQ(rp->name, "Combined")) {
2854 return true;
2855 }
2856 }
2857
2858 return false;
2859 }
2860
RE_pass_find_by_name(volatile RenderLayer * rl,const char * name,const char * viewname)2861 RenderPass *RE_pass_find_by_name(volatile RenderLayer *rl, const char *name, const char *viewname)
2862 {
2863 RenderPass *rp = NULL;
2864
2865 for (rp = rl->passes.last; rp; rp = rp->prev) {
2866 if (STREQ(rp->name, name)) {
2867 if (viewname == NULL || viewname[0] == '\0') {
2868 break;
2869 }
2870 if (STREQ(rp->view, viewname)) {
2871 break;
2872 }
2873 }
2874 }
2875 return rp;
2876 }
2877
2878 /* Only provided for API compatibility, don't use this in new code! */
RE_pass_find_by_type(volatile RenderLayer * rl,int passtype,const char * viewname)2879 RenderPass *RE_pass_find_by_type(volatile RenderLayer *rl, int passtype, const char *viewname)
2880 {
2881 #define CHECK_PASS(NAME) \
2882 if (passtype == SCE_PASS_##NAME) { \
2883 return RE_pass_find_by_name(rl, RE_PASSNAME_##NAME, viewname); \
2884 } \
2885 ((void)0)
2886
2887 CHECK_PASS(COMBINED);
2888 CHECK_PASS(Z);
2889 CHECK_PASS(VECTOR);
2890 CHECK_PASS(NORMAL);
2891 CHECK_PASS(UV);
2892 CHECK_PASS(EMIT);
2893 CHECK_PASS(SHADOW);
2894 CHECK_PASS(AO);
2895 CHECK_PASS(ENVIRONMENT);
2896 CHECK_PASS(INDEXOB);
2897 CHECK_PASS(INDEXMA);
2898 CHECK_PASS(MIST);
2899 CHECK_PASS(RAYHITS);
2900 CHECK_PASS(DIFFUSE_DIRECT);
2901 CHECK_PASS(DIFFUSE_INDIRECT);
2902 CHECK_PASS(DIFFUSE_COLOR);
2903 CHECK_PASS(GLOSSY_DIRECT);
2904 CHECK_PASS(GLOSSY_INDIRECT);
2905 CHECK_PASS(GLOSSY_COLOR);
2906 CHECK_PASS(TRANSM_DIRECT);
2907 CHECK_PASS(TRANSM_INDIRECT);
2908 CHECK_PASS(TRANSM_COLOR);
2909 CHECK_PASS(SUBSURFACE_DIRECT);
2910 CHECK_PASS(SUBSURFACE_INDIRECT);
2911 CHECK_PASS(SUBSURFACE_COLOR);
2912
2913 #undef CHECK_PASS
2914
2915 return NULL;
2916 }
2917
2918 /* create a renderlayer and renderpass for grease pencil layer */
RE_create_gp_pass(RenderResult * rr,const char * layername,const char * viewname)2919 RenderPass *RE_create_gp_pass(RenderResult *rr, const char *layername, const char *viewname)
2920 {
2921 RenderLayer *rl = BLI_findstring(&rr->layers, layername, offsetof(RenderLayer, name));
2922 /* only create render layer if not exist */
2923 if (!rl) {
2924 rl = MEM_callocN(sizeof(RenderLayer), layername);
2925 BLI_addtail(&rr->layers, rl);
2926 BLI_strncpy(rl->name, layername, sizeof(rl->name));
2927 rl->layflag = SCE_LAY_SOLID;
2928 rl->passflag = SCE_PASS_COMBINED;
2929 rl->rectx = rr->rectx;
2930 rl->recty = rr->recty;
2931 }
2932
2933 /* clear previous pass if exist or the new image will be over previous one*/
2934 RenderPass *rp = RE_pass_find_by_name(rl, RE_PASSNAME_COMBINED, viewname);
2935 if (rp) {
2936 if (rp->rect) {
2937 MEM_freeN(rp->rect);
2938 }
2939 BLI_freelinkN(&rl->passes, rp);
2940 }
2941 /* create a totally new pass */
2942 return render_layer_add_pass(rr, rl, 4, RE_PASSNAME_COMBINED, viewname, "RGBA");
2943 }
2944