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