Merge branch 'master' into blender2.8
[blender.git] / source / blender / editors / render / render_internal.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2008 Blender Foundation.
19  * All rights reserved.
20  *
21  *
22  * ***** END GPL LICENSE BLOCK *****
23  */
24
25 /** \file blender/editors/render/render_internal.c
26  *  \ingroup edrend
27  */
28
29
30 #include <math.h>
31 #include <string.h>
32 #include <stddef.h>
33
34 #include "MEM_guardedalloc.h"
35
36 #include "BLI_listbase.h"
37 #include "BLI_rect.h"
38 #include "BLI_timecode.h"
39 #include "BLI_math.h"
40 #include "BLI_threads.h"
41 #include "BLI_utildefines.h"
42
43 #include "PIL_time.h"
44
45 #include "BLT_translation.h"
46
47 #include "DNA_object_types.h"
48 #include "DNA_scene_types.h"
49 #include "DNA_view3d_types.h"
50 #include "DNA_userdef_types.h"
51
52 #include "BKE_blender_undo.h"
53 #include "BKE_blender_version.h"
54 #include "BKE_camera.h"
55 #include "BKE_context.h"
56 #include "BKE_colortools.h"
57 #include "BKE_global.h"
58 #include "BKE_image.h"
59 #include "BKE_layer.h"
60 #include "BKE_library.h"
61 #include "BKE_main.h"
62 #include "BKE_node.h"
63 #include "BKE_object.h"
64 #include "BKE_report.h"
65 #include "BKE_sequencer.h"
66 #include "BKE_screen.h"
67 #include "BKE_scene.h"
68 #include "BKE_undo_system.h"
69 #include "BKE_workspace.h"
70
71 #include "DEG_depsgraph.h"
72
73 #include "WM_api.h"
74 #include "WM_types.h"
75
76 #include "ED_object.h"
77 #include "ED_render.h"
78 #include "ED_screen.h"
79 #include "ED_util.h"
80 #include "ED_undo.h"
81 #include "ED_view3d.h"
82
83 #include "RE_pipeline.h"
84 #include "RE_engine.h"
85
86 #include "IMB_colormanagement.h"
87 #include "IMB_imbuf_types.h"
88
89 #include "GPU_shader.h"
90
91 #include "BIF_gl.h"
92 #include "BIF_glutil.h"
93
94 #include "RNA_access.h"
95 #include "RNA_define.h"
96
97 #include "BLO_undofile.h"
98
99 #include "render_intern.h"
100
101 /* Render Callbacks */
102 static int render_break(void *rjv);
103
104 typedef struct RenderJob {
105         Main *main;
106         Scene *scene;
107         ViewLayer *single_layer;
108         Scene *current_scene;
109         /* TODO(sergey): Should not be needed once engine will have own
110          * depsgraph and copy-on-write will be implemented.
111          */
112         Depsgraph *depsgraph;
113         Render *re;
114         struct Object *camera_override;
115         int lay_override;
116         bool v3d_override;
117         bool anim, write_still;
118         Image *image;
119         ImageUser iuser;
120         bool image_outdated;
121         short *stop;
122         short *do_update;
123         float *progress;
124         ReportList *reports;
125         int orig_layer;
126         int last_layer;
127         ScrArea *sa;
128         ColorManagedViewSettings view_settings;
129         ColorManagedDisplaySettings display_settings;
130         bool supports_glsl_draw;
131         bool interface_locked;
132 } RenderJob;
133
134 /* called inside thread! */
135 static void image_buffer_rect_update(RenderJob *rj, RenderResult *rr, ImBuf *ibuf, ImageUser *iuser, volatile rcti *renrect, const char *viewname)
136 {
137         Scene *scene = rj->scene;
138         const float *rectf = NULL;
139         int ymin, ymax, xmin, xmax;
140         int rymin, rxmin;
141         int linear_stride, linear_offset_x, linear_offset_y;
142         ColorManagedViewSettings *view_settings;
143         ColorManagedDisplaySettings *display_settings;
144
145         /* Exception for exr tiles -- display buffer conversion happens here,
146          * NOT in the color management pipeline.
147          */
148         if (ibuf->userflags & IB_DISPLAY_BUFFER_INVALID &&
149             rr->do_exr_tile == false)
150         {
151                 /* The whole image buffer it so be color managed again anyway. */
152                 return;
153         }
154
155         /* if renrect argument, we only refresh scanlines */
156         if (renrect) {
157                 /* if (ymax == recty), rendering of layer is ready, we should not draw, other things happen... */
158                 if (rr->renlay == NULL || renrect->ymax >= rr->recty)
159                         return;
160
161                 /* xmin here is first subrect x coord, xmax defines subrect width */
162                 xmin = renrect->xmin + rr->crop;
163                 xmax = renrect->xmax - xmin + rr->crop;
164                 if (xmax < 2)
165                         return;
166
167                 ymin = renrect->ymin + rr->crop;
168                 ymax = renrect->ymax - ymin + rr->crop;
169                 if (ymax < 2)
170                         return;
171                 renrect->ymin = renrect->ymax;
172
173         }
174         else {
175                 xmin = ymin = rr->crop;
176                 xmax = rr->rectx - 2 * rr->crop;
177                 ymax = rr->recty - 2 * rr->crop;
178         }
179
180         /* xmin ymin is in tile coords. transform to ibuf */
181         rxmin = rr->tilerect.xmin + xmin;
182         if (rxmin >= ibuf->x) return;
183         rymin = rr->tilerect.ymin + ymin;
184         if (rymin >= ibuf->y) return;
185
186         if (rxmin + xmax > ibuf->x)
187                 xmax = ibuf->x - rxmin;
188         if (rymin + ymax > ibuf->y)
189                 ymax = ibuf->y - rymin;
190
191         if (xmax < 1 || ymax < 1) return;
192
193         /* The thing here is, the logic below (which was default behavior
194          * of how rectf is acquiring since forever) gives float buffer for
195          * composite output only. This buffer can not be used for other
196          * passes obviously.
197          *
198          * We might try finding corresponding for pass buffer in render result
199          * (which is actually missing when rendering with Cycles, who only
200          * writes all the passes when the tile is finished) or use float
201          * buffer from image buffer as reference, which is easier to use and
202          * contains all the data we need anyway.
203          *                                              - sergey -
204          */
205         /* TODO(sergey): Need to check has_combined here? */
206         if (iuser->pass == 0) {
207                 RenderView *rv;
208                 const int view_id = BKE_scene_multiview_view_id_get(&scene->r, viewname);
209                 rv = RE_RenderViewGetById(rr, view_id);
210
211                 /* find current float rect for display, first case is after composite... still weak */
212                 if (rv->rectf)
213                         rectf = rv->rectf;
214                 else {
215                         if (rv->rect32) {
216                                 /* special case, currently only happens with sequencer rendering,
217                                  * which updates the whole frame, so we can only mark display buffer
218                                  * as invalid here (sergey)
219                                  */
220                                 ibuf->userflags |= IB_DISPLAY_BUFFER_INVALID;
221                                 return;
222                         }
223                         else {
224                                 if (rr->renlay == NULL) return;
225                                 rectf = RE_RenderLayerGetPass(rr->renlay, RE_PASSNAME_COMBINED, viewname);
226                         }
227                 }
228                 if (rectf == NULL) return;
229
230                 rectf += 4 * (rr->rectx * ymin + xmin);
231                 linear_stride = rr->rectx;
232                 linear_offset_x = rxmin;
233                 linear_offset_y = rymin;
234         }
235         else {
236                 rectf = ibuf->rect_float;
237                 linear_stride = ibuf->x;
238                 linear_offset_x = 0;
239                 linear_offset_y = 0;
240         }
241
242         if (rr->do_exr_tile) {
243                 /* We don't support changing color management settings during rendering
244                  * when using Save Buffers option.
245                  */
246                 view_settings = &rj->view_settings;
247                 display_settings = &rj->display_settings;
248         }
249         else {
250                 view_settings = &scene->view_settings;
251                 display_settings = &scene->display_settings;
252         }
253
254         IMB_partial_display_buffer_update(ibuf, rectf, NULL,
255                                           linear_stride, linear_offset_x, linear_offset_y,
256                                           view_settings, display_settings,
257                                           rxmin, rymin, rxmin + xmax, rymin + ymax,
258                                           rr->do_exr_tile);
259 }
260
261 /* ****************************** render invoking ***************** */
262
263 /* set callbacks, exported to sequence render too.
264  * Only call in foreground (UI) renders. */
265
266 static void screen_render_single_layer_set(wmOperator *op, Main *mainp, WorkSpace *workspace, Scene **scene, ViewLayer **single_layer)
267 {
268         /* single layer re-render */
269         if (RNA_struct_property_is_set(op->ptr, "scene")) {
270                 Scene *scn;
271                 char scene_name[MAX_ID_NAME - 2];
272
273                 RNA_string_get(op->ptr, "scene", scene_name);
274                 scn = (Scene *)BLI_findstring(&mainp->scene, scene_name, offsetof(ID, name) + 2);
275
276                 if (scn) {
277                         /* camera switch wont have updated */
278                         scn->r.cfra = (*scene)->r.cfra;
279                         BKE_scene_camera_switch_update(scn);
280
281                         *scene = scn;
282                 }
283         }
284
285         if (RNA_struct_property_is_set(op->ptr, "layer")) {
286                 ViewLayer *rl;
287                 char rl_name[RE_MAXNAME];
288
289                 RNA_string_get(op->ptr, "layer", rl_name);
290                 rl = (ViewLayer *)BLI_findstring(&(*scene)->view_layers, rl_name, offsetof(ViewLayer, name));
291
292                 if (rl)
293                         *single_layer = rl;
294         }
295         else if (((*scene)->r.scemode & R_SINGLE_LAYER) && workspace) {
296                 *single_layer = BKE_view_layer_from_workspace_get(*scene, workspace);
297         }
298 }
299
300 /* executes blocking render */
301 static int screen_render_exec(bContext *C, wmOperator *op)
302 {
303         Scene *scene = CTX_data_scene(C);
304         RenderEngineType *re_type = RE_engines_find(scene->r.engine);
305         ViewLayer *single_layer = NULL;
306         Render *re;
307         Image *ima;
308         View3D *v3d = CTX_wm_view3d(C);
309         Main *mainp = CTX_data_main(C);
310         WorkSpace *workspace = CTX_wm_workspace(C);
311         unsigned int lay_override;
312         const bool is_animation = RNA_boolean_get(op->ptr, "animation");
313         const bool is_write_still = RNA_boolean_get(op->ptr, "write_still");
314         struct Object *camera_override = v3d ? V3D_CAMERA_LOCAL(v3d) : NULL;
315
316         /* Cannot do render if there is not this function. */
317         if (re_type->render == NULL) {
318                 return OPERATOR_CANCELLED;
319         }
320
321         /* custom scene and single layer re-render */
322         screen_render_single_layer_set(op, mainp, workspace, &scene, &single_layer);
323
324         if (!is_animation && is_write_still && BKE_imtype_is_movie(scene->r.im_format.imtype)) {
325                 BKE_report(op->reports, RPT_ERROR, "Cannot write a single file with an animation format selected");
326                 return OPERATOR_CANCELLED;
327         }
328
329         re = RE_NewSceneRender(scene);
330         lay_override = (v3d && v3d->lay != scene->lay) ? v3d->lay : 0;
331
332         G.is_break = false;
333         RE_test_break_cb(re, NULL, render_break);
334
335         ima = BKE_image_verify_viewer(IMA_TYPE_R_RESULT, "Render Result");
336         BKE_image_signal(ima, NULL, IMA_SIGNAL_FREE);
337         BKE_image_backup_render(scene, ima, true);
338
339         /* cleanup sequencer caches before starting user triggered render.
340          * otherwise, invalidated cache entries can make their way into
341          * the output rendering. We can't put that into RE_BlenderFrame,
342          * since sequence rendering can call that recursively... (peter) */
343         BKE_sequencer_cache_cleanup();
344
345         RE_SetReports(re, op->reports);
346
347         BLI_threaded_malloc_begin();
348         if (is_animation)
349                 RE_BlenderAnim(re, mainp, scene, camera_override, lay_override, scene->r.sfra, scene->r.efra, scene->r.frame_step);
350         else
351                 RE_BlenderFrame(re, mainp, scene, single_layer, camera_override, lay_override, scene->r.cfra, is_write_still);
352         BLI_threaded_malloc_end();
353
354         RE_SetReports(re, NULL);
355
356         // no redraw needed, we leave state as we entered it
357         ED_update_for_newframe(mainp, CTX_data_depsgraph(C));
358
359         WM_event_add_notifier(C, NC_SCENE | ND_RENDER_RESULT, scene);
360
361         return OPERATOR_FINISHED;
362 }
363
364 static void render_freejob(void *rjv)
365 {
366         RenderJob *rj = rjv;
367
368         BKE_color_managed_view_settings_free(&rj->view_settings);
369         MEM_freeN(rj);
370 }
371
372 /* str is IMA_MAX_RENDER_TEXT in size */
373 static void make_renderinfo_string(const RenderStats *rs,
374                                    const Scene *scene,
375                                    const bool v3d_override,
376                                    const char *error,
377                                    char *str)
378 {
379         char info_time_str[32]; // used to be extern to header_info.c
380         uintptr_t mem_in_use, mmap_in_use, peak_memory;
381         float megs_used_memory, mmap_used_memory, megs_peak_memory;
382         char *spos = str;
383
384         mem_in_use = MEM_get_memory_in_use();
385         mmap_in_use = MEM_get_mapped_memory_in_use();
386         peak_memory = MEM_get_peak_memory();
387
388         megs_used_memory = (mem_in_use - mmap_in_use) / (1024.0 * 1024.0);
389         mmap_used_memory = (mmap_in_use) / (1024.0 * 1024.0);
390         megs_peak_memory = (peak_memory) / (1024.0 * 1024.0);
391
392         /* local view */
393         if (rs->localview)
394                 spos += sprintf(spos, "%s | ", IFACE_("3D Local View"));
395         else if (v3d_override)
396                 spos += sprintf(spos, "%s | ", IFACE_("3D View"));
397
398         /* frame number */
399         spos += sprintf(spos, IFACE_("Frame:%d "), (scene->r.cfra));
400
401         /* previous and elapsed time */
402         BLI_timecode_string_from_time_simple(info_time_str, sizeof(info_time_str), rs->lastframetime);
403
404         if (rs->infostr && rs->infostr[0]) {
405                 if (rs->lastframetime != 0.0)
406                         spos += sprintf(spos, IFACE_("| Last:%s "), info_time_str);
407                 else
408                         spos += sprintf(spos, "| ");
409
410                 BLI_timecode_string_from_time_simple(info_time_str, sizeof(info_time_str), PIL_check_seconds_timer() - rs->starttime);
411         }
412         else
413                 spos += sprintf(spos, "| ");
414
415         spos += sprintf(spos, IFACE_("Time:%s "), info_time_str);
416
417         /* statistics */
418         if (rs->statstr) {
419                 if (rs->statstr[0]) {
420                         spos += sprintf(spos, "| %s ", rs->statstr);
421                 }
422         }
423         else {
424                 if (rs->totvert || rs->totface || rs->tothalo || rs->totstrand || rs->totlamp)
425                         spos += sprintf(spos, "| ");
426
427                 if (rs->totvert) spos += sprintf(spos, IFACE_("Ve:%d "), rs->totvert);
428                 if (rs->totface) spos += sprintf(spos, IFACE_("Fa:%d "), rs->totface);
429                 if (rs->tothalo) spos += sprintf(spos, IFACE_("Ha:%d "), rs->tothalo);
430                 if (rs->totstrand) spos += sprintf(spos, IFACE_("St:%d "), rs->totstrand);
431                 if (rs->totlamp) spos += sprintf(spos, IFACE_("La:%d "), rs->totlamp);
432
433                 if (rs->mem_peak == 0.0f)
434                         spos += sprintf(spos, IFACE_("| Mem:%.2fM (%.2fM, Peak %.2fM) "),
435                                         megs_used_memory, mmap_used_memory, megs_peak_memory);
436                 else
437                         spos += sprintf(spos, IFACE_("| Mem:%.2fM, Peak: %.2fM "), rs->mem_used, rs->mem_peak);
438
439                 if (rs->curfield)
440                         spos += sprintf(spos, IFACE_("Field %d "), rs->curfield);
441                 if (rs->curblur)
442                         spos += sprintf(spos, IFACE_("Blur %d "), rs->curblur);
443         }
444
445         /* full sample */
446         if (rs->curfsa)
447                 spos += sprintf(spos, IFACE_("| Full Sample %d "), rs->curfsa);
448
449         /* extra info */
450         if (rs->infostr && rs->infostr[0]) {
451                 spos += sprintf(spos, "| %s ", rs->infostr);
452         }
453         else if (error && error[0]) {
454                 spos += sprintf(spos, "| %s ", error);
455         }
456
457         /* very weak... but 512 characters is quite safe */
458         if (spos >= str + IMA_MAX_RENDER_TEXT)
459                 if (G.debug & G_DEBUG)
460                         printf("WARNING! renderwin text beyond limit\n");
461
462 }
463
464 static void image_renderinfo_cb(void *rjv, RenderStats *rs)
465 {
466         RenderJob *rj = rjv;
467         RenderResult *rr;
468
469         rr = RE_AcquireResultRead(rj->re);
470
471         if (rr) {
472                 /* malloc OK here, stats_draw is not in tile threads */
473                 if (rr->text == NULL)
474                         rr->text = MEM_callocN(IMA_MAX_RENDER_TEXT, "rendertext");
475
476                 make_renderinfo_string(rs, rj->scene, rj->v3d_override,
477                                        rr->error, rr->text);
478         }
479
480         RE_ReleaseResult(rj->re);
481
482         /* make jobs timer to send notifier */
483         *(rj->do_update) = true;
484
485 }
486
487 static void render_progress_update(void *rjv, float progress)
488 {
489         RenderJob *rj = rjv;
490
491         if (rj->progress && *rj->progress != progress) {
492                 *rj->progress = progress;
493
494                 /* make jobs timer to send notifier */
495                 *(rj->do_update) = true;
496         }
497 }
498
499 /* Not totally reliable, but works fine in most of cases and
500  * in worst case would just make it so extra color management
501  * for the whole render result is applied (which was already
502  * happening already).
503  */
504 static void render_image_update_pass_and_layer(RenderJob *rj, RenderResult *rr, ImageUser *iuser)
505 {
506         wmWindowManager *wm;
507         ScrArea *first_sa = NULL, *matched_sa = NULL;
508
509         /* image window, compo node users */
510         for (wm = rj->main->wm.first; wm && matched_sa == NULL; wm = wm->id.next) { /* only 1 wm */
511                 wmWindow *win;
512                 for (win = wm->windows.first; win && matched_sa == NULL; win = win->next) {
513                         const bScreen *screen = WM_window_get_active_screen(win);
514
515                         for (ScrArea *sa = screen->areabase.first; sa; sa = sa->next) {
516                                 if (sa->spacetype == SPACE_IMAGE) {
517                                         SpaceImage *sima = sa->spacedata.first;
518                                         // sa->spacedata might be empty when toggling fullscreen mode.
519                                         if (sima != NULL && sima->image == rj->image) {
520                                                 if (first_sa == NULL) {
521                                                         first_sa = sa;
522                                                 }
523                                                 if (sa == rj->sa) {
524                                                         matched_sa = sa;
525                                                         break;
526                                                 }
527                                         }
528                                 }
529                         }
530                 }
531         }
532
533         if (matched_sa == NULL) {
534                 matched_sa = first_sa;
535         }
536
537         if (matched_sa) {
538                 SpaceImage *sima = matched_sa->spacedata.first;
539                 RenderResult *main_rr = RE_AcquireResultRead(rj->re);
540
541                 /* TODO(sergey): is there faster way to get the layer index? */
542                 if (rr->renlay) {
543                         int layer = BLI_findstringindex(&main_rr->layers,
544                                                         (char *)rr->renlay->name,
545                                                         offsetof(RenderLayer, name));
546                         sima->iuser.layer = layer;
547                         rj->last_layer = layer;
548                 }
549
550                 iuser->pass = sima->iuser.pass;
551                 iuser->layer = sima->iuser.layer;
552
553                 RE_ReleaseResult(rj->re);
554         }
555 }
556
557 static void image_rect_update(void *rjv, RenderResult *rr, volatile rcti *renrect)
558 {
559         RenderJob *rj = rjv;
560         Image *ima = rj->image;
561         ImBuf *ibuf;
562         void *lock;
563         const char *viewname = RE_GetActiveRenderView(rj->re);
564
565         /* only update if we are displaying the slot being rendered */
566         if (ima->render_slot != ima->last_render_slot) {
567                 rj->image_outdated = true;
568                 return;
569         }
570         else if (rj->image_outdated) {
571                 /* update entire render */
572                 rj->image_outdated = false;
573                 BKE_image_signal(ima, NULL, IMA_SIGNAL_COLORMANAGE);
574                 *(rj->do_update) = true;
575                 return;
576         }
577
578         if (rr == NULL)
579                 return;
580
581         /* update part of render */
582         render_image_update_pass_and_layer(rj, rr, &rj->iuser);
583         ibuf = BKE_image_acquire_ibuf(ima, &rj->iuser, &lock);
584         if (ibuf) {
585                 /* Don't waste time on CPU side color management if
586                  * image will be displayed using GLSL.
587                  *
588                  * Need to update rect if Save Buffers enabled because in
589                  * this case GLSL doesn't have original float buffer to
590                  * operate with.
591                  */
592                 if (rr->do_exr_tile ||
593                     !rj->supports_glsl_draw ||
594                     ibuf->channels == 1 ||
595                     U.image_draw_method != IMAGE_DRAW_METHOD_GLSL)
596                 {
597                         image_buffer_rect_update(rj, rr, ibuf, &rj->iuser, renrect, viewname);
598                 }
599
600                 /* make jobs timer to send notifier */
601                 *(rj->do_update) = true;
602         }
603         BKE_image_release_ibuf(ima, ibuf, lock);
604 }
605
606 static void current_scene_update(void *rjv, Scene *scene)
607 {
608         RenderJob *rj = rjv;
609         rj->current_scene = scene;
610         rj->iuser.scene = scene;
611 }
612
613 static void render_startjob(void *rjv, short *stop, short *do_update, float *progress)
614 {
615         RenderJob *rj = rjv;
616
617         rj->stop = stop;
618         rj->do_update = do_update;
619         rj->progress = progress;
620
621         RE_SetReports(rj->re, rj->reports);
622
623         if (rj->anim)
624                 RE_BlenderAnim(rj->re, rj->main, rj->scene, rj->camera_override, rj->lay_override, rj->scene->r.sfra, rj->scene->r.efra, rj->scene->r.frame_step);
625         else
626                 RE_BlenderFrame(rj->re, rj->main, rj->scene, rj->single_layer, rj->camera_override, rj->lay_override, rj->scene->r.cfra, rj->write_still);
627
628         RE_SetReports(rj->re, NULL);
629 }
630
631 static void render_image_restore_layer(RenderJob *rj)
632 {
633         wmWindowManager *wm;
634
635         /* image window, compo node users */
636         for (wm = rj->main->wm.first; wm; wm = wm->id.next) { /* only 1 wm */
637                 wmWindow *win;
638                 for (win = wm->windows.first; win; win = win->next) {
639                         const bScreen *screen = WM_window_get_active_screen(win);
640
641                         for (ScrArea *sa = screen->areabase.first; sa; sa = sa->next) {
642                                 if (sa == rj->sa) {
643                                         if (sa->spacetype == SPACE_IMAGE) {
644                                                 SpaceImage *sima = sa->spacedata.first;
645
646                                                 if (RE_HasSingleLayer(rj->re)) {
647                                                         /* For single layer renders keep the active layer
648                                                          * visible, or show the compositing result. */
649                                                         RenderResult *rr = RE_AcquireResultRead(rj->re);
650                                                         if (RE_HasCombinedLayer(rr)) {
651                                                                 sima->iuser.layer = 0;
652                                                         }
653                                                         RE_ReleaseResult(rj->re);
654                                                 }
655                                                 else {
656                                                         /* For multiple layer render, set back the layer
657                                                          * that was set at the start of rendering. */
658                                                         sima->iuser.layer = rj->orig_layer;
659                                                 }
660                                         }
661                                         return;
662                                 }
663                         }
664                 }
665         }
666 }
667
668 static void render_endjob(void *rjv)
669 {
670         RenderJob *rj = rjv;
671
672         /* this render may be used again by the sequencer without the active 'Render' where the callbacks
673          * would be re-assigned. assign dummy callbacks to avoid referencing freed renderjobs bug [#24508] */
674         RE_InitRenderCB(rj->re);
675
676         if (rj->main != G.main)
677                 BKE_main_free(rj->main);
678
679         /* else the frame will not update for the original value */
680         if (rj->anim && !(rj->scene->r.scemode & R_NO_FRAME_UPDATE)) {
681                 /* possible this fails of loading new file while rendering */
682                 if (G.main->wm.first) {
683                         ED_update_for_newframe(G.main, rj->depsgraph);
684                 }
685         }
686
687         /* XXX above function sets all tags in nodes */
688         ntreeCompositClearTags(rj->scene->nodetree);
689
690         /* potentially set by caller */
691         rj->scene->r.scemode &= ~R_NO_FRAME_UPDATE;
692
693         if (rj->single_layer) {
694                 nodeUpdateID(rj->scene->nodetree, &rj->scene->id);
695                 WM_main_add_notifier(NC_NODE | NA_EDITED, rj->scene);
696         }
697
698         if (rj->sa) {
699                 render_image_restore_layer(rj);
700         }
701
702         /* XXX render stability hack */
703         G.is_rendering = false;
704         WM_main_add_notifier(NC_SCENE | ND_RENDER_RESULT, NULL);
705
706         /* Partial render result will always update display buffer
707          * for first render layer only. This is nice because you'll
708          * see render progress during rendering, but it ends up in
709          * wrong display buffer shown after rendering.
710          *
711          * The code below will mark display buffer as invalid after
712          * rendering in case multiple layers were rendered, which
713          * ensures display buffer matches render layer after
714          * rendering.
715          *
716          * Perhaps proper way would be to toggle active render
717          * layer in image editor and job, so we always display
718          * layer being currently rendered. But this is not so much
719          * trivial at this moment, especially because of external
720          * engine API, so lets use simple and robust way for now
721          *                                          - sergey -
722          */
723         if (rj->scene->view_layers.first != rj->scene->view_layers.last ||
724             rj->image_outdated)
725         {
726                 void *lock;
727                 Image *ima = rj->image;
728                 ImBuf *ibuf = BKE_image_acquire_ibuf(ima, &rj->iuser, &lock);
729
730                 if (ibuf)
731                         ibuf->userflags |= IB_DISPLAY_BUFFER_INVALID;
732
733                 BKE_image_release_ibuf(ima, ibuf, lock);
734         }
735
736         /* Finally unlock the user interface (if it was locked). */
737         if (rj->interface_locked) {
738                 Scene *scene;
739
740                 /* Interface was locked, so window manager couldn't have been changed
741                  * and using one from Global will unlock exactly the same manager as
742                  * was locked before running the job.
743                  */
744                 WM_set_locked_interface(G.main->wm.first, false);
745
746                 /* We've freed all the derived caches before rendering, which is
747                  * effectively the same as if we re-loaded the file.
748                  *
749                  * So let's not try being smart here and just reset all updated
750                  * scene layers and use generic DAG_on_visible_update.
751                  */
752                 for (scene = G.main->scene.first; scene; scene = scene->id.next) {
753                         scene->lay_updated = 0;
754                 }
755
756                 DEG_on_visible_update(G.main, false);
757         }
758 }
759
760 /* called by render, check job 'stop' value or the global */
761 static int render_breakjob(void *rjv)
762 {
763         RenderJob *rj = rjv;
764
765         if (G.is_break)
766                 return 1;
767         if (rj->stop && *(rj->stop))
768                 return 1;
769         return 0;
770 }
771
772 /* for exec() when there is no render job
773  * note: this wont check for the escape key being pressed, but doing so isnt threadsafe */
774 static int render_break(void *UNUSED(rjv))
775 {
776         if (G.is_break)
777                 return 1;
778         return 0;
779 }
780
781 /* runs in thread, no cursor setting here works. careful with notifiers too (malloc conflicts) */
782 /* maybe need a way to get job send notifer? */
783 static void render_drawlock(void *rjv, int lock)
784 {
785         RenderJob *rj = rjv;
786
787         /* If interface is locked, renderer callback shall do nothing. */
788         if (!rj->interface_locked) {
789                 BKE_spacedata_draw_locks(lock);
790         }
791 }
792
793 /* catch esc */
794 static int screen_render_modal(bContext *C, wmOperator *op, const wmEvent *event)
795 {
796         Scene *scene = (Scene *) op->customdata;
797
798         /* no running blender, remove handler and pass through */
799         if (0 == WM_jobs_test(CTX_wm_manager(C), scene, WM_JOB_TYPE_RENDER)) {
800                 return OPERATOR_FINISHED | OPERATOR_PASS_THROUGH;
801         }
802
803         /* running render */
804         switch (event->type) {
805                 case ESCKEY:
806                         return OPERATOR_RUNNING_MODAL;
807         }
808         return OPERATOR_PASS_THROUGH;
809 }
810
811 static void screen_render_cancel(bContext *C, wmOperator *op)
812 {
813         wmWindowManager *wm = CTX_wm_manager(C);
814         Scene *scene = (Scene *) op->customdata;
815
816         /* kill on cancel, because job is using op->reports */
817         WM_jobs_kill_type(wm, scene, WM_JOB_TYPE_RENDER);
818 }
819
820 static void clean_viewport_memory_base(Base *base)
821 {
822         if ((base->flag & BASE_VISIBLED) == 0) {
823                 return;
824         }
825
826         Object *object = base->object;
827
828         if (object->id.tag & LIB_TAG_DOIT) {
829                 return;
830         }
831
832         object->id.tag &= ~LIB_TAG_DOIT;
833         if (RE_allow_render_generic_object(object)) {
834                 BKE_object_free_derived_caches(object);
835         }
836 }
837
838 static void clean_viewport_memory(Main *bmain, Scene *scene)
839 {
840         Scene *sce_iter;
841         Base *base;
842
843         /* Tag all the available objects. */
844         BKE_main_id_tag_listbase(&bmain->object, LIB_TAG_DOIT, true);
845
846         /* Go over all the visible objects. */
847         for (wmWindowManager *wm = bmain->wm.first; wm; wm = wm->id.next) {
848                 for (wmWindow *win = wm->windows.first; win; win = win->next) {
849                         WorkSpace *workspace = BKE_workspace_active_get(win->workspace_hook);
850                         ViewLayer *view_layer = BKE_view_layer_from_workspace_get(scene, workspace);
851
852                         for (base = view_layer->object_bases.first; base; base = base->next) {
853                                 clean_viewport_memory_base(base);
854                         }
855                 }
856         }
857
858         for (SETLOOPER_SET_ONLY(scene, sce_iter, base)) {
859                 clean_viewport_memory_base(base);
860         }
861 }
862
863 /* using context, starts job */
864 static int screen_render_invoke(bContext *C, wmOperator *op, const wmEvent *event)
865 {
866         /* new render clears all callbacks */
867         Main *bmain = CTX_data_main(C);
868         ViewLayer *single_layer = NULL;
869         Scene *scene = CTX_data_scene(C);
870         RenderEngineType *re_type = RE_engines_find(scene->r.engine);
871         Render *re;
872         wmJob *wm_job;
873         RenderJob *rj;
874         Image *ima;
875         int jobflag;
876         const bool is_animation = RNA_boolean_get(op->ptr, "animation");
877         const bool is_write_still = RNA_boolean_get(op->ptr, "write_still");
878         const bool use_viewport = RNA_boolean_get(op->ptr, "use_viewport");
879         View3D *v3d = use_viewport ? CTX_wm_view3d(C) : NULL;
880         WorkSpace *workspace = CTX_wm_workspace(C);
881         struct Object *camera_override = v3d ? V3D_CAMERA_LOCAL(v3d) : NULL;
882         const char *name;
883         ScrArea *sa;
884
885         /* Cannot do render if there is not this function. */
886         if (re_type->render == NULL) {
887                 return OPERATOR_CANCELLED;
888         }
889
890         /* custom scene and single layer re-render */
891         screen_render_single_layer_set(op, bmain, workspace, &scene, &single_layer);
892
893         /* only one render job at a time */
894         if (WM_jobs_test(CTX_wm_manager(C), scene, WM_JOB_TYPE_RENDER))
895                 return OPERATOR_CANCELLED;
896
897         if (!RE_is_rendering_allowed(scene, single_layer, camera_override, op->reports)) {
898                 return OPERATOR_CANCELLED;
899         }
900
901         if (!is_animation && is_write_still && BKE_imtype_is_movie(scene->r.im_format.imtype)) {
902                 BKE_report(op->reports, RPT_ERROR, "Cannot write a single file with an animation format selected");
903                 return OPERATOR_CANCELLED;
904         }
905
906         /* stop all running jobs, except screen one. currently previews frustrate Render */
907         WM_jobs_kill_all_except(CTX_wm_manager(C), CTX_wm_screen(C));
908
909         /* cancel animation playback */
910         if (ED_screen_animation_playing(CTX_wm_manager(C)))
911                 ED_screen_animation_play(C, 0, 0);
912
913         /* handle UI stuff */
914         WM_cursor_wait(1);
915
916         /* flush sculpt and editmode changes */
917         ED_editors_flush_edits(C, true);
918
919         /* cleanup sequencer caches before starting user triggered render.
920          * otherwise, invalidated cache entries can make their way into
921          * the output rendering. We can't put that into RE_BlenderFrame,
922          * since sequence rendering can call that recursively... (peter) */
923         BKE_sequencer_cache_cleanup();
924
925         // store spare
926         // get view3d layer, local layer, make this nice api call to render
927         // store spare
928
929         /* ensure at least 1 area shows result */
930         sa = render_view_open(C, event->x, event->y, op->reports);
931
932         jobflag = WM_JOB_EXCL_RENDER | WM_JOB_PRIORITY | WM_JOB_PROGRESS;
933
934         if (RNA_struct_property_is_set(op->ptr, "layer"))
935                 jobflag |= WM_JOB_SUSPEND;
936
937         /* job custom data */
938         rj = MEM_callocN(sizeof(RenderJob), "render job");
939         rj->main = bmain;
940         rj->scene = scene;
941         rj->current_scene = rj->scene;
942         rj->single_layer = single_layer;
943         /* TODO(sergey): Render engine should be using own depsgraph. */
944         rj->depsgraph = CTX_data_depsgraph(C);
945         rj->camera_override = camera_override;
946         rj->lay_override = 0;
947         rj->anim = is_animation;
948         rj->write_still = is_write_still && !is_animation;
949         rj->iuser.scene = scene;
950         rj->iuser.ok = 1;
951         rj->reports = op->reports;
952         rj->orig_layer = 0;
953         rj->last_layer = 0;
954         rj->sa = sa;
955         rj->supports_glsl_draw = IMB_colormanagement_support_glsl_draw(&scene->view_settings);
956
957         BKE_color_managed_display_settings_copy(&rj->display_settings, &scene->display_settings);
958         BKE_color_managed_view_settings_copy(&rj->view_settings, &scene->view_settings);
959
960         if (sa) {
961                 SpaceImage *sima = sa->spacedata.first;
962                 rj->orig_layer = sima->iuser.layer;
963         }
964
965         if (v3d) {
966                 if (scene->lay != v3d->lay) {
967                         rj->lay_override = v3d->lay;
968                         rj->v3d_override = true;
969                 }
970                 else if (camera_override && camera_override != scene->camera)
971                         rj->v3d_override = true;
972
973                 if (v3d->localvd)
974                         rj->lay_override |= v3d->localvd->lay;
975         }
976
977         /* Lock the user interface depending on render settings. */
978         if (scene->r.use_lock_interface) {
979                 WM_set_locked_interface(CTX_wm_manager(C), true);
980
981                 /* Set flag interface need to be unlocked.
982                  *
983                  * This is so because we don't have copy of render settings
984                  * accessible from render job and copy is needed in case
985                  * of non-locked rendering, so we wouldn't try to unlock
986                  * anything if option was initially unset but then was
987                  * enabled during rendering.
988                  */
989                 rj->interface_locked = true;
990
991                 /* Clean memory used by viewport? */
992                 clean_viewport_memory(rj->main, scene);
993         }
994
995         /* setup job */
996         if (RE_seq_render_active(scene, &scene->r)) name = "Sequence Render";
997         else name = "Render";
998
999         wm_job = WM_jobs_get(CTX_wm_manager(C), CTX_wm_window(C), scene, name, jobflag, WM_JOB_TYPE_RENDER);
1000         WM_jobs_customdata_set(wm_job, rj, render_freejob);
1001         WM_jobs_timer(wm_job, 0.2, NC_SCENE | ND_RENDER_RESULT, 0);
1002         WM_jobs_callbacks(wm_job, render_startjob, NULL, NULL, render_endjob);
1003
1004         /* get a render result image, and make sure it is empty */
1005         ima = BKE_image_verify_viewer(IMA_TYPE_R_RESULT, "Render Result");
1006         BKE_image_signal(ima, NULL, IMA_SIGNAL_FREE);
1007         BKE_image_backup_render(rj->scene, ima, true);
1008         rj->image = ima;
1009
1010         /* setup new render */
1011         re = RE_NewSceneRender(scene);
1012         RE_test_break_cb(re, rj, render_breakjob);
1013         RE_draw_lock_cb(re, rj, render_drawlock);
1014         RE_display_update_cb(re, rj, image_rect_update);
1015         RE_current_scene_update_cb(re, rj, current_scene_update);
1016         RE_stats_draw_cb(re, rj, image_renderinfo_cb);
1017         RE_progress_cb(re, rj, render_progress_update);
1018
1019         rj->re = re;
1020         G.is_break = false;
1021
1022         /* store actual owner of job, so modal operator could check for it,
1023          * the reason of this is that active scene could change when rendering
1024          * several layers from compositor [#31800]
1025          */
1026         op->customdata = scene;
1027
1028         WM_jobs_start(CTX_wm_manager(C), wm_job);
1029
1030         WM_cursor_wait(0);
1031         WM_event_add_notifier(C, NC_SCENE | ND_RENDER_RESULT, scene);
1032
1033         /* we set G.is_rendering here already instead of only in the job, this ensure
1034          * main loop or other scene updates are disabled in time, since they may
1035          * have started before the job thread */
1036         G.is_rendering = true;
1037
1038         /* add modal handler for ESC */
1039         WM_event_add_modal_handler(C, op);
1040
1041         return OPERATOR_RUNNING_MODAL;
1042 }
1043
1044 /* contextual render, using current scene, view3d? */
1045 void RENDER_OT_render(wmOperatorType *ot)
1046 {
1047         PropertyRNA *prop;
1048
1049         /* identifiers */
1050         ot->name = "Render";
1051         ot->description = "Render active scene";
1052         ot->idname = "RENDER_OT_render";
1053
1054         /* api callbacks */
1055         ot->invoke = screen_render_invoke;
1056         ot->modal = screen_render_modal;
1057         ot->cancel = screen_render_cancel;
1058         ot->exec = screen_render_exec;
1059
1060         /*ot->poll = ED_operator_screenactive;*/ /* this isn't needed, causes failer in background mode */
1061
1062         prop = RNA_def_boolean(ot->srna, "animation", 0, "Animation", "Render files from the animation range of this scene");
1063         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
1064         RNA_def_boolean(ot->srna, "write_still", 0, "Write Image", "Save rendered the image to the output path (used only when animation is disabled)");
1065         prop = RNA_def_boolean(ot->srna, "use_viewport", 0, "Use 3D Viewport", "When inside a 3D viewport, use layers and camera of the viewport");
1066         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
1067         prop = RNA_def_string(ot->srna, "layer", NULL, RE_MAXNAME, "Render Layer", "Single render layer to re-render (used only when animation is disabled)");
1068         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
1069         prop = RNA_def_string(ot->srna, "scene", NULL, MAX_ID_NAME - 2, "Scene", "Scene to render, current scene if not specified");
1070         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
1071 }
1072
1073
1074 Scene *ED_render_job_get_scene(const bContext *C)
1075 {
1076         wmWindowManager *wm = CTX_wm_manager(C);
1077         RenderJob *rj = (RenderJob *)WM_jobs_customdata_from_type(wm, WM_JOB_TYPE_RENDER);
1078
1079         if (rj)
1080                 return rj->scene;
1081
1082         return NULL;
1083 }
1084
1085 Scene *ED_render_job_get_current_scene(const bContext *C)
1086 {
1087         wmWindowManager *wm = CTX_wm_manager(C);
1088         RenderJob *rj = (RenderJob *)WM_jobs_customdata_from_type(wm, WM_JOB_TYPE_RENDER);
1089         if (rj) {
1090                 return rj->current_scene;
1091         }
1092         return NULL;
1093 }
1094
1095
1096 /* Motion blur curve preset */
1097
1098 static int render_shutter_curve_preset_exec(bContext *C, wmOperator *op)
1099 {
1100         Scene *scene = CTX_data_scene(C);
1101         CurveMapping *mblur_shutter_curve = &scene->r.mblur_shutter_curve;
1102         CurveMap *cm = mblur_shutter_curve->cm;
1103         int preset = RNA_enum_get(op->ptr, "shape");
1104
1105         cm->flag &= ~CUMA_EXTEND_EXTRAPOLATE;
1106         mblur_shutter_curve->preset = preset;
1107         curvemap_reset(cm,
1108                        &mblur_shutter_curve->clipr,
1109                        mblur_shutter_curve->preset,
1110                        CURVEMAP_SLOPE_POS_NEG);
1111         curvemapping_changed(mblur_shutter_curve, false);
1112
1113         return OPERATOR_FINISHED;
1114 }
1115
1116 void RENDER_OT_shutter_curve_preset(wmOperatorType *ot)
1117 {
1118         PropertyRNA *prop;
1119         static const EnumPropertyItem prop_shape_items[] = {
1120                 {CURVE_PRESET_SHARP, "SHARP", 0, "Sharp", ""},
1121                 {CURVE_PRESET_SMOOTH, "SMOOTH", 0, "Smooth", ""},
1122                 {CURVE_PRESET_MAX, "MAX", 0, "Max", ""},
1123                 {CURVE_PRESET_LINE, "LINE", 0, "Line", ""},
1124                 {CURVE_PRESET_ROUND, "ROUND", 0, "Round", ""},
1125                 {CURVE_PRESET_ROOT, "ROOT", 0, "Root", ""},
1126                 {0, NULL, 0, NULL, NULL}};
1127
1128         ot->name = "Shutter Curve Preset";
1129         ot->description = "Set shutter curve";
1130         ot->idname = "RENDER_OT_shutter_curve_preset";
1131
1132         ot->exec = render_shutter_curve_preset_exec;
1133
1134         prop = RNA_def_enum(ot->srna, "shape", prop_shape_items, CURVE_PRESET_SMOOTH, "Mode", "");
1135         RNA_def_property_translation_context(prop, BLT_I18NCONTEXT_ID_CURVE); /* Abusing id_curve :/ */
1136 }