Merge branch 'blender2.7'
[blender.git] / source / blender / editors / render / render_opengl.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2008 Blender Foundation.
17  * All rights reserved.
18  */
19
20 /** \file
21  * \ingroup edrend
22  */
23
24
25 #include <math.h>
26 #include <string.h>
27 #include <stddef.h>
28
29 #include "MEM_guardedalloc.h"
30
31 #include "DNA_camera_types.h"
32 #include "BLI_math.h"
33 #include "BLI_math_color_blend.h"
34 #include "BLI_blenlib.h"
35 #include "BLI_utildefines.h"
36 #include "BLI_threads.h"
37 #include "BLI_task.h"
38
39 #include "DNA_scene_types.h"
40 #include "DNA_object_types.h"
41 #include "DNA_gpencil_types.h"
42
43 #include "BKE_camera.h"
44 #include "BKE_context.h"
45 #include "BKE_global.h"
46 #include "BKE_image.h"
47 #include "BKE_main.h"
48 #include "BKE_report.h"
49 #include "BKE_scene.h"
50 #include "BKE_sequencer.h"
51 #include "BKE_writeavi.h"
52
53 #include "DEG_depsgraph.h"
54
55 #include "DRW_engine.h"
56
57 #include "WM_api.h"
58 #include "WM_types.h"
59
60 #include "ED_screen.h"
61 #include "ED_view3d.h"
62 #include "ED_gpencil.h"
63
64 #include "RE_pipeline.h"
65 #include "IMB_imbuf_types.h"
66 #include "IMB_imbuf.h"
67 #include "IMB_colormanagement.h"
68
69 #include "RNA_access.h"
70 #include "RNA_define.h"
71
72 #include "GPU_framebuffer.h"
73 #include "GPU_glew.h"
74 #include "GPU_matrix.h"
75
76 #include "render_intern.h"
77
78 /* Define this to get timing information. */
79 // #undef DEBUG_TIME
80
81 #ifdef DEBUG_TIME
82 #  include "PIL_time.h"
83 #endif
84
85 // TODO(sergey): Find better approximation of the scheduled frames.
86 // For really highres renders it might fail still.
87 #define MAX_SCHEDULED_FRAMES 8
88
89 typedef struct OGLRender {
90         Main *bmain;
91         Render *re;
92         Scene *scene;
93         WorkSpace *workspace;
94         ViewLayer *view_layer;
95         Depsgraph *depsgraph;
96
97         View3D *v3d;
98         RegionView3D *rv3d;
99         ARegion *ar;
100
101         ScrArea *prevsa;
102         ARegion *prevar;
103
104         int views_len;  /* multi-view views */
105
106         bool is_sequencer;
107         SpaceSeq *sseq;
108         struct {
109                 ImBuf **ibufs_arr;
110         } seq_data;
111
112
113         Image *ima;
114         ImageUser iuser;
115
116         GPUOffScreen *ofs;
117         int ofs_samples;
118         bool ofs_full_samples;
119         int sizex, sizey;
120         int write_still;
121
122         ReportList *reports;
123         bMovieHandle *mh;
124         int cfrao, nfra;
125
126         int totvideos;
127
128         /* quick lookup */
129         int view_id;
130
131         /* wm vars for timer and progress cursor */
132         wmWindowManager *wm;
133         wmWindow *win;
134
135         wmTimer *timer; /* use to check if running modal or not (invoke'd or exec'd)*/
136         void **movie_ctx_arr;
137
138         TaskScheduler *task_scheduler;
139         TaskPool *task_pool;
140         bool pool_ok;
141         bool is_animation;
142         SpinLock reports_lock;
143         unsigned int num_scheduled_frames;
144         ThreadMutex task_mutex;
145         ThreadCondition task_condition;
146
147 #ifdef DEBUG_TIME
148         double time_start;
149 #endif
150 } OGLRender;
151
152 static bool screen_opengl_is_multiview(OGLRender *oglrender)
153 {
154         View3D *v3d = oglrender->v3d;
155         RegionView3D *rv3d = oglrender->rv3d;
156         RenderData *rd = &oglrender->scene->r;
157
158         if ((rd == NULL) || ((!oglrender->is_sequencer) && ((rv3d == NULL) || (v3d == NULL))))
159                 return false;
160
161         return (rd->scemode & R_MULTIVIEW) && ((oglrender->is_sequencer) || (rv3d->persp == RV3D_CAMOB && v3d->camera));
162 }
163
164 static void screen_opengl_views_setup(OGLRender *oglrender)
165 {
166         RenderResult *rr;
167         RenderView *rv;
168         SceneRenderView *srv;
169         bool is_multiview;
170         Object *camera;
171         View3D *v3d = oglrender->v3d;
172
173         RenderData *rd = &oglrender->scene->r;
174
175         rr = RE_AcquireResultWrite(oglrender->re);
176
177         is_multiview = screen_opengl_is_multiview(oglrender);
178
179         if (!is_multiview) {
180                 /* we only have one view when multiview is off */
181                 rv = rr->views.first;
182
183                 if (rv == NULL) {
184                         rv = MEM_callocN(sizeof(RenderView), "new opengl render view");
185                         BLI_addtail(&rr->views, rv);
186                 }
187
188                 while (rv->next) {
189                         RenderView *rv_del = rv->next;
190                         BLI_remlink(&rr->views, rv_del);
191
192                         if (rv_del->rectf)
193                                 MEM_freeN(rv_del->rectf);
194
195                         if (rv_del->rectz)
196                                 MEM_freeN(rv_del->rectz);
197
198                         if (rv_del->rect32)
199                                 MEM_freeN(rv_del->rect32);
200
201                         MEM_freeN(rv_del);
202                 }
203         }
204         else {
205                 if (!oglrender->is_sequencer)
206                         RE_SetOverrideCamera(oglrender->re, V3D_CAMERA_SCENE(oglrender->scene, v3d));
207
208                 /* remove all the views that are not needed */
209                 rv = rr->views.last;
210                 while (rv) {
211                         srv = BLI_findstring(&rd->views, rv->name, offsetof(SceneRenderView, name));
212                         if (BKE_scene_multiview_is_render_view_active(rd, srv)) {
213                                 rv = rv->prev;
214                         }
215                         else {
216                                 RenderView *rv_del = rv;
217                                 rv  = rv_del->prev;
218
219                                 BLI_remlink(&rr->views, rv_del);
220
221                                 if (rv_del->rectf)
222                                         MEM_freeN(rv_del->rectf);
223
224                                 if (rv_del->rectz)
225                                         MEM_freeN(rv_del->rectz);
226
227                                 if (rv_del->rect32)
228                                         MEM_freeN(rv_del->rect32);
229
230                                 MEM_freeN(rv_del);
231                         }
232                 }
233
234                 /* create all the views that are needed */
235                 for (srv = rd->views.first; srv; srv = srv->next) {
236                         if (BKE_scene_multiview_is_render_view_active(rd, srv) == false)
237                                 continue;
238
239                         rv = BLI_findstring(&rr->views, srv->name, offsetof(SceneRenderView, name));
240
241                         if (rv == NULL) {
242                                 rv = MEM_callocN(sizeof(RenderView), "new opengl render view");
243                                 BLI_strncpy(rv->name, srv->name, sizeof(rv->name));
244                                 BLI_addtail(&rr->views, rv);
245                         }
246                 }
247         }
248
249         if (!(is_multiview && BKE_scene_multiview_is_stereo3d(rd)))
250                 oglrender->iuser.flag &= ~IMA_SHOW_STEREO;
251
252         /* will only work for non multiview correctly */
253         if (v3d) {
254                 camera = BKE_camera_multiview_render(oglrender->scene, v3d->camera, "new opengl render view");
255                 BKE_render_result_stamp_info(oglrender->scene, camera, rr, false);
256         }
257         else {
258                 BKE_render_result_stamp_info(oglrender->scene, oglrender->scene->camera, rr, false);
259         }
260
261         RE_ReleaseResult(oglrender->re);
262 }
263
264 static void screen_opengl_render_doit(const bContext *C, OGLRender *oglrender, RenderResult *rr)
265 {
266         Depsgraph *depsgraph = CTX_data_depsgraph(C);
267         ViewLayer *view_layer = CTX_data_view_layer(C);
268         Scene *scene = oglrender->scene;
269         ARegion *ar = oglrender->ar;
270         View3D *v3d = oglrender->v3d;
271         RegionView3D *rv3d = oglrender->rv3d;
272         Object *camera = NULL;
273         int sizex = oglrender->sizex;
274         int sizey = oglrender->sizey;
275         const short view_context = (v3d != NULL);
276         bool draw_sky = (scene->r.alphamode == R_ADDSKY);
277         float *rectf = NULL;
278         const char *viewname = RE_GetActiveRenderView(oglrender->re);
279         ImBuf *ibuf_result = NULL;
280
281         if (oglrender->is_sequencer) {
282                 SpaceSeq *sseq = oglrender->sseq;
283                 struct bGPdata *gpd = (sseq && (sseq->flag & SEQ_SHOW_GPENCIL)) ? sseq->gpd : NULL;
284
285                 /* use pre-calculated ImBuf (avoids deadlock), see: */
286                 ImBuf *ibuf = oglrender->seq_data.ibufs_arr[oglrender->view_id];
287
288                 if (ibuf) {
289                         ImBuf *out = IMB_dupImBuf(ibuf);
290                         IMB_freeImBuf(ibuf);
291                         /* OpenGL render is considered to be preview and should be
292                          * as fast as possible. So currently we're making sure sequencer
293                          * result is always byte to simplify color management pipeline.
294                          *
295                          * TODO(sergey): In the case of output to float container (EXR)
296                          * it actually makes sense to keep float buffer instead.
297                          */
298                         if (out->rect_float != NULL) {
299                                 IMB_rect_from_float(out);
300                                 imb_freerectfloatImBuf(out);
301                         }
302                         BLI_assert((oglrender->sizex == ibuf->x) && (oglrender->sizey == ibuf->y));
303                         RE_render_result_rect_from_ibuf(rr, &scene->r, out, oglrender->view_id);
304                         IMB_freeImBuf(out);
305                 }
306                 else if (gpd) {
307                         /* If there are no strips, Grease Pencil still needs a buffer to draw on */
308                         ImBuf *out = IMB_allocImBuf(oglrender->sizex, oglrender->sizey, 32, IB_rect);
309                         RE_render_result_rect_from_ibuf(rr, &scene->r, out, oglrender->view_id);
310                         IMB_freeImBuf(out);
311                 }
312
313                 if (gpd) {
314                         int i;
315                         unsigned char *gp_rect;
316                         unsigned char *render_rect = (unsigned char *)RE_RenderViewGetById(rr, oglrender->view_id)->rect32;
317
318                         DRW_opengl_context_enable();
319                         GPU_offscreen_bind(oglrender->ofs, true);
320
321                         GPU_clear_color(0.0f, 0.0f, 0.0f, 0.0f);
322                         GPU_clear(GPU_COLOR_BIT | GPU_DEPTH_BIT);
323
324                         wmOrtho2(0, sizex, 0, sizey);
325                         GPU_matrix_translate_2f(sizex / 2, sizey / 2);
326
327                         G.f |= G_FLAG_RENDER_VIEWPORT;
328                         ED_gpencil_draw_ex(
329                                 view_layer, rv3d, scene, gpd, sizex, sizey, scene->r.cfra, SPACE_SEQ);
330                         G.f &= ~G_FLAG_RENDER_VIEWPORT;
331
332                         gp_rect = MEM_mallocN(sizex * sizey * sizeof(unsigned char) * 4, "offscreen rect");
333                         GPU_offscreen_read_pixels(oglrender->ofs, GL_UNSIGNED_BYTE, gp_rect);
334
335                         for (i = 0; i < sizex * sizey * 4; i += 4) {
336                                 blend_color_mix_byte(&render_rect[i], &render_rect[i], &gp_rect[i]);
337                         }
338                         GPU_offscreen_unbind(oglrender->ofs, true);
339                         DRW_opengl_context_disable();
340
341                         MEM_freeN(gp_rect);
342                 }
343         }
344         else {
345                 /* shouldnt suddenly give errors mid-render but possible */
346                 char err_out[256] = "unknown";
347                 ImBuf *ibuf_view;
348                 const int alpha_mode = (draw_sky) ? R_ADDSKY : R_ALPHAPREMUL;
349
350                 unsigned int draw_flags = V3D_OFSDRAW_NONE;
351                 draw_flags |= (oglrender->ofs_full_samples) ? V3D_OFSDRAW_USE_FULL_SAMPLE : 0;
352
353                 if (view_context) {
354                         ibuf_view = ED_view3d_draw_offscreen_imbuf(
355                                depsgraph, scene, v3d->shading.type,
356                                v3d, ar, sizex, sizey,
357                                IB_rectfloat, draw_flags, alpha_mode, oglrender->ofs_samples, viewname,
358                                oglrender->ofs, err_out);
359
360                         /* for stamp only */
361                         if (rv3d->persp == RV3D_CAMOB && v3d->camera) {
362                                 camera = BKE_camera_multiview_render(oglrender->scene, v3d->camera, viewname);
363                         }
364                 }
365                 else {
366                         draw_flags |= V3D_OFSDRAW_USE_GPENCIL;
367                         ibuf_view = ED_view3d_draw_offscreen_imbuf_simple(
368                                 depsgraph, scene, OB_SOLID,
369                                 scene->camera, oglrender->sizex, oglrender->sizey,
370                                 IB_rectfloat, draw_flags,
371                                 alpha_mode, oglrender->ofs_samples, viewname,
372                                 oglrender->ofs, err_out);
373                         camera = scene->camera;
374                 }
375
376                 if (ibuf_view) {
377                         ibuf_result = ibuf_view;
378                         rectf = (float *)ibuf_view->rect_float;
379                 }
380                 else {
381                         fprintf(stderr, "%s: failed to get buffer, %s\n", __func__, err_out);
382                 }
383         }
384
385         if (ibuf_result != NULL) {
386                 if ((scene->r.stamp & R_STAMP_ALL) && (scene->r.stamp & R_STAMP_DRAW)) {
387                         BKE_image_stamp_buf(scene, camera, NULL, NULL, rectf, rr->rectx, rr->recty, 4);
388                 }
389                 RE_render_result_rect_from_ibuf(rr, &scene->r, ibuf_result, oglrender->view_id);
390                 IMB_freeImBuf(ibuf_result);
391         }
392 }
393
394 static void screen_opengl_render_write(OGLRender *oglrender)
395 {
396         Scene *scene = oglrender->scene;
397         RenderResult *rr;
398         bool ok;
399         char name[FILE_MAX];
400
401         rr = RE_AcquireResultRead(oglrender->re);
402
403         BKE_image_path_from_imformat(
404                 name, scene->r.pic, BKE_main_blendfile_path(oglrender->bmain), scene->r.cfra,
405                 &scene->r.im_format, (scene->r.scemode & R_EXTENSION) != 0, false, NULL);
406
407         /* write images as individual images or stereo */
408         BKE_render_result_stamp_info(scene, scene->camera, rr, false);
409         ok = RE_WriteRenderViewsImage(oglrender->reports, rr, scene, false, name);
410
411         RE_ReleaseResultImage(oglrender->re);
412
413         if (ok) printf("OpenGL Render written to '%s'\n", name);
414         else printf("OpenGL Render failed to write '%s'\n", name);
415 }
416
417 static void UNUSED_FUNCTION(addAlphaOverFloat)(float dest[4], const float source[4])
418 {
419         /* d = s + (1-alpha_s)d*/
420         float mul;
421
422         mul = 1.0f - source[3];
423
424         dest[0] = (mul * dest[0]) + source[0];
425         dest[1] = (mul * dest[1]) + source[1];
426         dest[2] = (mul * dest[2]) + source[2];
427         dest[3] = (mul * dest[3]) + source[3];
428
429 }
430
431 static void screen_opengl_render_apply(const bContext *C, OGLRender *oglrender)
432 {
433         RenderResult *rr;
434         RenderView *rv;
435         int view_id;
436         ImBuf *ibuf;
437         void *lock;
438
439         if (oglrender->is_sequencer) {
440                 Scene *scene = oglrender->scene;
441
442                 SeqRenderData context;
443                 SpaceSeq *sseq = oglrender->sseq;
444                 int chanshown = sseq ? sseq->chanshown : 0;
445
446                 BKE_sequencer_new_render_data(
447                         oglrender->bmain, oglrender->depsgraph, scene,
448                         oglrender->sizex, oglrender->sizey, 100.0f, false,
449                         &context);
450
451                 for (view_id = 0; view_id < oglrender->views_len; view_id++) {
452                         context.view_id = view_id;
453                         context.gpu_offscreen = oglrender->ofs;
454                         context.gpu_full_samples = oglrender->ofs_full_samples;
455
456                         oglrender->seq_data.ibufs_arr[view_id] = BKE_sequencer_give_ibuf(&context, CFRA, chanshown);
457                 }
458         }
459
460         rr = RE_AcquireResultRead(oglrender->re);
461         for (rv = rr->views.first, view_id = 0; rv; rv = rv->next, view_id++) {
462                 BLI_assert(view_id < oglrender->views_len);
463                 RE_SetActiveRenderView(oglrender->re, rv->name);
464                 oglrender->view_id = view_id;
465                 /* render composite */
466                 screen_opengl_render_doit(C, oglrender, rr);
467         }
468
469         RE_ReleaseResult(oglrender->re);
470
471         ibuf = BKE_image_acquire_ibuf(oglrender->ima, &oglrender->iuser, &lock);
472         if (ibuf) {
473                 ibuf->userflags |= IB_DISPLAY_BUFFER_INVALID;
474         }
475         BKE_image_release_ibuf(oglrender->ima, ibuf, lock);
476
477         if (oglrender->write_still) {
478                 screen_opengl_render_write(oglrender);
479         }
480 }
481
482 static bool screen_opengl_fullsample_enabled(Scene *scene)
483 {
484         if (scene->r.scemode & R_FULL_SAMPLE) {
485                 return true;
486         }
487         else {
488                 /* XXX TODO:
489                  * Technically if the hardware supports MSAA we could keep using Blender 2.7x approach.
490                  * However anti-aliasing without full_sample is not playing well even in 2.7x.
491                  *
492                  * For example, if you enable depth of field, there is aliasing, even if the viewport is fine.
493                  * For 2.8x this is more complicated because so many things rely on shader.
494                  * So until we fix the gpu_framebuffer anti-aliasing suupport we need to force full sample.
495                  */
496                 return true;
497         }
498 }
499
500 static bool screen_opengl_render_init(bContext *C, wmOperator *op)
501 {
502         /* new render clears all callbacks */
503         wmWindowManager *wm = CTX_wm_manager(C);
504         wmWindow *win = CTX_wm_window(C);
505         WorkSpace *workspace = CTX_wm_workspace(C);
506
507         Scene *scene = CTX_data_scene(C);
508         ScrArea *prevsa = CTX_wm_area(C);
509         ARegion *prevar = CTX_wm_region(C);
510         GPUOffScreen *ofs;
511         OGLRender *oglrender;
512         int sizex, sizey;
513         const int samples = (scene->r.mode & R_OSA) ? scene->r.osa : 0;
514         const bool full_samples = (samples != 0) && screen_opengl_fullsample_enabled(scene);
515         bool is_view_context = RNA_boolean_get(op->ptr, "view_context");
516         const bool is_animation = RNA_boolean_get(op->ptr, "animation");
517         const bool is_sequencer = RNA_boolean_get(op->ptr, "sequencer");
518         const bool is_write_still = RNA_boolean_get(op->ptr, "write_still");
519         char err_out[256] = "unknown";
520
521         if (G.background) {
522                 BKE_report(op->reports, RPT_ERROR, "Cannot use OpenGL render in background mode (no opengl context)");
523                 return false;
524         }
525
526         /* only one render job at a time */
527         if (WM_jobs_test(wm, scene, WM_JOB_TYPE_RENDER))
528                 return false;
529
530         if (is_sequencer) {
531                 is_view_context = false;
532         }
533         else {
534                 /* ensure we have a 3d view */
535                 if (!ED_view3d_context_activate(C)) {
536                         RNA_boolean_set(op->ptr, "view_context", false);
537                         is_view_context = false;
538                 }
539
540                 if (!is_view_context && scene->camera == NULL) {
541                         BKE_report(op->reports, RPT_ERROR, "Scene has no camera");
542                         return false;
543                 }
544         }
545
546         if (!is_animation && is_write_still && BKE_imtype_is_movie(scene->r.im_format.imtype)) {
547                 BKE_report(op->reports, RPT_ERROR, "Cannot write a single file with an animation format selected");
548                 return false;
549         }
550
551         /* stop all running jobs, except screen one. currently previews frustrate Render */
552         WM_jobs_kill_all_except(wm, CTX_wm_screen(C));
553
554         /* create offscreen buffer */
555         sizex = (scene->r.size * scene->r.xsch) / 100;
556         sizey = (scene->r.size * scene->r.ysch) / 100;
557
558         /* corrects render size with actual size, not every card supports non-power-of-two dimensions */
559         DRW_opengl_context_enable(); /* Offscreen creation needs to be done in DRW context. */
560         ofs = GPU_offscreen_create(sizex, sizey, full_samples ? 0 : samples, true, true, err_out);
561         DRW_opengl_context_disable();
562
563         if (!ofs) {
564                 BKE_reportf(op->reports, RPT_ERROR, "Failed to create OpenGL off-screen buffer, %s", err_out);
565                 return false;
566         }
567
568         /* allocate opengl render */
569         oglrender = MEM_callocN(sizeof(OGLRender), "OGLRender");
570         op->customdata = oglrender;
571
572         oglrender->ofs = ofs;
573         oglrender->ofs_samples = samples;
574         oglrender->ofs_full_samples = full_samples;
575         oglrender->sizex = sizex;
576         oglrender->sizey = sizey;
577         oglrender->bmain = CTX_data_main(C);
578         oglrender->scene = scene;
579         oglrender->workspace = workspace;
580         oglrender->view_layer = CTX_data_view_layer(C);
581         oglrender->depsgraph = CTX_data_depsgraph(C);
582         oglrender->cfrao = scene->r.cfra;
583
584         oglrender->write_still = is_write_still && !is_animation;
585         oglrender->is_animation = is_animation;
586
587         oglrender->views_len = BKE_scene_multiview_num_views_get(&scene->r);
588
589         oglrender->is_sequencer = is_sequencer;
590         if (is_sequencer) {
591                 oglrender->sseq = CTX_wm_space_seq(C);
592                 ImBuf **ibufs_arr = MEM_callocN(sizeof(*ibufs_arr) * oglrender->views_len, __func__);
593                 oglrender->seq_data.ibufs_arr = ibufs_arr;
594         }
595
596         oglrender->prevsa = prevsa;
597         oglrender->prevar = prevar;
598
599         if (is_view_context) {
600                 /* so quad view renders camera */
601                 ED_view3d_context_user_region(C, &oglrender->v3d, &oglrender->ar);
602
603                 oglrender->rv3d = oglrender->ar->regiondata;
604
605                 /* MUST be cleared on exit */
606                 oglrender->scene->customdata_mask_modal = ED_view3d_datamask(C, oglrender->scene, oglrender->v3d);
607
608                 /* apply immediately in case we're rendering from a script,
609                  * running notifiers again will overwrite */
610                 oglrender->scene->customdata_mask |= oglrender->scene->customdata_mask_modal;
611         }
612
613         /* create render */
614         oglrender->re = RE_NewSceneRender(scene);
615
616         /* create image and image user */
617         oglrender->ima = BKE_image_verify_viewer(oglrender->bmain, IMA_TYPE_R_RESULT, "Render Result");
618         BKE_image_signal(oglrender->bmain, oglrender->ima, NULL, IMA_SIGNAL_FREE);
619         BKE_image_backup_render(oglrender->scene, oglrender->ima, true);
620
621         oglrender->iuser.scene = scene;
622         oglrender->iuser.ok = 1;
623
624         /* create render result */
625         RE_InitState(oglrender->re, NULL, &scene->r, &scene->view_layers, NULL, sizex, sizey, NULL);
626
627         /* create render views */
628         screen_opengl_views_setup(oglrender);
629
630         /* wm vars */
631         oglrender->wm = wm;
632         oglrender->win = win;
633
634         oglrender->totvideos = 0;
635         oglrender->mh = NULL;
636         oglrender->movie_ctx_arr = NULL;
637
638         if (is_animation) {
639                 TaskScheduler *task_scheduler = BLI_task_scheduler_get();
640                 if (BKE_imtype_is_movie(scene->r.im_format.imtype)) {
641                         task_scheduler = BLI_task_scheduler_create(1);
642                         oglrender->task_scheduler = task_scheduler;
643                         oglrender->task_pool = BLI_task_pool_create_background(task_scheduler,
644                                                                                oglrender);
645                 }
646                 else {
647                         oglrender->task_scheduler = NULL;
648                         oglrender->task_pool = BLI_task_pool_create(task_scheduler,
649                                                                     oglrender);
650                 }
651                 oglrender->pool_ok = true;
652                 BLI_spin_init(&oglrender->reports_lock);
653         }
654         else {
655                 oglrender->task_scheduler = NULL;
656                 oglrender->task_pool = NULL;
657         }
658         oglrender->num_scheduled_frames = 0;
659         BLI_mutex_init(&oglrender->task_mutex);
660         BLI_condition_init(&oglrender->task_condition);
661
662 #ifdef DEBUG_TIME
663         oglrender->time_start = PIL_check_seconds_timer();
664 #endif
665
666         return true;
667 }
668
669 static void screen_opengl_render_end(bContext *C, OGLRender *oglrender)
670 {
671         Main *bmain = CTX_data_main(C);
672         Scene *scene = oglrender->scene;
673         int i;
674
675         if (oglrender->is_animation) {
676                 /* Trickery part for movie output:
677                  *
678                  * We MUST write frames in an exact order, so we only let background
679                  * thread to work on that, and main thread is simply waits for that
680                  * thread to do all the dirty work.
681                  *
682                  * After this loop is done work_and_wait() will have nothing to do,
683                  * so we don't run into wrong order of frames written to the stream.
684                  */
685                 if (BKE_imtype_is_movie(scene->r.im_format.imtype)) {
686                         BLI_mutex_lock(&oglrender->task_mutex);
687                         while (oglrender->num_scheduled_frames > 0) {
688                                 BLI_condition_wait(&oglrender->task_condition,
689                                                    &oglrender->task_mutex);
690                         }
691                         BLI_mutex_unlock(&oglrender->task_mutex);
692                 }
693                 BLI_task_pool_work_and_wait(oglrender->task_pool);
694                 BLI_task_pool_free(oglrender->task_pool);
695                 /* Depending on various things we might or might not use global scheduler. */
696                 if (oglrender->task_scheduler != NULL) {
697                         BLI_task_scheduler_free(oglrender->task_scheduler);
698                 }
699                 BLI_spin_end(&oglrender->reports_lock);
700         }
701         BLI_mutex_end(&oglrender->task_mutex);
702         BLI_condition_end(&oglrender->task_condition);
703
704 #ifdef DEBUG_TIME
705         printf("Total render time: %f\n", PIL_check_seconds_timer() - oglrender->time_start);
706 #endif
707
708         if (oglrender->mh) {
709                 if (BKE_imtype_is_movie(scene->r.im_format.imtype)) {
710                         for (i = 0; i < oglrender->totvideos; i++) {
711                                 oglrender->mh->end_movie(oglrender->movie_ctx_arr[i]);
712                                 oglrender->mh->context_free(oglrender->movie_ctx_arr[i]);
713                         }
714                 }
715
716                 if (oglrender->movie_ctx_arr) {
717                         MEM_freeN(oglrender->movie_ctx_arr);
718                 }
719         }
720
721         if (oglrender->timer) { /* exec will not have a timer */
722                 Depsgraph *depsgraph = oglrender->depsgraph;
723                 scene->r.cfra = oglrender->cfrao;
724                 BKE_scene_graph_update_for_newframe(depsgraph, bmain);
725
726                 WM_event_remove_timer(oglrender->wm, oglrender->win, oglrender->timer);
727         }
728
729         WM_cursor_modal_restore(oglrender->win);
730
731         WM_event_add_notifier(C, NC_SCENE | ND_RENDER_RESULT, oglrender->scene);
732
733         DRW_opengl_context_enable();
734         GPU_offscreen_free(oglrender->ofs);
735         DRW_opengl_context_disable();
736
737         if (oglrender->is_sequencer) {
738                 MEM_freeN(oglrender->seq_data.ibufs_arr);
739         }
740
741         oglrender->scene->customdata_mask_modal = 0;
742
743         CTX_wm_area_set(C, oglrender->prevsa);
744         CTX_wm_region_set(C, oglrender->prevar);
745
746         MEM_freeN(oglrender);
747 }
748
749 static void screen_opengl_render_cancel(bContext *C, wmOperator *op)
750 {
751         screen_opengl_render_end(C, op->customdata);
752 }
753
754 /* share between invoke and exec */
755 static bool screen_opengl_render_anim_initialize(bContext *C, wmOperator *op)
756 {
757         /* initialize animation */
758         OGLRender *oglrender;
759         Scene *scene;
760
761         oglrender = op->customdata;
762         scene = oglrender->scene;
763         oglrender->totvideos = BKE_scene_multiview_num_videos_get(&scene->r);
764
765         oglrender->reports = op->reports;
766
767         if (BKE_imtype_is_movie(scene->r.im_format.imtype)) {
768                 size_t width, height;
769                 int i;
770
771                 BKE_scene_multiview_videos_dimensions_get(&scene->r, oglrender->sizex, oglrender->sizey, &width, &height);
772                 oglrender->mh = BKE_movie_handle_get(scene->r.im_format.imtype);
773
774                 if (oglrender->mh == NULL) {
775                         BKE_report(oglrender->reports, RPT_ERROR, "Movie format unsupported");
776                         screen_opengl_render_end(C, oglrender);
777                         return false;
778                 }
779
780                 oglrender->movie_ctx_arr = MEM_mallocN(sizeof(void *) * oglrender->totvideos, "Movies");
781
782                 for (i = 0; i < oglrender->totvideos; i++) {
783                         const char *suffix = BKE_scene_multiview_view_id_suffix_get(&scene->r, i);
784
785                         oglrender->movie_ctx_arr[i] = oglrender->mh->context_create();
786                         if (!oglrender->mh->start_movie(oglrender->movie_ctx_arr[i], scene, &scene->r, oglrender->sizex,
787                                                         oglrender->sizey, oglrender->reports, PRVRANGEON != 0, suffix))
788                         {
789                                 screen_opengl_render_end(C, oglrender);
790                                 return false;
791                         }
792                 }
793         }
794
795         oglrender->cfrao = scene->r.cfra;
796         oglrender->nfra = PSFRA;
797         scene->r.cfra = PSFRA;
798
799         return true;
800 }
801
802 typedef struct WriteTaskData {
803         RenderResult *rr;
804         Scene tmp_scene;
805 } WriteTaskData;
806
807 static void write_result_func(TaskPool * __restrict pool,
808                               void *task_data_v,
809                               int UNUSED(thread_id))
810 {
811         OGLRender *oglrender = (OGLRender *) BLI_task_pool_userdata(pool);
812         WriteTaskData *task_data = (WriteTaskData *) task_data_v;
813         Scene *scene = &task_data->tmp_scene;
814         RenderResult *rr = task_data->rr;
815         const bool is_movie = BKE_imtype_is_movie(scene->r.im_format.imtype);
816         const int cfra = scene->r.cfra;
817         bool ok;
818         /* Don't attempt to write if we've got an error. */
819         if (!oglrender->pool_ok) {
820                 RE_FreeRenderResult(rr);
821                 BLI_mutex_lock(&oglrender->task_mutex);
822                 oglrender->num_scheduled_frames--;
823                 BLI_condition_notify_all(&oglrender->task_condition);
824                 BLI_mutex_unlock(&oglrender->task_mutex);
825                 return;
826         }
827         /* Construct local thread0safe copy of reports structure which we can
828          * safely pass to the underlying functions.
829          */
830         ReportList reports;
831         BKE_reports_init(&reports, oglrender->reports->flag & ~RPT_PRINT);
832         /* Do actual save logic here, depending on the file format.
833          *
834          * NOTE: We have to construct temporary scene with proper scene->r.cfra.
835          * This is because underlying calls do not use r.cfra but use scene
836          * for that.
837          */
838         if (is_movie) {
839                 ok = RE_WriteRenderViewsMovie(&reports,
840                                               rr,
841                                               scene,
842                                               &scene->r,
843                                               oglrender->mh,
844                                               oglrender->movie_ctx_arr,
845                                               oglrender->totvideos,
846                                               PRVRANGEON != 0);
847         }
848         else {
849                 /* TODO(sergey): We can in theory save some CPU ticks here because we
850                  * calculate file name again here.
851                  */
852                 char name[FILE_MAX];
853                 BKE_image_path_from_imformat(name,
854                                              scene->r.pic,
855                                              BKE_main_blendfile_path(oglrender->bmain),
856                                              cfra,
857                                              &scene->r.im_format,
858                                              (scene->r.scemode & R_EXTENSION) != 0,
859                                              true,
860                                              NULL);
861
862                 BKE_render_result_stamp_info(scene, scene->camera, rr, false);
863                 ok = RE_WriteRenderViewsImage(NULL, rr, scene, true, name);
864                 if (!ok) {
865                         BKE_reportf(&reports,
866                                     RPT_ERROR,
867                                     "Write error: cannot save %s",
868                                     name);
869                 }
870         }
871         if (reports.list.first != NULL) {
872                 BLI_spin_lock(&oglrender->reports_lock);
873                 for (Report *report = reports.list.first;
874                      report != NULL;
875                      report = report->next)
876                 {
877                         BKE_report(oglrender->reports,
878                                    report->type,
879                                    report->message);
880                 }
881                 BLI_spin_unlock(&oglrender->reports_lock);
882         }
883         if (!ok) {
884                 oglrender->pool_ok = false;
885         }
886         RE_FreeRenderResult(rr);
887         BLI_mutex_lock(&oglrender->task_mutex);
888         oglrender->num_scheduled_frames--;
889         BLI_condition_notify_all(&oglrender->task_condition);
890         BLI_mutex_unlock(&oglrender->task_mutex);
891 }
892
893 static bool schedule_write_result(OGLRender *oglrender, RenderResult *rr)
894 {
895         if (!oglrender->pool_ok) {
896                 RE_FreeRenderResult(rr);
897                 return false;
898         }
899         Scene *scene = oglrender->scene;
900         WriteTaskData *task_data = MEM_mallocN(sizeof(WriteTaskData), "write task data");
901         task_data->rr = rr;
902         task_data->tmp_scene = *scene;
903         BLI_mutex_lock(&oglrender->task_mutex);
904         oglrender->num_scheduled_frames++;
905         if (oglrender->num_scheduled_frames > MAX_SCHEDULED_FRAMES) {
906                 BLI_condition_wait(&oglrender->task_condition,
907                                    &oglrender->task_mutex);
908         }
909         BLI_mutex_unlock(&oglrender->task_mutex);
910         BLI_task_pool_push(oglrender->task_pool,
911                            write_result_func,
912                            task_data,
913                            true,
914                            TASK_PRIORITY_LOW);
915         return true;
916 }
917
918 static bool screen_opengl_render_anim_step(bContext *C, wmOperator *op)
919 {
920         Main *bmain = CTX_data_main(C);
921         OGLRender *oglrender = op->customdata;
922         Scene *scene = oglrender->scene;
923         Depsgraph *depsgraph = oglrender->depsgraph;
924         char name[FILE_MAX];
925         bool ok = false;
926         const bool view_context = (oglrender->v3d != NULL);
927         bool is_movie;
928         RenderResult *rr;
929
930         /* go to next frame */
931         if (CFRA < oglrender->nfra)
932                 CFRA++;
933         while (CFRA < oglrender->nfra) {
934                 BKE_scene_graph_update_for_newframe(depsgraph, bmain);
935                 CFRA++;
936         }
937
938         is_movie = BKE_imtype_is_movie(scene->r.im_format.imtype);
939
940         if (!is_movie) {
941                 BKE_image_path_from_imformat(
942                         name, scene->r.pic, BKE_main_blendfile_path(oglrender->bmain), scene->r.cfra,
943                         &scene->r.im_format, (scene->r.scemode & R_EXTENSION) != 0, true, NULL);
944
945                 if ((scene->r.mode & R_NO_OVERWRITE) && BLI_exists(name)) {
946                         BLI_spin_lock(&oglrender->reports_lock);
947                         BKE_reportf(op->reports, RPT_INFO, "Skipping existing frame \"%s\"", name);
948                         BLI_spin_unlock(&oglrender->reports_lock);
949                         ok = true;
950                         goto finally;
951                 }
952         }
953
954         WM_cursor_time(oglrender->win, scene->r.cfra);
955
956         BKE_scene_graph_update_for_newframe(depsgraph, bmain);
957
958         if (view_context) {
959                 if (oglrender->rv3d->persp == RV3D_CAMOB && oglrender->v3d->camera && oglrender->v3d->scenelock) {
960                         /* since BKE_scene_graph_update_for_newframe() is used rather
961                          * then ED_update_for_newframe() the camera needs to be set */
962                         if (BKE_scene_camera_switch_update(scene)) {
963                                 oglrender->v3d->camera = scene->camera;
964                         }
965                 }
966         }
967         else {
968                 BKE_scene_camera_switch_update(scene);
969         }
970
971         /* render into offscreen buffer */
972         screen_opengl_render_apply(C, oglrender);
973
974         /* save to disk */
975         rr = RE_AcquireResultRead(oglrender->re);
976         RenderResult *new_rr = RE_DuplicateRenderResult(rr);
977         RE_ReleaseResult(oglrender->re);
978
979         ok = schedule_write_result(oglrender, new_rr);
980
981 finally:  /* Step the frame and bail early if needed */
982
983         /* go to next frame */
984         oglrender->nfra += scene->r.frame_step;
985
986         /* stop at the end or on error */
987         if (CFRA >= PEFRA || !ok) {
988                 screen_opengl_render_end(C, op->customdata);
989                 return 0;
990         }
991
992         return 1;
993 }
994
995
996 static int screen_opengl_render_modal(bContext *C, wmOperator *op, const wmEvent *event)
997 {
998         OGLRender *oglrender = op->customdata;
999         const bool anim = RNA_boolean_get(op->ptr, "animation");
1000         bool ret;
1001
1002         switch (event->type) {
1003                 case ESCKEY:
1004                         /* cancel */
1005                         oglrender->pool_ok = false;  /* Flag pool for cancel. */
1006                         screen_opengl_render_end(C, op->customdata);
1007                         return OPERATOR_FINISHED;
1008                 case TIMER:
1009                         /* render frame? */
1010                         if (oglrender->timer == event->customdata)
1011                                 break;
1012                         ATTR_FALLTHROUGH;
1013                 default:
1014                         /* nothing to do */
1015                         return OPERATOR_RUNNING_MODAL;
1016         }
1017
1018         /* run first because screen_opengl_render_anim_step can free oglrender */
1019         WM_event_add_notifier(C, NC_SCENE | ND_RENDER_RESULT, oglrender->scene);
1020
1021         if (anim == 0) {
1022                 screen_opengl_render_apply(C, op->customdata);
1023                 screen_opengl_render_end(C, op->customdata);
1024                 return OPERATOR_FINISHED;
1025         }
1026         else {
1027                 ret = screen_opengl_render_anim_step(C, op);
1028         }
1029
1030         /* stop at the end or on error */
1031         if (ret == false) {
1032                 return OPERATOR_FINISHED;
1033         }
1034
1035         return OPERATOR_RUNNING_MODAL;
1036 }
1037
1038 static int screen_opengl_render_invoke(bContext *C, wmOperator *op, const wmEvent *event)
1039 {
1040         OGLRender *oglrender;
1041         const bool anim = RNA_boolean_get(op->ptr, "animation");
1042
1043         if (!screen_opengl_render_init(C, op))
1044                 return OPERATOR_CANCELLED;
1045
1046         if (anim) {
1047                 if (!screen_opengl_render_anim_initialize(C, op))
1048                         return OPERATOR_CANCELLED;
1049         }
1050
1051         oglrender = op->customdata;
1052         render_view_open(C, event->x, event->y, op->reports);
1053
1054         /* view may be changed above (R_OUTPUT_WINDOW) */
1055         oglrender->win = CTX_wm_window(C);
1056
1057         WM_event_add_modal_handler(C, op);
1058         oglrender->timer = WM_event_add_timer(oglrender->wm, oglrender->win, TIMER, 0.01f);
1059
1060         return OPERATOR_RUNNING_MODAL;
1061 }
1062
1063 /* executes blocking render */
1064 static int screen_opengl_render_exec(bContext *C, wmOperator *op)
1065 {
1066         const bool is_animation = RNA_boolean_get(op->ptr, "animation");
1067
1068         if (!screen_opengl_render_init(C, op))
1069                 return OPERATOR_CANCELLED;
1070
1071         if (!is_animation) { /* same as invoke */
1072                 /* render image */
1073                 screen_opengl_render_apply(C, op->customdata);
1074                 screen_opengl_render_end(C, op->customdata);
1075
1076                 return OPERATOR_FINISHED;
1077         }
1078         else {
1079                 bool ret = true;
1080
1081                 if (!screen_opengl_render_anim_initialize(C, op))
1082                         return OPERATOR_CANCELLED;
1083
1084                 while (ret) {
1085                         ret = screen_opengl_render_anim_step(C, op);
1086                 }
1087         }
1088
1089         /* no redraw needed, we leave state as we entered it */
1090 //      ED_update_for_newframe(C);
1091         WM_event_add_notifier(C, NC_SCENE | ND_RENDER_RESULT, CTX_data_scene(C));
1092
1093         return OPERATOR_FINISHED;
1094 }
1095
1096 void RENDER_OT_opengl(wmOperatorType *ot)
1097 {
1098         PropertyRNA *prop;
1099
1100         /* identifiers */
1101         ot->name = "Viewport Render";
1102         ot->description = "Take a snapshot of the active viewport";
1103         ot->idname = "RENDER_OT_opengl";
1104
1105         /* api callbacks */
1106         ot->invoke = screen_opengl_render_invoke;
1107         ot->exec = screen_opengl_render_exec; /* blocking */
1108         ot->modal = screen_opengl_render_modal;
1109         ot->cancel = screen_opengl_render_cancel;
1110
1111         ot->poll = ED_operator_screenactive;
1112
1113         prop = RNA_def_boolean(ot->srna, "animation", 0, "Animation", "Render files from the animation range of this scene");
1114         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
1115         prop = RNA_def_boolean(ot->srna, "sequencer", 0, "Sequencer", "Render using the sequencer's OpenGL display");
1116         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
1117         prop = RNA_def_boolean(ot->srna, "write_still", 0, "Write Image", "Save rendered the image to the output path (used only when animation is disabled)");
1118         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
1119         prop = RNA_def_boolean(ot->srna, "view_context", 1, "View Context", "Use the current 3D view for rendering, else use scene settings");
1120         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
1121
1122 }
1123
1124 /* function for getting an opengl buffer from a View3D, used by sequencer */
1125 // extern void *sequencer_view3d_cb;