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