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