Implement GPU-side dither
[blender-staging.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_blenlib.h"
37 #include "BLI_math.h"
38 #include "BLI_threads.h"
39 #include "BLI_utildefines.h"
40
41 #include "PIL_time.h"
42
43 #include "BLF_translation.h"
44
45 #include "DNA_object_types.h"
46 #include "DNA_scene_types.h"
47 #include "DNA_view3d_types.h"
48 #include "DNA_userdef_types.h"
49
50 #include "BKE_blender.h"
51 #include "BKE_context.h"
52 #include "BKE_depsgraph.h"
53 #include "BKE_freestyle.h"
54 #include "BKE_global.h"
55 #include "BKE_image.h"
56 #include "BKE_library.h"
57 #include "BKE_main.h"
58 #include "BKE_node.h"
59 #include "BKE_multires.h"
60 #include "BKE_paint.h"
61 #include "BKE_report.h"
62 #include "BKE_sequencer.h"
63 #include "BKE_screen.h"
64 #include "BKE_scene.h"
65
66 #include "WM_api.h"
67 #include "WM_types.h"
68
69 #include "ED_object.h"
70 #include "ED_render.h"
71 #include "ED_screen.h"
72 #include "ED_view3d.h"
73
74 #include "RE_pipeline.h"
75 #include "RE_engine.h"
76
77 #include "IMB_colormanagement.h"
78 #include "IMB_imbuf.h"
79 #include "IMB_imbuf_types.h"
80
81 #include "GPU_extensions.h"
82
83 #include "BIF_gl.h"
84 #include "BIF_glutil.h"
85
86 #include "RNA_access.h"
87 #include "RNA_define.h"
88
89 #include "wm_window.h"
90
91 #include "render_intern.h"
92
93 /* Render Callbacks */
94 static int render_break(void *rjv);
95
96 /* called inside thread! */
97 void image_buffer_rect_update(Scene *scene, RenderResult *rr, ImBuf *ibuf, volatile rcti *renrect)
98 {
99         float *rectf = NULL;
100         int ymin, ymax, xmin, xmax;
101         int rymin, rxmin;
102
103         /* if renrect argument, we only refresh scanlines */
104         if (renrect) {
105                 /* if (ymax == recty), rendering of layer is ready, we should not draw, other things happen... */
106                 if (rr->renlay == NULL || renrect->ymax >= rr->recty)
107                         return;
108
109                 /* xmin here is first subrect x coord, xmax defines subrect width */
110                 xmin = renrect->xmin + rr->crop;
111                 xmax = renrect->xmax - xmin + rr->crop;
112                 if (xmax < 2)
113                         return;
114
115                 ymin = renrect->ymin + rr->crop;
116                 ymax = renrect->ymax - ymin + rr->crop;
117                 if (ymax < 2)
118                         return;
119                 renrect->ymin = renrect->ymax;
120
121         }
122         else {
123                 xmin = ymin = rr->crop;
124                 xmax = rr->rectx - 2 * rr->crop;
125                 ymax = rr->recty - 2 * rr->crop;
126         }
127
128         /* xmin ymin is in tile coords. transform to ibuf */
129         rxmin = rr->tilerect.xmin + xmin;
130         if (rxmin >= ibuf->x) return;
131         rymin = rr->tilerect.ymin + ymin;
132         if (rymin >= ibuf->y) return;
133
134         if (rxmin + xmax > ibuf->x)
135                 xmax = ibuf->x - rxmin;
136         if (rymin + ymax > ibuf->y)
137                 ymax = ibuf->y - rymin;
138
139         if (xmax < 1 || ymax < 1) return;
140
141         /* find current float rect for display, first case is after composite... still weak */
142         if (rr->rectf)
143                 rectf = rr->rectf;
144         else {
145                 if (rr->rect32) {
146                         /* special case, currently only happens with sequencer rendering,
147                          * which updates the whole frame, so we can only mark display buffer
148                          * as invalid here (sergey)
149                          */
150                         ibuf->userflags |= IB_DISPLAY_BUFFER_INVALID;
151                         return;
152                 }
153                 else {
154                         if (rr->renlay == NULL || rr->renlay->rectf == NULL) return;
155                         rectf = rr->renlay->rectf;
156                 }
157         }
158         if (rectf == NULL) return;
159
160         if (ibuf->rect == NULL)
161                 imb_addrectImBuf(ibuf);
162
163         rectf += 4 * (rr->rectx * ymin + xmin);
164
165         IMB_partial_display_buffer_update(ibuf, rectf, NULL, rr->rectx, rxmin, rymin,
166                                           &scene->view_settings, &scene->display_settings,
167                                           rxmin, rymin, rxmin + xmax, rymin + ymax, true);
168 }
169
170 /* ****************************** render invoking ***************** */
171
172 /* set callbacks, exported to sequence render too.
173  * Only call in foreground (UI) renders. */
174
175 static void screen_render_scene_layer_set(wmOperator *op, Main *mainp, Scene **scene, SceneRenderLayer **srl)
176 {
177         /* single layer re-render */
178         if (RNA_struct_property_is_set(op->ptr, "scene")) {
179                 Scene *scn;
180                 char scene_name[MAX_ID_NAME - 2];
181
182                 RNA_string_get(op->ptr, "scene", scene_name);
183                 scn = (Scene *)BLI_findstring(&mainp->scene, scene_name, offsetof(ID, name) + 2);
184                 
185                 if (scn) {
186                         /* camera switch wont have updated */
187                         scn->r.cfra = (*scene)->r.cfra;
188                         BKE_scene_camera_switch_update(scn);
189
190                         *scene = scn;
191                 }
192         }
193
194         if (RNA_struct_property_is_set(op->ptr, "layer")) {
195                 SceneRenderLayer *rl;
196                 char rl_name[RE_MAXNAME];
197
198                 RNA_string_get(op->ptr, "layer", rl_name);
199                 rl = (SceneRenderLayer *)BLI_findstring(&(*scene)->r.layers, rl_name, offsetof(SceneRenderLayer, name));
200                 
201                 if (rl)
202                         *srl = rl;
203         }
204 }
205
206 /* executes blocking render */
207 static int screen_render_exec(bContext *C, wmOperator *op)
208 {
209         Scene *scene = CTX_data_scene(C);
210         SceneRenderLayer *srl = NULL;
211         Render *re;
212         Image *ima;
213         View3D *v3d = CTX_wm_view3d(C);
214         Main *mainp = CTX_data_main(C);
215         unsigned int lay_override;
216         const short is_animation = RNA_boolean_get(op->ptr, "animation");
217         const short is_write_still = RNA_boolean_get(op->ptr, "write_still");
218         struct Object *camera_override = v3d ? V3D_CAMERA_LOCAL(v3d) : NULL;
219
220         /* custom scene and single layer re-render */
221         screen_render_scene_layer_set(op, mainp, &scene, &srl);
222
223         if (!is_animation && is_write_still && BKE_imtype_is_movie(scene->r.im_format.imtype)) {
224                 BKE_report(op->reports, RPT_ERROR, "Cannot write a single file with an animation format selected");
225                 return OPERATOR_CANCELLED;
226         }
227
228         re = RE_NewRender(scene->id.name);
229         lay_override = (v3d && v3d->lay != scene->lay) ? v3d->lay : 0;
230
231         G.is_break = FALSE;
232         RE_test_break_cb(re, NULL, render_break);
233
234         ima = BKE_image_verify_viewer(IMA_TYPE_R_RESULT, "Render Result");
235         BKE_image_signal(ima, NULL, IMA_SIGNAL_FREE);
236         BKE_image_backup_render(scene, ima);
237
238         /* cleanup sequencer caches before starting user triggered render.
239          * otherwise, invalidated cache entries can make their way into
240          * the output rendering. We can't put that into RE_BlenderFrame,
241          * since sequence rendering can call that recursively... (peter) */
242         BKE_sequencer_cache_cleanup();
243
244         RE_SetReports(re, op->reports);
245
246         if (is_animation)
247                 RE_BlenderAnim(re, mainp, scene, camera_override, lay_override, scene->r.sfra, scene->r.efra, scene->r.frame_step);
248         else
249                 RE_BlenderFrame(re, mainp, scene, srl, camera_override, lay_override, scene->r.cfra, is_write_still);
250
251         RE_SetReports(re, NULL);
252
253         // no redraw needed, we leave state as we entered it
254         ED_update_for_newframe(mainp, scene, 1);
255
256         WM_event_add_notifier(C, NC_SCENE | ND_RENDER_RESULT, scene);
257
258         return OPERATOR_FINISHED;
259 }
260
261 typedef struct RenderJob {
262         Main *main;
263         Scene *scene;
264         Render *re;
265         wmWindow *win;
266         SceneRenderLayer *srl;
267         struct Object *camera_override;
268         int lay_override;
269         bool v3d_override;
270         short anim, write_still;
271         Image *image;
272         ImageUser iuser;
273         bool image_outdated;
274         short *stop;
275         short *do_update;
276         float *progress;
277         ReportList *reports;
278 } RenderJob;
279
280 static void render_freejob(void *rjv)
281 {
282         RenderJob *rj = rjv;
283
284         MEM_freeN(rj);
285 }
286
287 /* str is IMA_MAX_RENDER_TEXT in size */
288 static void make_renderinfo_string(RenderStats *rs, Scene *scene, bool v3d_override, char *str)
289 {
290         char info_time_str[32]; // used to be extern to header_info.c
291         uintptr_t mem_in_use, mmap_in_use, peak_memory;
292         float megs_used_memory, mmap_used_memory, megs_peak_memory;
293         char *spos = str;
294
295         mem_in_use = MEM_get_memory_in_use();
296         mmap_in_use = MEM_get_mapped_memory_in_use();
297         peak_memory = MEM_get_peak_memory();
298
299         megs_used_memory = (mem_in_use - mmap_in_use) / (1024.0 * 1024.0);
300         mmap_used_memory = (mmap_in_use) / (1024.0 * 1024.0);
301         megs_peak_memory = (peak_memory) / (1024.0 * 1024.0);
302
303         /* local view */
304         if (rs->localview)
305                 spos += sprintf(spos, "%s | ", IFACE_("3D Local View"));
306         else if (v3d_override)
307                 spos += sprintf(spos, "%s | ", IFACE_("3D View"));
308
309         /* frame number */
310         spos += sprintf(spos, IFACE_("Frame:%d "), (scene->r.cfra));
311
312         /* previous and elapsed time */
313         BLI_timestr(rs->lastframetime, info_time_str, sizeof(info_time_str));
314
315         if (rs->infostr && rs->infostr[0]) {
316                 if (rs->lastframetime != 0.0)
317                         spos += sprintf(spos, IFACE_("| Last:%s "), info_time_str);
318                 else
319                         spos += sprintf(spos, "| ");
320
321                 BLI_timestr(PIL_check_seconds_timer() - rs->starttime, info_time_str, sizeof(info_time_str));
322         }
323         else
324                 spos += sprintf(spos, "| ");
325
326         spos += sprintf(spos, IFACE_("Time:%s "), info_time_str);
327
328         /* statistics */
329         if (rs->statstr) {
330                 if (rs->statstr[0]) {
331                         spos += sprintf(spos, "| %s ", rs->statstr);
332                 }
333         }
334         else {
335                 if (rs->totvert || rs->totface || rs->tothalo || rs->totstrand || rs->totlamp)
336                         spos += sprintf(spos, "| ");
337
338                 if (rs->totvert) spos += sprintf(spos, IFACE_("Ve:%d "), rs->totvert);
339                 if (rs->totface) spos += sprintf(spos, IFACE_("Fa:%d "), rs->totface);
340                 if (rs->tothalo) spos += sprintf(spos, IFACE_("Ha:%d "), rs->tothalo);
341                 if (rs->totstrand) spos += sprintf(spos, IFACE_("St:%d "), rs->totstrand);
342                 if (rs->totlamp) spos += sprintf(spos, IFACE_("La:%d "), rs->totlamp);
343
344                 if (rs->mem_peak == 0.0f)
345                         spos += sprintf(spos, IFACE_("| Mem:%.2fM (%.2fM, Peak %.2fM) "),
346                                         megs_used_memory, mmap_used_memory, megs_peak_memory);
347                 else
348                         spos += sprintf(spos, IFACE_("| Mem:%.2fM, Peak: %.2fM "), rs->mem_used, rs->mem_peak);
349
350                 if (rs->curfield)
351                         spos += sprintf(spos, IFACE_("Field %d "), rs->curfield);
352                 if (rs->curblur)
353                         spos += sprintf(spos, IFACE_("Blur %d "), rs->curblur);
354         }
355
356         /* full sample */
357         if (rs->curfsa)
358                 spos += sprintf(spos, IFACE_("| Full Sample %d "), rs->curfsa);
359         
360         /* extra info */
361         if (rs->infostr && rs->infostr[0])
362                 spos += sprintf(spos, "| %s ", rs->infostr);
363
364         /* very weak... but 512 characters is quite safe */
365         if (spos >= str + IMA_MAX_RENDER_TEXT)
366                 if (G.debug & G_DEBUG)
367                         printf("WARNING! renderwin text beyond limit\n");
368
369 }
370
371 static void image_renderinfo_cb(void *rjv, RenderStats *rs)
372 {
373         RenderJob *rj = rjv;
374         RenderResult *rr;
375
376         rr = RE_AcquireResultRead(rj->re);
377
378         if (rr) {
379                 /* malloc OK here, stats_draw is not in tile threads */
380                 if (rr->text == NULL)
381                         rr->text = MEM_callocN(IMA_MAX_RENDER_TEXT, "rendertext");
382
383                 make_renderinfo_string(rs, rj->scene, rj->v3d_override, rr->text);
384         }
385
386         RE_ReleaseResult(rj->re);
387
388         /* make jobs timer to send notifier */
389         *(rj->do_update) = TRUE;
390
391 }
392
393 static void render_progress_update(void *rjv, float progress)
394 {
395         RenderJob *rj = rjv;
396         
397         if (rj->progress && *rj->progress != progress) {
398                 *rj->progress = progress;
399
400                 /* make jobs timer to send notifier */
401                 *(rj->do_update) = TRUE;
402         }
403 }
404
405 static void image_rect_update(void *rjv, RenderResult *rr, volatile rcti *renrect)
406 {
407         RenderJob *rj = rjv;
408         Image *ima = rj->image;
409         ImBuf *ibuf;
410         void *lock;
411
412         /* only update if we are displaying the slot being rendered */
413         if (ima->render_slot != ima->last_render_slot) {
414                 rj->image_outdated = true;
415                 return;
416         }
417         else if (rj->image_outdated) {
418                 /* update entire render */
419                 rj->image_outdated = false;
420                 BKE_image_signal(ima, NULL, IMA_SIGNAL_COLORMANAGE);
421                 *(rj->do_update) = TRUE;
422                 return;
423         }
424
425         /* update part of render */
426         ibuf = BKE_image_acquire_ibuf(ima, &rj->iuser, &lock);
427         if (ibuf) {
428                 image_buffer_rect_update(rj->scene, rr, ibuf, renrect);
429
430                 /* make jobs timer to send notifier */
431                 *(rj->do_update) = TRUE;
432         }
433         BKE_image_release_ibuf(ima, ibuf, lock);
434 }
435
436 static void render_startjob(void *rjv, short *stop, short *do_update, float *progress)
437 {
438         RenderJob *rj = rjv;
439
440         rj->stop = stop;
441         rj->do_update = do_update;
442         rj->progress = progress;
443
444         RE_SetReports(rj->re, rj->reports);
445
446         if (rj->anim)
447                 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);
448         else
449                 RE_BlenderFrame(rj->re, rj->main, rj->scene, rj->srl, rj->camera_override, rj->lay_override, rj->scene->r.cfra, rj->write_still);
450
451         RE_SetReports(rj->re, NULL);
452 }
453
454 static void render_endjob(void *rjv)
455 {
456         RenderJob *rj = rjv;
457
458         /* this render may be used again by the sequencer without the active 'Render' where the callbacks
459          * would be re-assigned. assign dummy callbacks to avoid referencing freed renderjobs bug [#24508] */
460         RE_InitRenderCB(rj->re);
461
462         if (rj->main != G.main)
463                 free_main(rj->main);
464
465         /* else the frame will not update for the original value */
466         if (rj->anim && !(rj->scene->r.scemode & R_NO_FRAME_UPDATE)) {
467                 /* possible this fails of loading new file while rendering */
468                 if (G.main->wm.first) {
469                         ED_update_for_newframe(G.main, rj->scene, 1);
470                 }
471         }
472         
473         /* XXX above function sets all tags in nodes */
474         ntreeCompositClearTags(rj->scene->nodetree);
475         
476         /* potentially set by caller */
477         rj->scene->r.scemode &= ~R_NO_FRAME_UPDATE;
478         
479         if (rj->srl) {
480                 nodeUpdateID(rj->scene->nodetree, &rj->scene->id);
481                 WM_main_add_notifier(NC_NODE | NA_EDITED, rj->scene);
482         }
483
484         /* XXX render stability hack */
485         G.is_rendering = FALSE;
486         WM_main_add_notifier(NC_SCENE | ND_RENDER_RESULT, NULL);
487
488         /* Partial render result will always update display buffer
489          * for first render layer only. This is nice because you'll
490          * see render progress during rendering, but it ends up in
491          * wrong display buffer shown after rendering.
492          *
493          * The code below will mark display buffer as invalid after
494          * rendering in case multiple layers were rendered, which
495          * ensures display buffer matches render layer after
496          * rendering.
497          *
498          * Perhaps proper way would be to toggle active render
499          * layer in image editor and job, so we always display
500          * layer being currently rendered. But this is not so much
501          * trivial at this moment, especially because of external
502          * engine API, so lets use simple and robust way for now
503          *                                          - sergey -
504          */
505         if (rj->scene->r.layers.first != rj->scene->r.layers.last ||
506             rj->image_outdated)
507         {
508                 void *lock;
509                 Image *ima = rj->image;
510                 ImBuf *ibuf = BKE_image_acquire_ibuf(ima, &rj->iuser, &lock);
511
512                 if (ibuf)
513                         ibuf->userflags |= IB_DISPLAY_BUFFER_INVALID;
514
515                 BKE_image_release_ibuf(ima, ibuf, lock);
516         }
517 }
518
519 /* called by render, check job 'stop' value or the global */
520 static int render_breakjob(void *rjv)
521 {
522         RenderJob *rj = rjv;
523
524         if (G.is_break)
525                 return 1;
526         if (rj->stop && *(rj->stop))
527                 return 1;
528         return 0;
529 }
530
531 /* for exec() when there is no render job
532  * note: this wont check for the escape key being pressed, but doing so isnt threadsafe */
533 static int render_break(void *UNUSED(rjv))
534 {
535         if (G.is_break)
536                 return 1;
537         return 0;
538 }
539
540 /* runs in thread, no cursor setting here works. careful with notifiers too (malloc conflicts) */
541 /* maybe need a way to get job send notifer? */
542 static void render_drawlock(void *UNUSED(rjv), int lock)
543 {
544         BKE_spacedata_draw_locks(lock);
545         
546 }
547
548 /* catch esc */
549 static int screen_render_modal(bContext *C, wmOperator *op, const wmEvent *event)
550 {
551         Scene *scene = (Scene *) op->customdata;
552
553         /* no running blender, remove handler and pass through */
554         if (0 == WM_jobs_test(CTX_wm_manager(C), scene, WM_JOB_TYPE_RENDER)) {
555                 return OPERATOR_FINISHED | OPERATOR_PASS_THROUGH;
556         }
557
558         /* running render */
559         switch (event->type) {
560                 case ESCKEY:
561                         return OPERATOR_RUNNING_MODAL;
562                         break;
563         }
564         return OPERATOR_PASS_THROUGH;
565 }
566
567 static void screen_render_cancel(bContext *C, wmOperator *op)
568 {
569         wmWindowManager *wm = CTX_wm_manager(C);
570         Scene *scene = (Scene *) op->customdata;
571
572         /* kill on cancel, because job is using op->reports */
573         WM_jobs_kill_type(wm, scene, WM_JOB_TYPE_RENDER);
574 }
575
576 /* using context, starts job */
577 static int screen_render_invoke(bContext *C, wmOperator *op, const wmEvent *event)
578 {
579         /* new render clears all callbacks */
580         Main *mainp;
581         Scene *scene = CTX_data_scene(C);
582         SceneRenderLayer *srl = NULL;
583         Render *re;
584         wmJob *wm_job;
585         RenderJob *rj;
586         Image *ima;
587         int jobflag;
588         const bool is_animation = RNA_boolean_get(op->ptr, "animation");
589         const bool is_write_still = RNA_boolean_get(op->ptr, "write_still");
590         const bool use_viewport = RNA_boolean_get(op->ptr, "use_viewport");
591         View3D *v3d = use_viewport ? CTX_wm_view3d(C) : NULL;
592         struct Object *camera_override = v3d ? V3D_CAMERA_LOCAL(v3d) : NULL;
593         const char *name;
594         Object *active_object = CTX_data_active_object(C);
595         
596         /* only one render job at a time */
597         if (WM_jobs_test(CTX_wm_manager(C), scene, WM_JOB_TYPE_RENDER))
598                 return OPERATOR_CANCELLED;
599
600         if (!RE_is_rendering_allowed(scene, camera_override, op->reports)) {
601                 return OPERATOR_CANCELLED;
602         }
603
604         if (!is_animation && is_write_still && BKE_imtype_is_movie(scene->r.im_format.imtype)) {
605                 BKE_report(op->reports, RPT_ERROR, "Cannot write a single file with an animation format selected");
606                 return OPERATOR_CANCELLED;
607         }
608         
609         /* stop all running jobs, except screen one. currently previews frustrate Render */
610         WM_jobs_kill_all_except(CTX_wm_manager(C), CTX_wm_screen(C));
611
612         /* get main */
613         if (G.debug_value == 101) {
614                 /* thread-safety experiment, copy main from the undo buffer */
615                 mainp = BKE_undo_get_main(&scene);
616         }
617         else
618                 mainp = CTX_data_main(C);
619
620         /* cancel animation playback */
621         if (ED_screen_animation_playing(CTX_wm_manager(C)))
622                 ED_screen_animation_play(C, 0, 0);
623         
624         /* handle UI stuff */
625         WM_cursor_wait(1);
626
627         /* flush multires changes (for sculpt) */
628         multires_force_render_update(active_object);
629
630         /* flush changes from dynamic topology sculpt */
631         sculptsession_bm_to_me_for_render(active_object);
632
633         /* cleanup sequencer caches before starting user triggered render.
634          * otherwise, invalidated cache entries can make their way into
635          * the output rendering. We can't put that into RE_BlenderFrame,
636          * since sequence rendering can call that recursively... (peter) */
637         BKE_sequencer_cache_cleanup();
638
639         /* get editmode results */
640         ED_object_editmode_load(CTX_data_edit_object(C));
641
642         // store spare
643         // get view3d layer, local layer, make this nice api call to render
644         // store spare
645
646         /* ensure at least 1 area shows result */
647         render_view_open(C, event->x, event->y);
648
649         jobflag = WM_JOB_EXCL_RENDER | WM_JOB_PRIORITY | WM_JOB_PROGRESS;
650         
651         /* custom scene and single layer re-render */
652         screen_render_scene_layer_set(op, mainp, &scene, &srl);
653
654         if (RNA_struct_property_is_set(op->ptr, "layer"))
655                 jobflag |= WM_JOB_SUSPEND;
656
657         /* job custom data */
658         rj = MEM_callocN(sizeof(RenderJob), "render job");
659         rj->main = mainp;
660         rj->scene = scene;
661         rj->win = CTX_wm_window(C);
662         rj->srl = srl;
663         rj->camera_override = camera_override;
664         rj->lay_override = 0;
665         rj->anim = is_animation;
666         rj->write_still = is_write_still && !is_animation;
667         rj->iuser.scene = scene;
668         rj->iuser.ok = 1;
669         rj->reports = op->reports;
670
671         if (v3d) {
672                 if (scene->lay != v3d->lay) {
673                         rj->lay_override = v3d->lay;
674                         rj->v3d_override = true;
675                 }
676                 else if (camera_override && camera_override != scene->camera)
677                         rj->v3d_override = true;
678
679                 if (v3d->localvd)
680                         rj->lay_override |= v3d->localvd->lay;
681         }
682
683         /* setup job */
684         if (RE_seq_render_active(scene, &scene->r)) name = "Sequence Render";
685         else name = "Render";
686
687         wm_job = WM_jobs_get(CTX_wm_manager(C), CTX_wm_window(C), scene, name, jobflag, WM_JOB_TYPE_RENDER);
688         WM_jobs_customdata_set(wm_job, rj, render_freejob);
689         WM_jobs_timer(wm_job, 0.2, NC_SCENE | ND_RENDER_RESULT, 0);
690         WM_jobs_callbacks(wm_job, render_startjob, NULL, NULL, render_endjob);
691
692         /* get a render result image, and make sure it is empty */
693         ima = BKE_image_verify_viewer(IMA_TYPE_R_RESULT, "Render Result");
694         BKE_image_signal(ima, NULL, IMA_SIGNAL_FREE);
695         BKE_image_backup_render(rj->scene, ima);
696         rj->image = ima;
697
698         /* setup new render */
699         re = RE_NewRender(scene->id.name);
700         RE_test_break_cb(re, rj, render_breakjob);
701         RE_draw_lock_cb(re, rj, render_drawlock);
702         RE_display_draw_cb(re, rj, image_rect_update);
703         RE_stats_draw_cb(re, rj, image_renderinfo_cb);
704         RE_progress_cb(re, rj, render_progress_update);
705
706         rj->re = re;
707         G.is_break = FALSE;
708
709         /* store actual owner of job, so modal operator could check for it,
710          * the reason of this is that active scene could change when rendering
711          * several layers from compositor [#31800]
712          */
713         op->customdata = scene;
714
715         WM_jobs_start(CTX_wm_manager(C), wm_job);
716
717         WM_cursor_wait(0);
718         WM_event_add_notifier(C, NC_SCENE | ND_RENDER_RESULT, scene);
719
720         /* we set G.is_rendering here already instead of only in the job, this ensure
721          * main loop or other scene updates are disabled in time, since they may
722          * have started before the job thread */
723         G.is_rendering = TRUE;
724
725         /* add modal handler for ESC */
726         WM_event_add_modal_handler(C, op);
727
728         return OPERATOR_RUNNING_MODAL;
729 }
730
731 /* contextual render, using current scene, view3d? */
732 void RENDER_OT_render(wmOperatorType *ot)
733 {
734         PropertyRNA *prop;
735
736         /* identifiers */
737         ot->name = "Render";
738         ot->description = "Render active scene";
739         ot->idname = "RENDER_OT_render";
740
741         /* api callbacks */
742         ot->invoke = screen_render_invoke;
743         ot->modal = screen_render_modal;
744         ot->cancel = screen_render_cancel;
745         ot->exec = screen_render_exec;
746
747         /*ot->poll = ED_operator_screenactive;*/ /* this isn't needed, causes failer in background mode */
748
749         RNA_def_boolean(ot->srna, "animation", 0, "Animation", "Render files from the animation range of this scene");
750         RNA_def_boolean(ot->srna, "write_still", 0, "Write Image", "Save rendered the image to the output path (used only when animation is disabled)");
751         RNA_def_boolean(ot->srna, "use_viewport", 0, "Use 3D Viewport", "When inside a 3D viewport, use layers and camera of the viewport");
752         prop = RNA_def_string(ot->srna, "layer", "", RE_MAXNAME, "Render Layer", "Single render layer to re-render (used only when animation is disabled)");
753         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
754         prop = RNA_def_string(ot->srna, "scene", "", MAX_ID_NAME - 2, "Scene", "Scene to render, current scene if not specified");
755         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
756 }
757
758
759 /* ************** preview for 3d viewport ***************** */
760
761 #define PR_UPDATE_VIEW                          1
762 #define PR_UPDATE_RENDERSIZE            2
763 #define PR_UPDATE_MATERIAL                      4
764 #define PR_UPDATE_DATABASE                      8
765
766 typedef struct RenderPreview {
767         /* from wmJob */
768         void *owner;
769         short *stop, *do_update;
770         wmJob *job;
771         
772         Scene *scene;
773         ScrArea *sa;
774         ARegion *ar;
775         View3D *v3d;
776         RegionView3D *rv3d;
777         Main *bmain;
778         RenderEngine *engine;
779         
780         float viewmat[4][4];
781 } RenderPreview;
782
783 static int render_view3d_disprect(Scene *scene, ARegion *ar, View3D *v3d, RegionView3D *rv3d, rcti *disprect)
784 {
785         /* copied code from view3d_draw.c */
786         rctf viewborder;
787         int draw_border;
788         
789         if (rv3d->persp == RV3D_CAMOB)
790                 draw_border = (scene->r.mode & R_BORDER) != 0;
791         else
792                 draw_border = (v3d->flag2 & V3D_RENDER_BORDER) != 0;
793
794         if (draw_border) {
795                 if (rv3d->persp == RV3D_CAMOB) {
796                         ED_view3d_calc_camera_border(scene, ar, v3d, rv3d, &viewborder, false);
797                         
798                         disprect->xmin = viewborder.xmin + scene->r.border.xmin * BLI_rctf_size_x(&viewborder);
799                         disprect->ymin = viewborder.ymin + scene->r.border.ymin * BLI_rctf_size_y(&viewborder);
800                         disprect->xmax = viewborder.xmin + scene->r.border.xmax * BLI_rctf_size_x(&viewborder);
801                         disprect->ymax = viewborder.ymin + scene->r.border.ymax * BLI_rctf_size_y(&viewborder);
802                 }
803                 else {
804                         disprect->xmin = v3d->render_border.xmin * ar->winx;
805                         disprect->xmax = v3d->render_border.xmax * ar->winx;
806                         disprect->ymin = v3d->render_border.ymin * ar->winy;
807                         disprect->ymax = v3d->render_border.ymax * ar->winy;
808                 }
809                 
810                 return 1;
811         }
812         
813         BLI_rcti_init(disprect, 0, 0, 0, 0);
814         return 0;
815 }
816
817 /* returns true if OK  */
818 static bool render_view3d_get_rects(ARegion *ar, View3D *v3d, RegionView3D *rv3d, rctf *viewplane, RenderEngine *engine,
819                                     float *r_clipsta, float *r_clipend, float *r_pixsize, bool *r_ortho)
820 {
821         
822         if (ar->winx < 4 || ar->winy < 4) return false;
823         
824         *r_ortho = ED_view3d_viewplane_get(v3d, rv3d, ar->winx, ar->winy, viewplane, r_clipsta, r_clipend, r_pixsize);
825         
826         engine->resolution_x = ar->winx;
827         engine->resolution_y = ar->winy;
828
829         return true;
830 }
831
832 static bool render_view3d_is_valid(RenderPreview *rp)
833 {
834         return (rp->rv3d->render_engine != NULL);
835 }
836
837 /* called by renderer, checks job value */
838 static int render_view3d_break(void *rpv)
839 {
840         RenderPreview *rp = rpv;
841         
842         if (G.is_break)
843                 return 1;
844         
845         /* during render, rv3d->engine can get freed */
846         if (render_view3d_is_valid(rp) == false) {
847                 *rp->stop = 1;
848         }
849         
850         return *(rp->stop);
851 }
852
853 static void render_view3d_draw_update(void *rpv, RenderResult *UNUSED(rr), volatile struct rcti *UNUSED(rect))
854 {
855         RenderPreview *rp = rpv;
856         
857         *(rp->do_update) = TRUE;
858 }
859
860 static void render_view3d_renderinfo_cb(void *rjp, RenderStats *rs)
861 {
862         RenderPreview *rp = rjp;
863
864         /* during render, rv3d->engine can get freed */
865         if (rp->rv3d->render_engine == NULL) {
866                 *rp->stop = 1;
867         }
868         else {
869                 make_renderinfo_string(rs, rp->scene, false, rp->engine->text);
870         
871                 /* make jobs timer to send notifier */
872                 *(rp->do_update) = TRUE;
873         }
874 }
875
876
877 static void render_view3d_startjob(void *customdata, short *stop, short *do_update, float *UNUSED(progress))
878 {
879         RenderPreview *rp = customdata;
880         Render *re;
881         RenderStats *rstats;
882         RenderData rdata;
883         rctf viewplane;
884         rcti cliprct;
885         float clipsta, clipend, pixsize;
886         bool orth, restore = 0;
887         char name[32];
888         int update_flag;
889
890         update_flag = rp->engine->job_update_flag;
891         rp->engine->job_update_flag = 0;
892
893         //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);
894
895         G.is_break = FALSE;
896         
897         if (false == render_view3d_get_rects(rp->ar, rp->v3d, rp->rv3d, &viewplane, rp->engine, &clipsta, &clipend, &pixsize, &orth))
898                 return;
899         
900         rp->stop = stop;
901         rp->do_update = do_update;
902
903         // printf("Enter previewrender\n");
904         
905         /* ok, are we rendering all over? */
906         sprintf(name, "View3dPreview %p", (void *)rp->ar);
907         re = rp->engine->re = RE_GetRender(name);
908         
909         /* set this always, rp is different for each job */
910         RE_test_break_cb(re, rp, render_view3d_break);
911         RE_display_draw_cb(re, rp, render_view3d_draw_update);
912         RE_stats_draw_cb(re, rp, render_view3d_renderinfo_cb);
913         
914         rstats = RE_GetStats(re);
915
916         if ((update_flag & (PR_UPDATE_RENDERSIZE | PR_UPDATE_DATABASE)) || rstats->convertdone == 0) {
917                 /* no osa, blur, seq, layers, etc for preview render */
918                 rdata = rp->scene->r;
919                 rdata.mode &= ~(R_OSA | R_MBLUR | R_BORDER | R_PANORAMA);
920                 rdata.scemode &= ~(R_DOSEQ | R_DOCOMP | R_FREE_IMAGE);
921                 rdata.scemode |= R_VIEWPORT_PREVIEW;
922                 
923                 /* we do use layers, but only active */
924                 rdata.scemode |= R_SINGLE_LAYER;
925
926                 /* initalize always */
927                 if (render_view3d_disprect(rp->scene, rp->ar, rp->v3d, rp->rv3d, &cliprct)) {
928                         rdata.mode |= R_BORDER;
929                         RE_InitState(re, NULL, &rdata, NULL, rp->ar->winx, rp->ar->winy, &cliprct);
930                 }
931                 else
932                         RE_InitState(re, NULL, &rdata, NULL, rp->ar->winx, rp->ar->winy, NULL);
933         }
934
935         if (orth)
936                 RE_SetOrtho(re, &viewplane, clipsta, clipend);
937         else
938                 RE_SetWindow(re, &viewplane, clipsta, clipend);
939
940         RE_SetPixelSize(re, pixsize);
941         
942         if ((update_flag & PR_UPDATE_DATABASE) || rstats->convertdone == 0) {
943                 unsigned int lay = rp->scene->lay;
944
945                 /* allow localview render for objects with lights in normal layers */
946                 if (rp->v3d->lay & 0xFF000000)
947                         lay |= rp->v3d->lay;
948                 else lay = rp->v3d->lay;
949                 
950                 RE_SetView(re, rp->viewmat);
951
952                 /* copying blender data while main thread is locked, to avoid crashes */
953                 WM_job_main_thread_lock_acquire(rp->job);
954                 RE_Database_Free(re);
955                 RE_Database_FromScene(re, rp->bmain, rp->scene, lay, 0);                // 0= dont use camera view
956                 WM_job_main_thread_lock_release(rp->job);
957
958                 /* do preprocessing like building raytree, shadows, volumes, SSS */
959                 RE_Database_Preprocess(re);
960
961                 /* conversion not completed, need to do it again */
962                 if (!rstats->convertdone) {
963                         if (render_view3d_is_valid(rp)) {
964                                 rp->engine->job_update_flag |= PR_UPDATE_DATABASE;
965                         }
966                 }
967
968                 // printf("dbase update\n");
969         }
970         else {
971                 // printf("dbase rotate\n");
972                 RE_DataBase_IncrementalView(re, rp->viewmat, 0);
973                 restore = 1;
974         }
975
976         RE_DataBase_ApplyWindow(re);
977
978         /* OK, can we enter render code? */
979         if (rstats->convertdone) {
980                 RE_TileProcessor(re);
981                 
982                 /* always rotate back */
983                 if (restore)
984                         RE_DataBase_IncrementalView(re, rp->viewmat, 1);
985         }
986 }
987
988 static void render_view3d_free(void *customdata)
989 {
990         RenderPreview *rp = customdata;
991         
992         MEM_freeN(rp);
993 }
994
995 static bool render_view3d_flag_changed(RenderEngine *engine, const bContext *C)
996 {
997         RegionView3D *rv3d = CTX_wm_region_view3d(C);
998         View3D *v3d = CTX_wm_view3d(C);
999         ARegion *ar = CTX_wm_region(C);
1000         Scene *scene = CTX_data_scene(C);
1001         Render *re;
1002         rctf viewplane;
1003         rcti disprect;
1004         float clipsta, clipend;
1005         bool orth;
1006         int job_update_flag = 0;
1007         char name[32];
1008         
1009         /* ensure render engine exists */
1010         re = engine->re;
1011
1012         if (!re) {
1013                 sprintf(name, "View3dPreview %p", (void *)ar);
1014                 re = engine->re = RE_GetRender(name);
1015                 if (!re)
1016                         re = engine->re = RE_NewRender(name);
1017
1018                 engine->update_flag |= RE_ENGINE_UPDATE_DATABASE;
1019         }
1020
1021         /* check update_flag */
1022         if (engine->update_flag & RE_ENGINE_UPDATE_MA)
1023                 job_update_flag |= PR_UPDATE_MATERIAL;
1024         
1025         if (engine->update_flag & RE_ENGINE_UPDATE_OTHER)
1026                 job_update_flag |= PR_UPDATE_MATERIAL;
1027         
1028         if (engine->update_flag & RE_ENGINE_UPDATE_DATABASE) {
1029                 job_update_flag |= PR_UPDATE_DATABASE;
1030
1031                 /* load editmesh */
1032                 if (scene->obedit)
1033                         ED_object_editmode_load(scene->obedit);
1034         }
1035         
1036         engine->update_flag = 0;
1037         
1038         /* check if viewport changed */
1039         if (engine->last_winx != ar->winx || engine->last_winy != ar->winy) {
1040                 engine->last_winx = ar->winx;
1041                 engine->last_winy = ar->winy;
1042                 job_update_flag |= PR_UPDATE_RENDERSIZE;
1043         }
1044
1045         if (compare_m4m4(engine->last_viewmat, rv3d->viewmat, 0.00001f) == 0) {
1046                 copy_m4_m4(engine->last_viewmat, rv3d->viewmat);
1047                 job_update_flag |= PR_UPDATE_VIEW;
1048         }
1049         
1050         render_view3d_get_rects(ar, v3d, rv3d, &viewplane, engine, &clipsta, &clipend, NULL, &orth);
1051         
1052         if (BLI_rctf_compare(&viewplane, &engine->last_viewplane, 0.00001f) == 0) {
1053                 engine->last_viewplane = viewplane;
1054                 job_update_flag |= PR_UPDATE_VIEW;
1055         }
1056         
1057         render_view3d_disprect(scene, ar, v3d, rv3d, &disprect);
1058         if (BLI_rcti_compare(&disprect, &engine->last_disprect) == 0) {
1059                 engine->last_disprect = disprect;
1060                 job_update_flag |= PR_UPDATE_RENDERSIZE;
1061         }
1062
1063         /* any changes? go ahead and rerender */
1064         if (job_update_flag) {
1065                 engine->job_update_flag |= job_update_flag;
1066                 return true;
1067         }
1068
1069         return false;
1070 }
1071
1072 static void render_view3d_do(RenderEngine *engine, const bContext *C)
1073 {
1074         wmJob *wm_job;
1075         RenderPreview *rp;
1076         Scene *scene = CTX_data_scene(C);
1077         
1078         if (CTX_wm_window(C) == NULL)
1079                 return;
1080         if (!render_view3d_flag_changed(engine, C))
1081                 return;
1082
1083         wm_job = WM_jobs_get(CTX_wm_manager(C), CTX_wm_window(C), CTX_wm_region(C), "Render Preview",
1084                              WM_JOB_EXCL_RENDER, WM_JOB_TYPE_RENDER_PREVIEW);
1085         rp = MEM_callocN(sizeof(RenderPreview), "render preview");
1086         rp->job = wm_job;
1087
1088         /* customdata for preview thread */
1089         rp->scene = scene;
1090         rp->engine = engine;
1091         rp->sa = CTX_wm_area(C);
1092         rp->ar = CTX_wm_region(C);
1093         rp->v3d = rp->sa->spacedata.first;
1094         rp->rv3d = CTX_wm_region_view3d(C);
1095         rp->bmain = CTX_data_main(C);
1096         copy_m4_m4(rp->viewmat, rp->rv3d->viewmat);
1097         
1098         /* clear info text */
1099         engine->text[0] = '\0';
1100         
1101         /* setup job */
1102         WM_jobs_customdata_set(wm_job, rp, render_view3d_free);
1103         WM_jobs_timer(wm_job, 0.1, NC_SPACE | ND_SPACE_VIEW3D, NC_SPACE | ND_SPACE_VIEW3D);
1104         WM_jobs_callbacks(wm_job, render_view3d_startjob, NULL, NULL, NULL);
1105         
1106         WM_jobs_start(CTX_wm_manager(C), wm_job);
1107         
1108         engine->flag &= ~RE_ENGINE_DO_UPDATE;
1109 }
1110
1111 /* callback for render engine , on changes */
1112 void render_view3d_update(RenderEngine *engine, const bContext *C)
1113 {       
1114         /* this shouldn't be needed and causes too many database rebuilds, but we
1115          * aren't actually tracking updates for all relevent datablocks so this is
1116          * a catch-all for updates */
1117         engine->update_flag |= RE_ENGINE_UPDATE_DATABASE;
1118
1119         render_view3d_do(engine, C);
1120 }
1121
1122 void render_view3d_draw(RenderEngine *engine, const bContext *C)
1123 {
1124         Render *re = engine->re;
1125         RenderResult rres;
1126         char name[32];
1127         
1128         render_view3d_do(engine, C);
1129         
1130         if (re == NULL) {
1131                 sprintf(name, "View3dPreview %p", (void *)CTX_wm_region(C));
1132                 re = RE_GetRender(name);
1133         
1134                 if (re == NULL) return;
1135         }
1136         
1137         RE_AcquireResultImage(re, &rres);
1138         
1139         if (rres.rectf) {
1140                 Scene *scene = CTX_data_scene(C);
1141                 bool force_fallback = false;
1142                 bool need_fallback = true;
1143                 float dither = scene->r.dither_intensity;
1144
1145                 /* Dithering is not supported on GLSL yet */
1146                 force_fallback |= dither != 0.0f;
1147
1148                 /* If user decided not to use GLSL, fallback to glaDrawPixelsAuto */
1149                 force_fallback |= (U.image_draw_method != IMAGE_DRAW_METHOD_GLSL);
1150
1151                 /* Try using GLSL display transform. */
1152                 if (force_fallback == false) {
1153                         if (IMB_colormanagement_setup_glsl_draw(&scene->view_settings, &scene->display_settings, 0.0f, true)) {
1154                                 glEnable(GL_BLEND);
1155                                 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
1156                                 glaDrawPixelsTex(rres.xof, rres.yof, rres.rectx, rres.recty, GL_RGBA, GL_FLOAT,
1157                                                  GL_NEAREST, rres.rectf);
1158                                 glDisable(GL_BLEND);
1159
1160                                 IMB_colormanagement_finish_glsl_draw();
1161                                 need_fallback = false;
1162                         }
1163                 }
1164
1165                 /* If GLSL failed, use old-school CPU-based transform. */
1166                 if (need_fallback) {
1167                         unsigned char *display_buffer = MEM_mallocN(4 * rres.rectx * rres.recty * sizeof(char),
1168                                                                     "render_view3d_draw");
1169
1170                         IMB_colormanagement_buffer_make_display_space(rres.rectf, display_buffer, rres.rectx, rres.recty,
1171                                                                       4, dither, &scene->view_settings, &scene->display_settings);
1172
1173                         glEnable(GL_BLEND);
1174                         glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
1175                         glaDrawPixelsAuto(rres.xof, rres.yof, rres.rectx, rres.recty, GL_RGBA, GL_UNSIGNED_BYTE,
1176                                           GL_NEAREST, display_buffer);
1177                         glDisable(GL_BLEND);
1178
1179                         MEM_freeN(display_buffer);
1180                 }
1181         }
1182
1183         RE_ReleaseResultImage(re);
1184 }
1185
1186 void ED_viewport_render_kill_jobs(const bContext *C, bool free_database)
1187 {
1188         wmWindowManager *wm = CTX_wm_manager(C);
1189         Main *bmain = CTX_data_main(C);
1190         bScreen *sc;
1191         ScrArea *sa;
1192         ARegion *ar;
1193
1194         if (!wm)
1195                 return;
1196
1197         /* kill all actively running jobs */
1198         WM_jobs_kill(wm, NULL, render_view3d_startjob);
1199
1200         /* loop over 3D view render engines */
1201         for (sc = bmain->screen.first; sc; sc = sc->id.next) {
1202                 for (sa = sc->areabase.first; sa; sa = sa->next) {
1203                         if (sa->spacetype != SPACE_VIEW3D)
1204                                 continue;
1205                         
1206                         for (ar = sa->regionbase.first; ar; ar = ar->next) {
1207                                 RegionView3D *rv3d;
1208                                 
1209                                 if (ar->regiontype != RGN_TYPE_WINDOW)
1210                                         continue;
1211                                 
1212                                 rv3d = ar->regiondata;
1213
1214                                 if (rv3d->render_engine) {
1215                                         /* free render database now before we change data, because
1216                                          * RE_Database_Free will also loop over blender data */
1217                                         if (free_database) {
1218                                                 char name[32];
1219                                                 Render *re;
1220
1221                                                 sprintf(name, "View3dPreview %p", (void *)ar);
1222                                                 re = RE_GetRender(name);
1223
1224                                                 if (re)
1225                                                         RE_Database_Free(re);
1226
1227                                                 /* tag render engine to update entire database */
1228                                                 rv3d->render_engine->update_flag |= RE_ENGINE_UPDATE_DATABASE;
1229                                         }
1230                                         else {
1231                                                 /* quick shader update */
1232                                                 rv3d->render_engine->update_flag |= RE_ENGINE_UPDATE_MA;
1233                                         }
1234                                 }
1235                         }
1236                 }
1237         }
1238 }
1239
1240 Scene *ED_render_job_get_scene(const bContext *C)
1241 {
1242         wmWindowManager *wm = CTX_wm_manager(C);
1243         RenderJob *rj = (RenderJob *)WM_jobs_customdata_from_type(wm, WM_JOB_TYPE_RENDER);
1244         
1245         if (rj)
1246                 return rj->scene;
1247         
1248         return NULL;
1249 }