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