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 *view_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_view_layer_set(wmOperator *op, Main *mainp, Scene **scene, ViewLayer **view_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                         *view_layer = rl;
294         }
295 }
296
297 /* executes blocking render */
298 static int screen_render_exec(bContext *C, wmOperator *op)
299 {
300         Scene *scene = CTX_data_scene(C);
301         RenderEngineType *re_type = RE_engines_find(scene->view_render.engine_id);
302         ViewLayer *view_layer = NULL;
303         Render *re;
304         Image *ima;
305         View3D *v3d = CTX_wm_view3d(C);
306         Main *mainp = CTX_data_main(C);
307         unsigned int lay_override;
308         const bool is_animation = RNA_boolean_get(op->ptr, "animation");
309         const bool is_write_still = RNA_boolean_get(op->ptr, "write_still");
310         struct Object *camera_override = v3d ? V3D_CAMERA_LOCAL(v3d) : NULL;
311
312         /* Cannot do render if there is not this function. */
313         if (re_type->render_to_image == NULL) {
314                 return OPERATOR_CANCELLED;
315         }
316
317         /* custom scene and single layer re-render */
318         screen_render_view_layer_set(op, mainp, &scene, &view_layer);
319
320         if (!is_animation && is_write_still && BKE_imtype_is_movie(scene->r.im_format.imtype)) {
321                 BKE_report(op->reports, RPT_ERROR, "Cannot write a single file with an animation format selected");
322                 return OPERATOR_CANCELLED;
323         }
324
325         re = RE_NewSceneRender(scene);
326         lay_override = (v3d && v3d->lay != scene->lay) ? v3d->lay : 0;
327
328         G.is_break = false;
329         RE_test_break_cb(re, NULL, render_break);
330
331         ima = BKE_image_verify_viewer(IMA_TYPE_R_RESULT, "Render Result");
332         BKE_image_signal(ima, NULL, IMA_SIGNAL_FREE);
333         BKE_image_backup_render(scene, ima, true);
334
335         /* cleanup sequencer caches before starting user triggered render.
336          * otherwise, invalidated cache entries can make their way into
337          * the output rendering. We can't put that into RE_BlenderFrame,
338          * since sequence rendering can call that recursively... (peter) */
339         BKE_sequencer_cache_cleanup();
340
341         RE_SetReports(re, op->reports);
342
343         BLI_threaded_malloc_begin();
344         if (is_animation)
345                 RE_BlenderAnim(re, mainp, scene, camera_override, lay_override, scene->r.sfra, scene->r.efra, scene->r.frame_step);
346         else
347                 RE_BlenderFrame(re, mainp, scene, view_layer, camera_override, lay_override, scene->r.cfra, is_write_still);
348         BLI_threaded_malloc_end();
349
350         RE_SetReports(re, NULL);
351
352         // no redraw needed, we leave state as we entered it
353         ED_update_for_newframe(mainp, scene, view_layer, CTX_data_depsgraph(C));
354
355         WM_event_add_notifier(C, NC_SCENE | ND_RENDER_RESULT, scene);
356
357         return OPERATOR_FINISHED;
358 }
359
360 static void render_freejob(void *rjv)
361 {
362         RenderJob *rj = rjv;
363
364         BKE_color_managed_view_settings_free(&rj->view_settings);
365         MEM_freeN(rj);
366 }
367
368 /* str is IMA_MAX_RENDER_TEXT in size */
369 static void make_renderinfo_string(const RenderStats *rs,
370                                    const Scene *scene,
371                                    const bool v3d_override,
372                                    const char *error,
373                                    char *str)
374 {
375         char info_time_str[32]; // used to be extern to header_info.c
376         uintptr_t mem_in_use, mmap_in_use, peak_memory;
377         float megs_used_memory, mmap_used_memory, megs_peak_memory;
378         char *spos = str;
379
380         mem_in_use = MEM_get_memory_in_use();
381         mmap_in_use = MEM_get_mapped_memory_in_use();
382         peak_memory = MEM_get_peak_memory();
383
384         megs_used_memory = (mem_in_use - mmap_in_use) / (1024.0 * 1024.0);
385         mmap_used_memory = (mmap_in_use) / (1024.0 * 1024.0);
386         megs_peak_memory = (peak_memory) / (1024.0 * 1024.0);
387
388         /* local view */
389         if (rs->localview)
390                 spos += sprintf(spos, "%s | ", IFACE_("3D Local View"));
391         else if (v3d_override)
392                 spos += sprintf(spos, "%s | ", IFACE_("3D View"));
393
394         /* frame number */
395         spos += sprintf(spos, IFACE_("Frame:%d "), (scene->r.cfra));
396
397         /* previous and elapsed time */
398         BLI_timecode_string_from_time_simple(info_time_str, sizeof(info_time_str), rs->lastframetime);
399
400         if (rs->infostr && rs->infostr[0]) {
401                 if (rs->lastframetime != 0.0)
402                         spos += sprintf(spos, IFACE_("| Last:%s "), info_time_str);
403                 else
404                         spos += sprintf(spos, "| ");
405
406                 BLI_timecode_string_from_time_simple(info_time_str, sizeof(info_time_str), PIL_check_seconds_timer() - rs->starttime);
407         }
408         else
409                 spos += sprintf(spos, "| ");
410
411         spos += sprintf(spos, IFACE_("Time:%s "), info_time_str);
412
413         /* statistics */
414         if (rs->statstr) {
415                 if (rs->statstr[0]) {
416                         spos += sprintf(spos, "| %s ", rs->statstr);
417                 }
418         }
419         else {
420                 if (rs->totvert || rs->totface || rs->tothalo || rs->totstrand || rs->totlamp)
421                         spos += sprintf(spos, "| ");
422
423                 if (rs->totvert) spos += sprintf(spos, IFACE_("Ve:%d "), rs->totvert);
424                 if (rs->totface) spos += sprintf(spos, IFACE_("Fa:%d "), rs->totface);
425                 if (rs->tothalo) spos += sprintf(spos, IFACE_("Ha:%d "), rs->tothalo);
426                 if (rs->totstrand) spos += sprintf(spos, IFACE_("St:%d "), rs->totstrand);
427                 if (rs->totlamp) spos += sprintf(spos, IFACE_("La:%d "), rs->totlamp);
428
429                 if (rs->mem_peak == 0.0f)
430                         spos += sprintf(spos, IFACE_("| Mem:%.2fM (%.2fM, Peak %.2fM) "),
431                                         megs_used_memory, mmap_used_memory, megs_peak_memory);
432                 else
433                         spos += sprintf(spos, IFACE_("| Mem:%.2fM, Peak: %.2fM "), rs->mem_used, rs->mem_peak);
434
435                 if (rs->curfield)
436                         spos += sprintf(spos, IFACE_("Field %d "), rs->curfield);
437                 if (rs->curblur)
438                         spos += sprintf(spos, IFACE_("Blur %d "), rs->curblur);
439         }
440
441         /* full sample */
442         if (rs->curfsa)
443                 spos += sprintf(spos, IFACE_("| Full Sample %d "), rs->curfsa);
444         
445         /* extra info */
446         if (rs->infostr && rs->infostr[0]) {
447                 spos += sprintf(spos, "| %s ", rs->infostr);
448         }
449         else if (error && error[0]) {
450                 spos += sprintf(spos, "| %s ", error);
451         }
452
453         /* very weak... but 512 characters is quite safe */
454         if (spos >= str + IMA_MAX_RENDER_TEXT)
455                 if (G.debug & G_DEBUG)
456                         printf("WARNING! renderwin text beyond limit\n");
457
458 }
459
460 static void image_renderinfo_cb(void *rjv, RenderStats *rs)
461 {
462         RenderJob *rj = rjv;
463         RenderResult *rr;
464
465         rr = RE_AcquireResultRead(rj->re);
466
467         if (rr) {
468                 /* malloc OK here, stats_draw is not in tile threads */
469                 if (rr->text == NULL)
470                         rr->text = MEM_callocN(IMA_MAX_RENDER_TEXT, "rendertext");
471
472                 make_renderinfo_string(rs, rj->scene, rj->v3d_override,
473                                        rr->error, rr->text);
474         }
475
476         RE_ReleaseResult(rj->re);
477
478         /* make jobs timer to send notifier */
479         *(rj->do_update) = true;
480
481 }
482
483 static void render_progress_update(void *rjv, float progress)
484 {
485         RenderJob *rj = rjv;
486         
487         if (rj->progress && *rj->progress != progress) {
488                 *rj->progress = progress;
489
490                 /* make jobs timer to send notifier */
491                 *(rj->do_update) = true;
492         }
493 }
494
495 /* Not totally reliable, but works fine in most of cases and
496  * in worst case would just make it so extra color management
497  * for the whole render result is applied (which was already
498  * happening already).
499  */
500 static void render_image_update_pass_and_layer(RenderJob *rj, RenderResult *rr, ImageUser *iuser)
501 {
502         wmWindowManager *wm;
503         ScrArea *first_sa = NULL, *matched_sa = NULL;
504
505         /* image window, compo node users */
506         for (wm = rj->main->wm.first; wm && matched_sa == NULL; wm = wm->id.next) { /* only 1 wm */
507                 wmWindow *win;
508                 for (win = wm->windows.first; win && matched_sa == NULL; win = win->next) {
509                         const bScreen *screen = WM_window_get_active_screen(win);
510
511                         for (ScrArea *sa = screen->areabase.first; sa; sa = sa->next) {
512                                 if (sa->spacetype == SPACE_IMAGE) {
513                                         SpaceImage *sima = sa->spacedata.first;
514                                         // sa->spacedata might be empty when toggling fullscreen mode.
515                                         if (sima != NULL && sima->image == rj->image) {
516                                                 if (first_sa == NULL) {
517                                                         first_sa = sa;
518                                                 }
519                                                 if (sa == rj->sa) {
520                                                         matched_sa = sa;
521                                                         break;
522                                                 }
523                                         }
524                                 }
525                         }
526                 }
527         }
528
529         if (matched_sa == NULL) {
530                 matched_sa = first_sa;
531         }
532
533         if (matched_sa) {
534                 SpaceImage *sima = matched_sa->spacedata.first;
535                 RenderResult *main_rr = RE_AcquireResultRead(rj->re);
536
537                 /* TODO(sergey): is there faster way to get the layer index? */
538                 if (rr->renlay) {
539                         int layer = BLI_findstringindex(&main_rr->layers,
540                                                         (char *)rr->renlay->name,
541                                                         offsetof(RenderLayer, name));
542                         sima->iuser.layer = layer;
543                         rj->last_layer = layer;
544                 }
545
546                 iuser->pass = sima->iuser.pass;
547                 iuser->layer = sima->iuser.layer;
548
549                 RE_ReleaseResult(rj->re);
550         }
551 }
552
553 static void image_rect_update(void *rjv, RenderResult *rr, volatile rcti *renrect)
554 {
555         RenderJob *rj = rjv;
556         Image *ima = rj->image;
557         ImBuf *ibuf;
558         void *lock;
559         const char *viewname = RE_GetActiveRenderView(rj->re);
560
561         /* only update if we are displaying the slot being rendered */
562         if (ima->render_slot != ima->last_render_slot) {
563                 rj->image_outdated = true;
564                 return;
565         }
566         else if (rj->image_outdated) {
567                 /* update entire render */
568                 rj->image_outdated = false;
569                 BKE_image_signal(ima, NULL, IMA_SIGNAL_COLORMANAGE);
570                 *(rj->do_update) = true;
571                 return;
572         }
573         
574         if (rr == NULL)
575                 return;
576         
577         /* update part of render */
578         render_image_update_pass_and_layer(rj, rr, &rj->iuser);
579         ibuf = BKE_image_acquire_ibuf(ima, &rj->iuser, &lock);
580         if (ibuf) {
581                 /* Don't waste time on CPU side color management if
582                  * image will be displayed using GLSL.
583                  *
584                  * Need to update rect if Save Buffers enabled because in
585                  * this case GLSL doesn't have original float buffer to
586                  * operate with.
587                  */
588                 if (rr->do_exr_tile ||
589                     !rj->supports_glsl_draw ||
590                     ibuf->channels == 1 ||
591                     U.image_draw_method != IMAGE_DRAW_METHOD_GLSL)
592                 {
593                         image_buffer_rect_update(rj, rr, ibuf, &rj->iuser, renrect, viewname);
594                 }
595                 
596                 /* make jobs timer to send notifier */
597                 *(rj->do_update) = true;
598         }
599         BKE_image_release_ibuf(ima, ibuf, lock);
600 }
601
602 static void current_scene_update(void *rjv, Scene *scene)
603 {
604         RenderJob *rj = rjv;
605         rj->current_scene = scene;
606         rj->iuser.scene = scene;
607 }
608
609 static void render_startjob(void *rjv, short *stop, short *do_update, float *progress)
610 {
611         RenderJob *rj = rjv;
612
613         rj->stop = stop;
614         rj->do_update = do_update;
615         rj->progress = progress;
616
617         RE_SetReports(rj->re, rj->reports);
618
619         if (rj->anim)
620                 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);
621         else
622                 RE_BlenderFrame(rj->re, rj->main, rj->scene, rj->view_layer, rj->camera_override, rj->lay_override, rj->scene->r.cfra, rj->write_still);
623
624         RE_SetReports(rj->re, NULL);
625 }
626
627 static void render_image_restore_layer(RenderJob *rj)
628 {
629         wmWindowManager *wm;
630
631         /* image window, compo node users */
632         for (wm = rj->main->wm.first; wm; wm = wm->id.next) { /* only 1 wm */
633                 wmWindow *win;
634                 for (win = wm->windows.first; win; win = win->next) {
635                         const bScreen *screen = WM_window_get_active_screen(win);
636
637                         for (ScrArea *sa = screen->areabase.first; sa; sa = sa->next) {
638                                 if (sa == rj->sa) {
639                                         if (sa->spacetype == SPACE_IMAGE) {
640                                                 SpaceImage *sima = sa->spacedata.first;
641
642                                                 if (RE_HasSingleLayer(rj->re)) {
643                                                         /* For single layer renders keep the active layer
644                                                          * visible, or show the compositing result. */
645                                                         RenderResult *rr = RE_AcquireResultRead(rj->re);
646                                                         if (RE_HasCombinedLayer(rr)) {
647                                                                 sima->iuser.layer = 0;
648                                                         }
649                                                         RE_ReleaseResult(rj->re);
650                                                 }
651                                                 else {
652                                                         /* For multiple layer render, set back the layer
653                                                          * that was set at the start of rendering. */
654                                                         sima->iuser.layer = rj->orig_layer;
655                                                 }
656                                         }
657                                         return;
658                                 }
659                         }
660                 }
661         }
662 }
663
664 static void render_endjob(void *rjv)
665 {
666         RenderJob *rj = rjv;
667
668         /* this render may be used again by the sequencer without the active 'Render' where the callbacks
669          * would be re-assigned. assign dummy callbacks to avoid referencing freed renderjobs bug [#24508] */
670         RE_InitRenderCB(rj->re);
671
672         if (rj->main != G.main)
673                 BKE_main_free(rj->main);
674
675         /* else the frame will not update for the original value */
676         if (rj->anim && !(rj->scene->r.scemode & R_NO_FRAME_UPDATE)) {
677                 /* possible this fails of loading new file while rendering */
678                 if (G.main->wm.first) {
679                         ED_update_for_newframe(G.main, rj->scene, rj->view_layer, rj->depsgraph);
680                 }
681         }
682         
683         /* XXX above function sets all tags in nodes */
684         ntreeCompositClearTags(rj->scene->nodetree);
685         
686         /* potentially set by caller */
687         rj->scene->r.scemode &= ~R_NO_FRAME_UPDATE;
688         
689         if (rj->view_layer) {
690                 nodeUpdateID(rj->scene->nodetree, &rj->scene->id);
691                 WM_main_add_notifier(NC_NODE | NA_EDITED, rj->scene);
692         }
693
694         if (rj->sa) {
695                 render_image_restore_layer(rj);
696         }
697
698         /* XXX render stability hack */
699         G.is_rendering = false;
700         WM_main_add_notifier(NC_SCENE | ND_RENDER_RESULT, NULL);
701
702         /* Partial render result will always update display buffer
703          * for first render layer only. This is nice because you'll
704          * see render progress during rendering, but it ends up in
705          * wrong display buffer shown after rendering.
706          *
707          * The code below will mark display buffer as invalid after
708          * rendering in case multiple layers were rendered, which
709          * ensures display buffer matches render layer after
710          * rendering.
711          *
712          * Perhaps proper way would be to toggle active render
713          * layer in image editor and job, so we always display
714          * layer being currently rendered. But this is not so much
715          * trivial at this moment, especially because of external
716          * engine API, so lets use simple and robust way for now
717          *                                          - sergey -
718          */
719         if (rj->scene->view_layers.first != rj->scene->view_layers.last ||
720             rj->image_outdated)
721         {
722                 void *lock;
723                 Image *ima = rj->image;
724                 ImBuf *ibuf = BKE_image_acquire_ibuf(ima, &rj->iuser, &lock);
725
726                 if (ibuf)
727                         ibuf->userflags |= IB_DISPLAY_BUFFER_INVALID;
728
729                 BKE_image_release_ibuf(ima, ibuf, lock);
730         }
731
732         /* Finally unlock the user interface (if it was locked). */
733         if (rj->interface_locked) {
734                 Scene *scene;
735
736                 /* Interface was locked, so window manager couldn't have been changed
737                  * and using one from Global will unlock exactly the same manager as
738                  * was locked before running the job.
739                  */
740                 WM_set_locked_interface(G.main->wm.first, false);
741
742                 /* We've freed all the derived caches before rendering, which is
743                  * effectively the same as if we re-loaded the file.
744                  *
745                  * So let's not try being smart here and just reset all updated
746                  * scene layers and use generic DAG_on_visible_update.
747                  */
748                 for (scene = G.main->scene.first; scene; scene = scene->id.next) {
749                         scene->lay_updated = 0;
750                 }
751
752                 DEG_on_visible_update(G.main, false);
753         }
754 }
755
756 /* called by render, check job 'stop' value or the global */
757 static int render_breakjob(void *rjv)
758 {
759         RenderJob *rj = rjv;
760
761         if (G.is_break)
762                 return 1;
763         if (rj->stop && *(rj->stop))
764                 return 1;
765         return 0;
766 }
767
768 /* for exec() when there is no render job
769  * note: this wont check for the escape key being pressed, but doing so isnt threadsafe */
770 static int render_break(void *UNUSED(rjv))
771 {
772         if (G.is_break)
773                 return 1;
774         return 0;
775 }
776
777 /* runs in thread, no cursor setting here works. careful with notifiers too (malloc conflicts) */
778 /* maybe need a way to get job send notifer? */
779 static void render_drawlock(void *rjv, int lock)
780 {
781         RenderJob *rj = rjv;
782
783         /* If interface is locked, renderer callback shall do nothing. */
784         if (!rj->interface_locked) {
785                 BKE_spacedata_draw_locks(lock);
786         }
787 }
788
789 /* catch esc */
790 static int screen_render_modal(bContext *C, wmOperator *op, const wmEvent *event)
791 {
792         Scene *scene = (Scene *) op->customdata;
793
794         /* no running blender, remove handler and pass through */
795         if (0 == WM_jobs_test(CTX_wm_manager(C), scene, WM_JOB_TYPE_RENDER)) {
796                 return OPERATOR_FINISHED | OPERATOR_PASS_THROUGH;
797         }
798
799         /* running render */
800         switch (event->type) {
801                 case ESCKEY:
802                         return OPERATOR_RUNNING_MODAL;
803         }
804         return OPERATOR_PASS_THROUGH;
805 }
806
807 static void screen_render_cancel(bContext *C, wmOperator *op)
808 {
809         wmWindowManager *wm = CTX_wm_manager(C);
810         Scene *scene = (Scene *) op->customdata;
811
812         /* kill on cancel, because job is using op->reports */
813         WM_jobs_kill_type(wm, scene, WM_JOB_TYPE_RENDER);
814 }
815
816 static void clean_viewport_memory_base(Base *base)
817 {
818         if ((base->flag & BASE_VISIBLED) == 0) {
819                 return;
820         }
821
822         Object *object = base->object;
823
824         if (object->id.tag & LIB_TAG_DOIT) {
825                 return;
826         }
827
828         object->id.tag &= ~LIB_TAG_DOIT;
829         if (RE_allow_render_generic_object(object)) {
830                 BKE_object_free_derived_caches(object);
831         }
832 }
833
834 static void clean_viewport_memory(Main *bmain, Scene *scene)
835 {
836         Scene *sce_iter;
837         Base *base;
838
839         /* Tag all the available objects. */
840         BKE_main_id_tag_listbase(&bmain->object, LIB_TAG_DOIT, true);
841
842         /* Go over all the visible objects. */
843         for (wmWindowManager *wm = bmain->wm.first; wm; wm = wm->id.next) {
844                 for (wmWindow *win = wm->windows.first; win; win = win->next) {
845                         WorkSpace *workspace = BKE_workspace_active_get(win->workspace_hook);
846                         ViewLayer *view_layer = BKE_view_layer_from_workspace_get(scene, workspace);
847
848                         for (base = view_layer->object_bases.first; base; base = base->next) {
849                                 clean_viewport_memory_base(base);
850                         }
851                 }
852         }
853
854         for (SETLOOPER_SET_ONLY(scene, sce_iter, base)) {
855                 clean_viewport_memory_base(base);
856         }
857 }
858
859 /* using context, starts job */
860 static int screen_render_invoke(bContext *C, wmOperator *op, const wmEvent *event)
861 {
862         /* new render clears all callbacks */
863         Main *mainp;
864         ViewLayer *view_layer = NULL;
865         Scene *scene = CTX_data_scene(C);
866         RenderEngineType *re_type = RE_engines_find(scene->view_render.engine_id);
867         Render *re;
868         wmJob *wm_job;
869         RenderJob *rj;
870         Image *ima;
871         int jobflag;
872         const bool is_animation = RNA_boolean_get(op->ptr, "animation");
873         const bool is_write_still = RNA_boolean_get(op->ptr, "write_still");
874         const bool use_viewport = RNA_boolean_get(op->ptr, "use_viewport");
875         View3D *v3d = use_viewport ? CTX_wm_view3d(C) : NULL;
876         struct Object *camera_override = v3d ? V3D_CAMERA_LOCAL(v3d) : NULL;
877         const char *name;
878         ScrArea *sa;
879
880         /* Cannot do render if there is not this function. */
881         if (re_type->render_to_image == NULL) {
882                 return OPERATOR_CANCELLED;
883         }
884
885         /* only one render job at a time */
886         if (WM_jobs_test(CTX_wm_manager(C), scene, WM_JOB_TYPE_RENDER))
887                 return OPERATOR_CANCELLED;
888
889         if (RE_force_single_renderlayer(scene))
890                 WM_event_add_notifier(C, NC_SCENE | ND_RENDER_OPTIONS, NULL);
891
892         if (!RE_is_rendering_allowed(scene, camera_override, op->reports)) {
893                 return OPERATOR_CANCELLED;
894         }
895
896         if (!is_animation && is_write_still && BKE_imtype_is_movie(scene->r.im_format.imtype)) {
897                 BKE_report(op->reports, RPT_ERROR, "Cannot write a single file with an animation format selected");
898                 return OPERATOR_CANCELLED;
899         }
900         
901         /* stop all running jobs, except screen one. currently previews frustrate Render */
902         WM_jobs_kill_all_except(CTX_wm_manager(C), CTX_wm_screen(C));
903
904         /* get main */
905         if (G.debug_value == 101) {
906                 /* thread-safety experiment, copy main from the undo buffer */
907                 struct MemFile *memfile = ED_undosys_stack_memfile_get_active(CTX_wm_manager(C)->undo_stack);
908                 mainp = BLO_memfile_main_get(memfile, CTX_data_main(C), &scene);
909         }
910         else
911                 mainp = CTX_data_main(C);
912
913         /* cancel animation playback */
914         if (ED_screen_animation_playing(CTX_wm_manager(C)))
915                 ED_screen_animation_play(C, 0, 0);
916         
917         /* handle UI stuff */
918         WM_cursor_wait(1);
919
920         /* flush sculpt and editmode changes */
921         ED_editors_flush_edits(C, true);
922
923         /* cleanup sequencer caches before starting user triggered render.
924          * otherwise, invalidated cache entries can make their way into
925          * the output rendering. We can't put that into RE_BlenderFrame,
926          * since sequence rendering can call that recursively... (peter) */
927         BKE_sequencer_cache_cleanup();
928
929         // store spare
930         // get view3d layer, local layer, make this nice api call to render
931         // store spare
932
933         /* ensure at least 1 area shows result */
934         sa = render_view_open(C, event->x, event->y, op->reports);
935
936         jobflag = WM_JOB_EXCL_RENDER | WM_JOB_PRIORITY | WM_JOB_PROGRESS;
937         
938         /* custom scene and single layer re-render */
939         screen_render_view_layer_set(op, mainp, &scene, &view_layer);
940
941         if (RNA_struct_property_is_set(op->ptr, "layer"))
942                 jobflag |= WM_JOB_SUSPEND;
943
944         /* job custom data */
945         rj = MEM_callocN(sizeof(RenderJob), "render job");
946         rj->main = mainp;
947         rj->scene = scene;
948         rj->current_scene = rj->scene;
949         rj->view_layer = view_layer;
950         /* TODO(sergey): Render engine should be using own depsgraph. */
951         rj->depsgraph = CTX_data_depsgraph(C);
952         rj->camera_override = camera_override;
953         rj->lay_override = 0;
954         rj->anim = is_animation;
955         rj->write_still = is_write_still && !is_animation;
956         rj->iuser.scene = scene;
957         rj->iuser.ok = 1;
958         rj->reports = op->reports;
959         rj->orig_layer = 0;
960         rj->last_layer = 0;
961         rj->sa = sa;
962         rj->supports_glsl_draw = IMB_colormanagement_support_glsl_draw(&scene->view_settings);
963
964         BKE_color_managed_display_settings_copy(&rj->display_settings, &scene->display_settings);
965         BKE_color_managed_view_settings_copy(&rj->view_settings, &scene->view_settings);
966
967         if (sa) {
968                 SpaceImage *sima = sa->spacedata.first;
969                 rj->orig_layer = sima->iuser.layer;
970         }
971
972         if (v3d) {
973                 if (scene->lay != v3d->lay) {
974                         rj->lay_override = v3d->lay;
975                         rj->v3d_override = true;
976                 }
977                 else if (camera_override && camera_override != scene->camera)
978                         rj->v3d_override = true;
979
980                 if (v3d->localvd)
981                         rj->lay_override |= v3d->localvd->lay;
982         }
983
984         /* Lock the user interface depending on render settings. */
985         if (scene->r.use_lock_interface) {
986                 WM_set_locked_interface(CTX_wm_manager(C), true);
987
988                 /* Set flag interface need to be unlocked.
989                  *
990                  * This is so because we don't have copy of render settings
991                  * accessible from render job and copy is needed in case
992                  * of non-locked rendering, so we wouldn't try to unlock
993                  * anything if option was initially unset but then was
994                  * enabled during rendering.
995                  */
996                 rj->interface_locked = true;
997
998                 /* Clean memory used by viewport? */
999                 clean_viewport_memory(rj->main, scene);
1000         }
1001
1002         /* setup job */
1003         if (RE_seq_render_active(scene, &scene->r)) name = "Sequence Render";
1004         else name = "Render";
1005
1006         wm_job = WM_jobs_get(CTX_wm_manager(C), CTX_wm_window(C), scene, name, jobflag, WM_JOB_TYPE_RENDER);
1007         WM_jobs_customdata_set(wm_job, rj, render_freejob);
1008         WM_jobs_timer(wm_job, 0.2, NC_SCENE | ND_RENDER_RESULT, 0);
1009         WM_jobs_callbacks(wm_job, render_startjob, NULL, NULL, render_endjob);
1010
1011         /* get a render result image, and make sure it is empty */
1012         ima = BKE_image_verify_viewer(IMA_TYPE_R_RESULT, "Render Result");
1013         BKE_image_signal(ima, NULL, IMA_SIGNAL_FREE);
1014         BKE_image_backup_render(rj->scene, ima, true);
1015         rj->image = ima;
1016
1017         /* setup new render */
1018         re = RE_NewSceneRender(scene);
1019         RE_test_break_cb(re, rj, render_breakjob);
1020         RE_draw_lock_cb(re, rj, render_drawlock);
1021         RE_display_update_cb(re, rj, image_rect_update);
1022         RE_current_scene_update_cb(re, rj, current_scene_update);
1023         RE_stats_draw_cb(re, rj, image_renderinfo_cb);
1024         RE_progress_cb(re, rj, render_progress_update);
1025
1026         rj->re = re;
1027         G.is_break = false;
1028
1029         /* store actual owner of job, so modal operator could check for it,
1030          * the reason of this is that active scene could change when rendering
1031          * several layers from compositor [#31800]
1032          */
1033         op->customdata = scene;
1034
1035         WM_jobs_start(CTX_wm_manager(C), wm_job);
1036
1037         WM_cursor_wait(0);
1038         WM_event_add_notifier(C, NC_SCENE | ND_RENDER_RESULT, scene);
1039
1040         /* we set G.is_rendering here already instead of only in the job, this ensure
1041          * main loop or other scene updates are disabled in time, since they may
1042          * have started before the job thread */
1043         G.is_rendering = true;
1044
1045         /* add modal handler for ESC */
1046         WM_event_add_modal_handler(C, op);
1047
1048         return OPERATOR_RUNNING_MODAL;
1049 }
1050
1051 /* contextual render, using current scene, view3d? */
1052 void RENDER_OT_render(wmOperatorType *ot)
1053 {
1054         PropertyRNA *prop;
1055
1056         /* identifiers */
1057         ot->name = "Render";
1058         ot->description = "Render active scene";
1059         ot->idname = "RENDER_OT_render";
1060
1061         /* api callbacks */
1062         ot->invoke = screen_render_invoke;
1063         ot->modal = screen_render_modal;
1064         ot->cancel = screen_render_cancel;
1065         ot->exec = screen_render_exec;
1066
1067         /*ot->poll = ED_operator_screenactive;*/ /* this isn't needed, causes failer in background mode */
1068
1069         prop = RNA_def_boolean(ot->srna, "animation", 0, "Animation", "Render files from the animation range of this scene");
1070         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
1071         RNA_def_boolean(ot->srna, "write_still", 0, "Write Image", "Save rendered the image to the output path (used only when animation is disabled)");
1072         prop = RNA_def_boolean(ot->srna, "use_viewport", 0, "Use 3D Viewport", "When inside a 3D viewport, use layers and camera of the viewport");
1073         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
1074         prop = RNA_def_string(ot->srna, "layer", NULL, RE_MAXNAME, "Render Layer", "Single render layer to re-render (used only when animation is disabled)");
1075         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
1076         prop = RNA_def_string(ot->srna, "scene", NULL, MAX_ID_NAME - 2, "Scene", "Scene to render, current scene if not specified");
1077         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
1078 }
1079
1080
1081 /* ************** preview for 3d viewport ***************** */
1082
1083 #define PR_UPDATE_VIEW                          1
1084 #define PR_UPDATE_RENDERSIZE            2
1085 #define PR_UPDATE_MATERIAL                      4
1086 #define PR_UPDATE_DATABASE                      8
1087
1088 typedef struct RenderPreview {
1089         /* from wmJob */
1090         void *owner;
1091         short *stop, *do_update;
1092         wmJob *job;
1093         
1094         Scene *scene;
1095         EvaluationContext *eval_ctx;
1096         Depsgraph *depsgraph;
1097         ScrArea *sa;
1098         ARegion *ar;
1099         View3D *v3d;
1100         RegionView3D *rv3d;
1101         Main *bmain;
1102         RenderEngine *engine;
1103         
1104         float viewmat[4][4];
1105
1106         int start_resolution_divider;
1107         int resolution_divider;
1108         bool has_freestyle;
1109 } RenderPreview;
1110
1111 static int render_view3d_disprect(Scene *scene, const Depsgraph *depsgraph,
1112                                   ARegion *ar, View3D *v3d, RegionView3D *rv3d, rcti *disprect)
1113 {
1114         /* copied code from view3d_draw.c */
1115         rctf viewborder;
1116         int draw_border;
1117         
1118         if (rv3d->persp == RV3D_CAMOB)
1119                 draw_border = (scene->r.mode & R_BORDER) != 0;
1120         else
1121                 draw_border = (v3d->flag2 & V3D_RENDER_BORDER) != 0;
1122
1123         if (draw_border) {
1124                 if (rv3d->persp == RV3D_CAMOB) {
1125                         ED_view3d_calc_camera_border(scene, depsgraph, ar, v3d, rv3d, &viewborder, false);
1126                         
1127                         disprect->xmin = viewborder.xmin + scene->r.border.xmin * BLI_rctf_size_x(&viewborder);
1128                         disprect->ymin = viewborder.ymin + scene->r.border.ymin * BLI_rctf_size_y(&viewborder);
1129                         disprect->xmax = viewborder.xmin + scene->r.border.xmax * BLI_rctf_size_x(&viewborder);
1130                         disprect->ymax = viewborder.ymin + scene->r.border.ymax * BLI_rctf_size_y(&viewborder);
1131                 }
1132                 else {
1133                         disprect->xmin = v3d->render_border.xmin * ar->winx;
1134                         disprect->xmax = v3d->render_border.xmax * ar->winx;
1135                         disprect->ymin = v3d->render_border.ymin * ar->winy;
1136                         disprect->ymax = v3d->render_border.ymax * ar->winy;
1137                 }
1138                 
1139                 return 1;
1140         }
1141         
1142         BLI_rcti_init(disprect, 0, 0, 0, 0);
1143         return 0;
1144 }
1145
1146 /* returns true if OK  */
1147 static bool render_view3d_get_rects(
1148         const Depsgraph *depsgraph,
1149         ARegion *ar, View3D *v3d, RegionView3D *rv3d, rctf *viewplane, RenderEngine *engine,
1150         float *r_clipsta, float *r_clipend, float *r_pixsize, bool *r_ortho)
1151 {
1152         
1153         if (ar->winx < 4 || ar->winy < 4) return false;
1154         
1155         *r_ortho = ED_view3d_viewplane_get(depsgraph, v3d, rv3d, ar->winx, ar->winy, viewplane, r_clipsta, r_clipend, r_pixsize);
1156         
1157         engine->resolution_x = ar->winx;
1158         engine->resolution_y = ar->winy;
1159
1160         return true;
1161 }
1162
1163 static bool render_view3d_is_valid(RenderPreview *rp)
1164 {
1165         return (rp->rv3d->render_engine != NULL);
1166 }
1167
1168 /* called by renderer, checks job value */
1169 static int render_view3d_break(void *rpv)
1170 {
1171         RenderPreview *rp = rpv;
1172         
1173         if (G.is_break)
1174                 return 1;
1175         
1176         /* during render, rv3d->engine can get freed */
1177         if (render_view3d_is_valid(rp) == false) {
1178                 *rp->stop = 1;
1179         }
1180         
1181         return *(rp->stop);
1182 }
1183
1184 static void render_view3d_display_update(void *rpv, RenderResult *UNUSED(rr), volatile struct rcti *UNUSED(rect))
1185 {
1186         RenderPreview *rp = rpv;
1187         
1188         *(rp->do_update) = true;
1189 }
1190
1191 static void render_view3d_renderinfo_cb(void *rjp, RenderStats *rs)
1192 {
1193         RenderPreview *rp = rjp;
1194
1195         /* during render, rv3d->engine can get freed */
1196         if (rp->rv3d->render_engine == NULL) {
1197                 *rp->stop = 1;
1198         }
1199         else {
1200                 make_renderinfo_string(rs, rp->scene, false, NULL, rp->engine->text);
1201         
1202                 /* make jobs timer to send notifier */
1203                 *(rp->do_update) = true;
1204         }
1205 }
1206
1207 BLI_INLINE void rcti_scale_coords(rcti *scaled_rect, const rcti *rect,
1208                                   const float scale)
1209 {
1210         scaled_rect->xmin = rect->xmin * scale;
1211         scaled_rect->ymin = rect->ymin * scale;
1212         scaled_rect->xmax = rect->xmax * scale;
1213         scaled_rect->ymax = rect->ymax * scale;
1214 }
1215
1216 static void render_update_resolution(Render *re, const RenderPreview *rp,
1217                                      bool use_border, const rcti *clip_rect)
1218 {
1219         int winx = rp->ar->winx / rp->resolution_divider;
1220         int winy = rp->ar->winy / rp->resolution_divider;
1221         if (use_border) {
1222                 rcti scaled_cliprct;
1223                 rcti_scale_coords(&scaled_cliprct, clip_rect,
1224                                   1.0f / rp->resolution_divider);
1225                 RE_ChangeResolution(re, winx, winy, &scaled_cliprct);
1226         }
1227         else {
1228                 RE_ChangeResolution(re, winx, winy, NULL);
1229         }
1230
1231         if (rp->has_freestyle) {
1232                 if (rp->resolution_divider == BKE_render_preview_pixel_size(&rp->scene->r)) {
1233                         RE_ChangeModeFlag(re, R_EDGE_FRS, false);
1234                 }
1235                 else {
1236                         RE_ChangeModeFlag(re, R_EDGE_FRS, true);
1237                 }
1238         }
1239 }
1240
1241 static void render_view3d_startjob(void *customdata, short *stop, short *do_update, float *UNUSED(progress))
1242 {
1243         RenderPreview *rp = customdata;
1244         Render *re;
1245         RenderStats *rstats;
1246         rctf viewplane;
1247         rcti cliprct;
1248         float clipsta, clipend, pixsize;
1249         bool orth, restore = 0;
1250         char name[32];
1251         int update_flag;
1252         bool use_border;
1253         int ob_inst_update_flag = 0;
1254
1255         update_flag = rp->engine->job_update_flag;
1256         rp->engine->job_update_flag = 0;
1257
1258         //printf("ma %d res %d view %d db %d\n", update_flag & PR_UPDATE_MATERIAL, update_flag & PR_UPDATE_RENDERSIZE, update_flag & PR_UPDATE_VIEW, update_flag & PR_UPDATE_DATABASE);
1259
1260         G.is_break = false;
1261         
1262         if (false == render_view3d_get_rects(rp->depsgraph, rp->ar, rp->v3d, rp->rv3d, &viewplane, rp->engine, &clipsta, &clipend, &pixsize, &orth))
1263                 return;
1264         
1265         rp->stop = stop;
1266         rp->do_update = do_update;
1267
1268         // printf("Enter previewrender\n");
1269         
1270         /* ok, are we rendering all over? */
1271         sprintf(name, "View3dPreview %p", (void *)rp->ar);
1272         re = rp->engine->re = RE_GetRender(name);
1273         
1274         /* set this always, rp is different for each job */
1275         RE_test_break_cb(re, rp, render_view3d_break);
1276         RE_display_update_cb(re, rp, render_view3d_display_update);
1277         RE_stats_draw_cb(re, rp, render_view3d_renderinfo_cb);
1278         
1279         rstats = RE_GetStats(re);
1280
1281         if (update_flag & PR_UPDATE_VIEW) {
1282                 Object *object;
1283                 rp->resolution_divider = rp->start_resolution_divider;
1284
1285                 /* Same as database_init_objects(), loop over all objects.
1286                  * We might consider de-duplicating the code between this two cases.
1287                  */
1288                 for (object = rp->bmain->object.first; object; object = object->id.next) {
1289                         float mat[4][4];
1290                         mul_m4_m4m4(mat, rp->viewmat, object->obmat);
1291                         invert_m4_m4(object->imat_ren, mat);
1292                 }
1293         }
1294
1295         use_border = render_view3d_disprect(rp->scene, rp->depsgraph,
1296                                             rp->ar, rp->v3d, rp->rv3d,
1297                                             &cliprct);
1298
1299         if ((update_flag & (PR_UPDATE_RENDERSIZE | PR_UPDATE_DATABASE | PR_UPDATE_VIEW)) || rstats->convertdone == 0) {
1300                 RenderData rdata;
1301
1302                 /* no osa, blur, seq, layers, savebuffer etc for preview render */
1303                 rdata = rp->scene->r;
1304                 rdata.mode &= ~(R_OSA | R_MBLUR | R_BORDER | R_PANORAMA);
1305                 rdata.scemode &= ~(R_DOSEQ | R_DOCOMP | R_FREE_IMAGE | R_EXR_TILE_FILE | R_FULL_SAMPLE);
1306                 rdata.scemode |= R_VIEWPORT_PREVIEW;
1307
1308                 /* we do use layers, but only active */
1309                 rdata.scemode |= R_SINGLE_LAYER;
1310
1311                 /* initalize always */
1312                 if (use_border) {
1313                         rdata.mode |= R_BORDER;
1314                         RE_InitState(re, NULL, &rdata, &rp->scene->view_layers, rp->scene->active_view_layer, &rp->scene->view_render, NULL, rp->ar->winx, rp->ar->winy, &cliprct);
1315                 }
1316                 else
1317                         RE_InitState(re, NULL, &rdata, &rp->scene->view_layers, rp->scene->active_view_layer, &rp->scene->view_render, NULL, rp->ar->winx, rp->ar->winy, NULL);
1318         }
1319
1320         if (orth)
1321                 RE_SetOrtho(re, &viewplane, clipsta, clipend);
1322         else
1323                 RE_SetWindow(re, &viewplane, clipsta, clipend);
1324
1325         RE_SetPixelSize(re, pixsize);
1326         
1327         if ((update_flag & PR_UPDATE_DATABASE) || rstats->convertdone == 0) {
1328                 unsigned int lay = rp->scene->lay;
1329
1330                 /* allow localview render for objects with lights in normal layers */
1331                 if (rp->v3d->lay & 0xFF000000)
1332                         lay |= rp->v3d->lay;
1333                 else lay = rp->v3d->lay;
1334                 
1335                 RE_SetView(re, rp->viewmat);
1336
1337                 /* copying blender data while main thread is locked, to avoid crashes */
1338                 WM_job_main_thread_lock_acquire(rp->job);
1339                 RE_Database_Free(re);
1340                 RE_Database_FromScene(re, rp->bmain, rp->scene, lay, 0);                // 0= dont use camera view
1341                 WM_job_main_thread_lock_release(rp->job);
1342
1343                 /* do preprocessing like building raytree, shadows, volumes, SSS */
1344                 RE_Database_Preprocess(rp->eval_ctx, re);
1345
1346                 /* conversion not completed, need to do it again */
1347                 if (!rstats->convertdone) {
1348                         if (render_view3d_is_valid(rp)) {
1349                                 rp->engine->job_update_flag |= PR_UPDATE_DATABASE;
1350                         }
1351                 }
1352
1353                 // printf("dbase update\n");
1354         }
1355         else {
1356                 // printf("dbase rotate\n");
1357                 RE_DataBase_IncrementalView(re, rp->viewmat, 0);
1358                 restore = 1;
1359         }
1360
1361         RE_DataBase_ApplyWindow(re);
1362
1363         /* OK, can we enter render code? */
1364         if (rstats->convertdone) {
1365                 bool first_time = true;
1366
1367                 if (update_flag & PR_UPDATE_VIEW) {
1368                         ob_inst_update_flag |= RE_OBJECT_INSTANCES_UPDATE_VIEW;
1369                 }
1370
1371                 RE_updateRenderInstances(re, ob_inst_update_flag);
1372
1373                 for (;;) {
1374                         int pixel_size = BKE_render_preview_pixel_size(&rp->scene->r);
1375                         if (first_time == false) {
1376                                 if (restore)
1377                                         RE_DataBase_IncrementalView(re, rp->viewmat, 1);
1378
1379                                 rp->resolution_divider = MAX2(rp->resolution_divider / 2, pixel_size);
1380                                 *do_update = 1;
1381
1382                                 render_update_resolution(re, rp, use_border, &cliprct);
1383
1384                                 RE_DataBase_IncrementalView(re, rp->viewmat, 0);
1385                                 RE_DataBase_ApplyWindow(re);
1386                                 restore = 1;
1387                         }
1388                         else {
1389                                 render_update_resolution(re, rp, use_border, &cliprct);
1390                         }
1391
1392                         RE_TileProcessor(re);
1393
1394                         first_time = false;
1395
1396                         if (*stop || rp->resolution_divider == pixel_size) {
1397                                 break;
1398                         }
1399                 }
1400
1401                 /* always rotate back */
1402                 if (restore)
1403                         RE_DataBase_IncrementalView(re, rp->viewmat, 1);
1404         }
1405 }
1406
1407 static void render_view3d_free(void *customdata)
1408 {
1409         RenderPreview *rp = customdata;
1410         DEG_evaluation_context_free(rp->eval_ctx);
1411         
1412         MEM_freeN(rp);
1413 }
1414
1415 static bool render_view3d_flag_changed(RenderEngine *engine, const bContext *C)
1416 {
1417         RegionView3D *rv3d = CTX_wm_region_view3d(C);
1418         View3D *v3d = CTX_wm_view3d(C);
1419         ARegion *ar = CTX_wm_region(C);
1420         Scene *scene = CTX_data_scene(C);
1421         Depsgraph *depsgraph = CTX_data_depsgraph(C);
1422         Render *re;
1423         rctf viewplane;
1424         rcti disprect;
1425         float clipsta, clipend;
1426         bool orth;
1427         int job_update_flag = 0;
1428         char name[32];
1429         
1430         /* ensure render engine exists */
1431         re = engine->re;
1432
1433         if (!re) {
1434                 sprintf(name, "View3dPreview %p", (void *)ar);
1435                 re = engine->re = RE_GetRender(name);
1436                 if (!re)
1437                         re = engine->re = RE_NewRender(name);
1438
1439                 engine->update_flag |= RE_ENGINE_UPDATE_DATABASE;
1440         }
1441
1442         /* check update_flag */
1443         if (engine->update_flag & RE_ENGINE_UPDATE_MA)
1444                 job_update_flag |= PR_UPDATE_MATERIAL;
1445         
1446         if (engine->update_flag & RE_ENGINE_UPDATE_OTHER)
1447                 job_update_flag |= PR_UPDATE_MATERIAL;
1448         
1449         if (engine->update_flag & RE_ENGINE_UPDATE_DATABASE) {
1450                 job_update_flag |= PR_UPDATE_DATABASE;
1451
1452                 /* load editmesh */
1453                 Object *obedit = CTX_data_edit_object(C);
1454                 if (obedit) {
1455                         ED_object_editmode_load(obedit);
1456                 }
1457         }
1458         
1459         engine->update_flag = 0;
1460         
1461         /* check if viewport changed */
1462         if (engine->last_winx != ar->winx || engine->last_winy != ar->winy) {
1463                 engine->last_winx = ar->winx;
1464                 engine->last_winy = ar->winy;
1465                 job_update_flag |= PR_UPDATE_RENDERSIZE;
1466         }
1467
1468         if (compare_m4m4(engine->last_viewmat, rv3d->viewmat, 0.00001f) == 0) {
1469                 copy_m4_m4(engine->last_viewmat, rv3d->viewmat);
1470                 job_update_flag |= PR_UPDATE_VIEW;
1471         }
1472         
1473         render_view3d_get_rects(depsgraph, ar, v3d, rv3d, &viewplane, engine, &clipsta, &clipend, NULL, &orth);
1474         
1475         if (BLI_rctf_compare(&viewplane, &engine->last_viewplane, 0.00001f) == 0) {
1476                 engine->last_viewplane = viewplane;
1477                 job_update_flag |= PR_UPDATE_VIEW;
1478         }
1479         
1480         render_view3d_disprect(scene, depsgraph, ar, v3d, rv3d, &disprect);
1481         if (BLI_rcti_compare(&disprect, &engine->last_disprect) == 0) {
1482                 engine->last_disprect = disprect;
1483                 job_update_flag |= PR_UPDATE_RENDERSIZE;
1484         }
1485
1486         /* any changes? go ahead and rerender */
1487         if (job_update_flag) {
1488                 engine->job_update_flag |= job_update_flag;
1489                 return true;
1490         }
1491
1492         return false;
1493 }
1494
1495 static void render_view3d_do(RenderEngine *engine, const bContext *C)
1496 {
1497         wmJob *wm_job;
1498         RenderPreview *rp;
1499         Scene *scene = CTX_data_scene(C);
1500         Depsgraph *depsgraph = CTX_data_depsgraph(C);
1501         ARegion *ar = CTX_wm_region(C);
1502         int width = ar->winx, height = ar->winy;
1503         int divider = BKE_render_preview_pixel_size(&scene->r);
1504         int resolution_threshold = scene->r.preview_start_resolution *
1505                                    scene->r.preview_start_resolution;
1506
1507         if (CTX_wm_window(C) == NULL)
1508                 return;
1509         if (!render_view3d_flag_changed(engine, C))
1510                 return;
1511
1512         wm_job = WM_jobs_get(CTX_wm_manager(C), CTX_wm_window(C), CTX_wm_region(C), "Render Preview",
1513                              WM_JOB_EXCL_RENDER, WM_JOB_TYPE_RENDER_PREVIEW);
1514         rp = MEM_callocN(sizeof(RenderPreview), "render preview");
1515         rp->job = wm_job;
1516
1517         while (width * height > resolution_threshold) {
1518                 width = max_ii(1, width / 2);
1519                 height = max_ii(1, height / 2);
1520                 divider *= 2;
1521         }
1522
1523         /* customdata for preview thread */
1524         rp->scene = scene;
1525         rp->depsgraph = depsgraph;
1526         rp->eval_ctx = DEG_evaluation_context_new(DAG_EVAL_PREVIEW);
1527         CTX_data_eval_ctx(C, rp->eval_ctx);
1528         rp->engine = engine;
1529         rp->sa = CTX_wm_area(C);
1530         rp->ar = CTX_wm_region(C);
1531         rp->v3d = rp->sa->spacedata.first;
1532         rp->rv3d = CTX_wm_region_view3d(C);
1533         rp->bmain = CTX_data_main(C);
1534         rp->resolution_divider = divider;
1535         rp->start_resolution_divider = divider;
1536         rp->has_freestyle = (scene->r.mode & R_EDGE_FRS) != 0;
1537         copy_m4_m4(rp->viewmat, rp->rv3d->viewmat);
1538         
1539         /* clear info text */
1540         engine->text[0] = '\0';
1541         
1542         /* setup job */
1543         WM_jobs_customdata_set(wm_job, rp, render_view3d_free);
1544         WM_jobs_timer(wm_job, 0.1, NC_SPACE | ND_SPACE_VIEW3D, NC_SPACE | ND_SPACE_VIEW3D);
1545         WM_jobs_callbacks(wm_job, render_view3d_startjob, NULL, NULL, NULL);
1546         
1547         WM_jobs_start(CTX_wm_manager(C), wm_job);
1548         
1549         engine->flag &= ~RE_ENGINE_DO_UPDATE;
1550 }
1551
1552 /* callback for render engine, on changes */
1553 void render_view3d_update(RenderEngine *engine, const bContext *C)
1554 {       
1555         /* this shouldn't be needed and causes too many database rebuilds, but we
1556          * aren't actually tracking updates for all relevant datablocks so this is
1557          * a catch-all for updates */
1558         engine->update_flag |= RE_ENGINE_UPDATE_DATABASE;
1559
1560         render_view3d_do(engine, C);
1561 }
1562
1563 void render_view3d_draw(RenderEngine *engine, const bContext *C)
1564 {
1565         Render *re = engine->re;
1566         RenderResult rres;
1567         char name[32];
1568         
1569         render_view3d_do(engine, C);
1570         
1571         if (re == NULL) {
1572                 sprintf(name, "View3dPreview %p", (void *)CTX_wm_region(C));
1573                 re = RE_GetRender(name);
1574         
1575                 if (re == NULL) return;
1576         }
1577         
1578         /* Viewport render preview doesn't support multiview, view hardcoded to 0 */
1579         RE_AcquireResultImage(re, &rres, 0);
1580         
1581         if (rres.rectf) {
1582                 RegionView3D *rv3d = CTX_wm_region_view3d(C);
1583                 View3D *v3d = CTX_wm_view3d(C);
1584                 Scene *scene = CTX_data_scene(C);
1585                 Depsgraph *depsgraph = CTX_data_depsgraph(C);
1586                 ARegion *ar = CTX_wm_region(C);
1587                 bool force_fallback = false;
1588                 bool need_fallback = true;
1589                 float dither = scene->r.dither_intensity;
1590                 float scale_x, scale_y;
1591                 rcti clip_rect;
1592                 int xof, yof;
1593
1594                 if (render_view3d_disprect(scene, depsgraph, ar, v3d, rv3d, &clip_rect)) {
1595                         scale_x = (float) BLI_rcti_size_x(&clip_rect) / rres.rectx;
1596                         scale_y = (float) BLI_rcti_size_y(&clip_rect) / rres.recty;
1597                         xof = clip_rect.xmin;
1598                         yof = clip_rect.ymin;
1599                 }
1600                 else {
1601                         scale_x = (float) ar->winx / rres.rectx;
1602                         scale_y = (float) ar->winy / rres.recty;
1603                         xof = rres.xof;
1604                         yof = rres.yof;
1605                 }
1606
1607                 /* If user decided not to use GLSL, fallback to glaDrawPixelsAuto */
1608                 force_fallback |= (U.image_draw_method != IMAGE_DRAW_METHOD_GLSL);
1609
1610                 /* Try using GLSL display transform. */
1611                 if (force_fallback == false) {
1612                         if (IMB_colormanagement_setup_glsl_draw(&scene->view_settings, &scene->display_settings, dither, true)) {
1613                                 glEnable(GL_BLEND);
1614                                 IMMDrawPixelsTexState state = immDrawPixelsTexSetup(GPU_SHADER_2D_IMAGE_COLOR);
1615                                 immDrawPixelsTex(&state, xof, yof, rres.rectx, rres.recty,
1616                                                  GL_RGBA, GL_FLOAT, GL_NEAREST, rres.rectf,
1617                                                  scale_x, scale_y, NULL);;
1618                                 glDisable(GL_BLEND);
1619
1620                                 IMB_colormanagement_finish_glsl_draw();
1621                                 need_fallback = false;
1622                         }
1623                 }
1624
1625                 /* If GLSL failed, use old-school CPU-based transform. */
1626                 if (need_fallback) {
1627                         unsigned char *display_buffer = MEM_mallocN(4 * rres.rectx * rres.recty * sizeof(char),
1628                                                                     "render_view3d_draw");
1629
1630                         IMB_colormanagement_buffer_make_display_space(rres.rectf, display_buffer, rres.rectx, rres.recty,
1631                                                                       4, dither, &scene->view_settings, &scene->display_settings);
1632
1633                         glEnable(GL_BLEND);
1634                         IMMDrawPixelsTexState state = immDrawPixelsTexSetup(GPU_SHADER_2D_IMAGE_COLOR);
1635                         immDrawPixelsTex(&state, xof, yof, rres.rectx, rres.recty,
1636                                          GL_RGBA, GL_UNSIGNED_BYTE,
1637                                          GL_NEAREST, display_buffer,
1638                                          scale_x, scale_y, NULL);
1639                         glDisable(GL_BLEND);
1640
1641                         MEM_freeN(display_buffer);
1642                 }
1643         }
1644
1645         RE_ReleaseResultImage(re);
1646 }
1647
1648 void ED_viewport_render_kill_jobs(wmWindowManager *wm,
1649                                   Main *bmain,
1650                                   bool free_database)
1651 {
1652         bScreen *sc;
1653         ScrArea *sa;
1654         ARegion *ar;
1655
1656         if (!wm)
1657                 return;
1658
1659         /* kill all actively running jobs */
1660         WM_jobs_kill(wm, NULL, render_view3d_startjob);
1661
1662         /* loop over 3D view render engines */
1663         for (sc = bmain->screen.first; sc; sc = sc->id.next) {
1664                 for (sa = sc->areabase.first; sa; sa = sa->next) {
1665                         if (sa->spacetype != SPACE_VIEW3D)
1666                                 continue;
1667                         
1668                         for (ar = sa->regionbase.first; ar; ar = ar->next) {
1669                                 RegionView3D *rv3d;
1670                                 
1671                                 if (ar->regiontype != RGN_TYPE_WINDOW)
1672                                         continue;
1673                                 
1674                                 rv3d = ar->regiondata;
1675
1676                                 if (rv3d->render_engine) {
1677                                         /* free render database now before we change data, because
1678                                          * RE_Database_Free will also loop over blender data */
1679                                         if (free_database) {
1680                                                 char name[32];
1681                                                 Render *re;
1682
1683                                                 sprintf(name, "View3dPreview %p", (void *)ar);
1684                                                 re = RE_GetRender(name);
1685
1686                                                 if (re)
1687                                                         RE_Database_Free(re);
1688
1689                                                 /* tag render engine to update entire database */
1690                                                 rv3d->render_engine->update_flag |= RE_ENGINE_UPDATE_DATABASE;
1691                                         }
1692                                         else {
1693                                                 /* quick shader update */
1694                                                 rv3d->render_engine->update_flag |= RE_ENGINE_UPDATE_MA;
1695                                         }
1696                                 }
1697                         }
1698                 }
1699         }
1700 }
1701
1702 Scene *ED_render_job_get_scene(const bContext *C)
1703 {
1704         wmWindowManager *wm = CTX_wm_manager(C);
1705         RenderJob *rj = (RenderJob *)WM_jobs_customdata_from_type(wm, WM_JOB_TYPE_RENDER);
1706         
1707         if (rj)
1708                 return rj->scene;
1709         
1710         return NULL;
1711 }
1712
1713 Scene *ED_render_job_get_current_scene(const bContext *C)
1714 {
1715         wmWindowManager *wm = CTX_wm_manager(C);
1716         RenderJob *rj = (RenderJob *)WM_jobs_customdata_from_type(wm, WM_JOB_TYPE_RENDER);
1717         if (rj) {
1718                 return rj->current_scene;
1719         }
1720         return NULL;
1721 }
1722
1723 /* Motion blur curve preset */
1724
1725 static int render_shutter_curve_preset_exec(bContext *C, wmOperator *op)
1726 {
1727         Scene *scene = CTX_data_scene(C);
1728         CurveMapping *mblur_shutter_curve = &scene->r.mblur_shutter_curve;
1729         CurveMap *cm = mblur_shutter_curve->cm;
1730         int preset = RNA_enum_get(op->ptr, "shape");
1731
1732         cm->flag &= ~CUMA_EXTEND_EXTRAPOLATE;
1733         mblur_shutter_curve->preset = preset;
1734         curvemap_reset(cm,
1735                        &mblur_shutter_curve->clipr,
1736                        mblur_shutter_curve->preset,
1737                        CURVEMAP_SLOPE_POS_NEG);
1738         curvemapping_changed(mblur_shutter_curve, false);
1739
1740         return OPERATOR_FINISHED;
1741 }
1742
1743 void RENDER_OT_shutter_curve_preset(wmOperatorType *ot)
1744 {
1745         PropertyRNA *prop;
1746         static const EnumPropertyItem prop_shape_items[] = {
1747                 {CURVE_PRESET_SHARP, "SHARP", 0, "Sharp", ""},
1748                 {CURVE_PRESET_SMOOTH, "SMOOTH", 0, "Smooth", ""},
1749                 {CURVE_PRESET_MAX, "MAX", 0, "Max", ""},
1750                 {CURVE_PRESET_LINE, "LINE", 0, "Line", ""},
1751                 {CURVE_PRESET_ROUND, "ROUND", 0, "Round", ""},
1752                 {CURVE_PRESET_ROOT, "ROOT", 0, "Root", ""},
1753                 {0, NULL, 0, NULL, NULL}};
1754
1755         ot->name = "Shutter Curve Preset";
1756         ot->description = "Set shutter curve";
1757         ot->idname = "RENDER_OT_shutter_curve_preset";
1758
1759         ot->exec = render_shutter_curve_preset_exec;
1760
1761         prop = RNA_def_enum(ot->srna, "shape", prop_shape_items, CURVE_PRESET_SMOOTH, "Mode", "");
1762         RNA_def_property_translation_context(prop, BLT_I18NCONTEXT_ID_CURVE); /* Abusing id_curve :/ */
1763 }