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