Cleanup: remove redundant, invalid info from headers
[blender.git] / source / blender / editors / render / render_opengl.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2008 Blender Foundation.
17  * All rights reserved.
18  */
19
20 /** \file blender/editors/render/render_opengl.c
21  *  \ingroup edrend
22  */
23
24
25 #include <math.h>
26 #include <string.h>
27 #include <stddef.h>
28
29 #include "MEM_guardedalloc.h"
30
31 #include "DNA_camera_types.h"
32 #include "BLI_math.h"
33 #include "BLI_math_color_blend.h"
34 #include "BLI_blenlib.h"
35 #include "BLI_utildefines.h"
36 #include "BLI_threads.h"
37 #include "BLI_task.h"
38
39 #include "DNA_scene_types.h"
40 #include "DNA_object_types.h"
41 #include "DNA_gpencil_types.h"
42
43 #include "BKE_camera.h"
44 #include "BKE_context.h"
45 #include "BKE_global.h"
46 #include "BKE_image.h"
47 #include "BKE_main.h"
48 #include "BKE_report.h"
49 #include "BKE_scene.h"
50 #include "BKE_sequencer.h"
51 #include "BKE_writeavi.h"
52
53 #include "WM_api.h"
54 #include "WM_types.h"
55
56 #include "ED_screen.h"
57 #include "ED_view3d.h"
58 #include "ED_gpencil.h"
59
60 #include "RE_pipeline.h"
61 #include "IMB_imbuf_types.h"
62 #include "IMB_imbuf.h"
63 #include "IMB_colormanagement.h"
64
65 #include "RNA_access.h"
66 #include "RNA_define.h"
67
68 #include "GPU_glew.h"
69 #include "GPU_compositing.h"
70 #include "GPU_framebuffer.h"
71
72 #include "render_intern.h"
73
74 /* Define this to get timing information. */
75 // #undef DEBUG_TIME
76
77 #ifdef DEBUG_TIME
78 #  include "PIL_time.h"
79 #endif
80
81 // TODO(sergey): Find better approximation of the scheduled frames.
82 // For really highres renders it might fail still.
83 #define MAX_SCHEDULED_FRAMES 8
84
85 typedef struct OGLRender {
86         Main *bmain;
87         Render *re;
88         Scene *scene;
89
90         View3D *v3d;
91         RegionView3D *rv3d;
92         ARegion *ar;
93
94         ScrArea *prevsa;
95         ARegion *prevar;
96
97         int views_len;  /* multi-view views */
98
99         bool is_sequencer;
100         SpaceSeq *sseq;
101         struct {
102                 ImBuf **ibufs_arr;
103         } seq_data;
104
105
106         Image *ima;
107         ImageUser iuser;
108
109         GPUOffScreen *ofs;
110         int ofs_samples;
111         bool ofs_full_samples;
112         GPUFX *fx;
113         int sizex, sizey;
114         int write_still;
115
116         ReportList *reports;
117         bMovieHandle *mh;
118         int cfrao, nfra;
119
120         int totvideos;
121
122         /* quick lookup */
123         int view_id;
124
125         /* wm vars for timer and progress cursor */
126         wmWindowManager *wm;
127         wmWindow *win;
128
129         wmTimer *timer; /* use to check if running modal or not (invoke'd or exec'd)*/
130         void **movie_ctx_arr;
131
132         TaskScheduler *task_scheduler;
133         TaskPool *task_pool;
134         bool pool_ok;
135         bool is_animation;
136         SpinLock reports_lock;
137         unsigned int num_scheduled_frames;
138         ThreadMutex task_mutex;
139         ThreadCondition task_condition;
140
141 #ifdef DEBUG_TIME
142         double time_start;
143 #endif
144 } OGLRender;
145
146 /* added because v3d is not always valid */
147 static unsigned int screen_opengl_layers(OGLRender *oglrender)
148 {
149         if (oglrender->v3d) {
150                 return oglrender->scene->lay | oglrender->v3d->lay;
151         }
152         else {
153                 return oglrender->scene->lay;
154         }
155 }
156
157 static bool screen_opengl_is_multiview(OGLRender *oglrender)
158 {
159         View3D *v3d = oglrender->v3d;
160         RegionView3D *rv3d = oglrender->rv3d;
161         RenderData *rd = &oglrender->scene->r;
162
163         if ((rd == NULL) || ((!oglrender->is_sequencer) && ((rv3d == NULL) || (v3d == NULL))))
164                 return false;
165
166         return (rd->scemode & R_MULTIVIEW) && ((oglrender->is_sequencer) || (rv3d->persp == RV3D_CAMOB && v3d->camera));
167 }
168
169 static void screen_opengl_views_setup(OGLRender *oglrender)
170 {
171         RenderResult *rr;
172         RenderView *rv;
173         SceneRenderView *srv;
174         bool is_multiview;
175         Object *camera;
176         View3D *v3d = oglrender->v3d;
177
178         RenderData *rd = &oglrender->scene->r;
179
180         rr = RE_AcquireResultWrite(oglrender->re);
181
182         is_multiview = screen_opengl_is_multiview(oglrender);
183
184         if (!is_multiview) {
185                 /* we only have one view when multiview is off */
186                 rv = rr->views.first;
187
188                 if (rv == NULL) {
189                         rv = MEM_callocN(sizeof(RenderView), "new opengl render view");
190                         BLI_addtail(&rr->views, rv);
191                 }
192
193                 while (rv->next) {
194                         RenderView *rv_del = rv->next;
195                         BLI_remlink(&rr->views, rv_del);
196
197                         if (rv_del->rectf)
198                                 MEM_freeN(rv_del->rectf);
199
200                         if (rv_del->rectz)
201                                 MEM_freeN(rv_del->rectz);
202
203                         if (rv_del->rect32)
204                                 MEM_freeN(rv_del->rect32);
205
206                         MEM_freeN(rv_del);
207                 }
208         }
209         else {
210                 if (!oglrender->is_sequencer)
211                         RE_SetOverrideCamera(oglrender->re, V3D_CAMERA_SCENE(oglrender->scene, v3d));
212
213                 /* remove all the views that are not needed */
214                 rv = rr->views.last;
215                 while (rv) {
216                         srv = BLI_findstring(&rd->views, rv->name, offsetof(SceneRenderView, name));
217                         if (BKE_scene_multiview_is_render_view_active(rd, srv)) {
218                                 rv = rv->prev;
219                         }
220                         else {
221                                 RenderView *rv_del = rv;
222                                 rv  = rv_del->prev;
223
224                                 BLI_remlink(&rr->views, rv_del);
225
226                                 if (rv_del->rectf)
227                                         MEM_freeN(rv_del->rectf);
228
229                                 if (rv_del->rectz)
230                                         MEM_freeN(rv_del->rectz);
231
232                                 if (rv_del->rect32)
233                                         MEM_freeN(rv_del->rect32);
234
235                                 MEM_freeN(rv_del);
236                         }
237                 }
238
239                 /* create all the views that are needed */
240                 for (srv = rd->views.first; srv; srv = srv->next) {
241                         if (BKE_scene_multiview_is_render_view_active(rd, srv) == false)
242                                 continue;
243
244                         rv = BLI_findstring(&rr->views, srv->name, offsetof(SceneRenderView, name));
245
246                         if (rv == NULL) {
247                                 rv = MEM_callocN(sizeof(RenderView), "new opengl render view");
248                                 BLI_strncpy(rv->name, srv->name, sizeof(rv->name));
249                                 BLI_addtail(&rr->views, rv);
250                         }
251                 }
252         }
253
254         if (!(is_multiview && BKE_scene_multiview_is_stereo3d(rd)))
255                 oglrender->iuser.flag &= ~IMA_SHOW_STEREO;
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                 else if (gpd) {
311                         /* If there are no strips, Grease Pencil still needs a buffer to draw on */
312                         ImBuf *out = IMB_allocImBuf(oglrender->sizex, oglrender->sizey, 32, IB_rect);
313                         RE_render_result_rect_from_ibuf(rr, &scene->r, out, oglrender->view_id);
314                         IMB_freeImBuf(out);
315                 }
316
317                 if (gpd) {
318                         int i;
319                         unsigned char *gp_rect;
320                         unsigned char *render_rect = (unsigned char *)RE_RenderViewGetById(rr, oglrender->view_id)->rect32;
321
322                         GPU_offscreen_bind(oglrender->ofs, true);
323
324                         glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
325                         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
326
327                         wmOrtho2(0, sizex, 0, sizey);
328                         glTranslatef(sizex / 2, sizey / 2, 0.0f);
329
330                         G.f |= G_RENDER_OGL;
331                         ED_gpencil_draw_ex(scene, gpd, sizex, sizey, scene->r.cfra, SPACE_SEQ);
332                         G.f &= ~G_RENDER_OGL;
333
334                         gp_rect = MEM_mallocN(sizex * sizey * sizeof(unsigned char) * 4, "offscreen rect");
335                         GPU_offscreen_read_pixels(oglrender->ofs, GL_UNSIGNED_BYTE, gp_rect);
336
337                         for (i = 0; i < sizex * sizey * 4; i += 4) {
338                                 blend_color_mix_byte(&render_rect[i], &render_rect[i], &gp_rect[i]);
339                         }
340                         GPU_offscreen_unbind(oglrender->ofs, true);
341
342                         MEM_freeN(gp_rect);
343                 }
344         }
345         else {
346                 /* shouldnt suddenly give errors mid-render but possible */
347                 char err_out[256] = "unknown";
348                 ImBuf *ibuf_view;
349                 const int alpha_mode = (draw_sky) ? R_ADDSKY : R_ALPHAPREMUL;
350
351                 unsigned int draw_flags = V3D_OFSDRAW_NONE;
352                 draw_flags |= (oglrender->ofs_full_samples) ? V3D_OFSDRAW_USE_FULL_SAMPLE : 0;
353
354                 if (view_context) {
355                         draw_flags |= (draw_bgpic) ? V3D_OFSDRAW_USE_BACKGROUND : 0;
356
357                         ibuf_view = ED_view3d_draw_offscreen_imbuf(
358                                oglrender->bmain, scene, v3d, ar, sizex, sizey,
359                                IB_rect, draw_flags, alpha_mode, oglrender->ofs_samples, viewname,
360                                oglrender->fx, oglrender->ofs, err_out);
361
362                         /* for stamp only */
363                         if (rv3d->persp == RV3D_CAMOB && v3d->camera) {
364                                 camera = BKE_camera_multiview_render(oglrender->scene, v3d->camera, viewname);
365                         }
366                 }
367                 else {
368                         draw_flags |= (V3D_OFSDRAW_USE_GPENCIL | V3D_OFSDRAW_USE_BACKGROUND);
369                         ibuf_view = ED_view3d_draw_offscreen_imbuf_simple(
370                                 oglrender->bmain, scene, scene->camera, oglrender->sizex, oglrender->sizey,
371                                 IB_rect, draw_flags, OB_SOLID,
372                                 alpha_mode, oglrender->ofs_samples, viewname,
373                                 oglrender->fx, oglrender->ofs, err_out);
374                         camera = scene->camera;
375                 }
376
377                 if (ibuf_view) {
378                         ibuf_result = ibuf_view;
379                         rect = (unsigned char *)ibuf_view->rect;
380                 }
381                 else {
382                         fprintf(stderr, "%s: failed to get buffer, %s\n", __func__, err_out);
383                 }
384         }
385
386         if (ibuf_result != NULL) {
387                 if ((scene->r.stamp & R_STAMP_ALL) && (scene->r.stamp & R_STAMP_DRAW)) {
388                         BKE_image_stamp_buf(scene, camera, NULL, rect, NULL, rr->rectx, rr->recty, 4);
389                 }
390                 RE_render_result_rect_from_ibuf(rr, &scene->r, ibuf_result, oglrender->view_id);
391                 IMB_freeImBuf(ibuf_result);
392         }
393 }
394
395 static void screen_opengl_render_write(OGLRender *oglrender)
396 {
397         Scene *scene = oglrender->scene;
398         RenderResult *rr;
399         bool ok;
400         char name[FILE_MAX];
401
402         rr = RE_AcquireResultRead(oglrender->re);
403
404         BKE_image_path_from_imformat(
405                 name, scene->r.pic, BKE_main_blendfile_path(oglrender->bmain), scene->r.cfra,
406                 &scene->r.im_format, (scene->r.scemode & R_EXTENSION) != 0, false, NULL);
407
408         /* write images as individual images or stereo */
409         BKE_render_result_stamp_info(scene, scene->camera, rr, false);
410         ok = RE_WriteRenderViewsImage(oglrender->reports, rr, scene, false, name);
411
412         RE_ReleaseResultImage(oglrender->re);
413
414         if (ok) printf("OpenGL Render written to '%s'\n", name);
415         else printf("OpenGL Render failed to write '%s'\n", name);
416 }
417
418 static void addAlphaOverFloat(float dest[4], const float source[4])
419 {
420         /* d = s + (1-alpha_s)d*/
421         float mul;
422
423         mul = 1.0f - source[3];
424
425         dest[0] = (mul * dest[0]) + source[0];
426         dest[1] = (mul * dest[1]) + source[1];
427         dest[2] = (mul * dest[2]) + source[2];
428         dest[3] = (mul * dest[3]) + source[3];
429
430 }
431
432 /* add renderlayer and renderpass for each grease pencil layer for using in composition */
433 static void add_gpencil_renderpass(OGLRender *oglrender, RenderResult *rr, RenderView *rv)
434 {
435         bGPdata *gpd = oglrender->scene->gpd;
436         Scene *scene = oglrender->scene;
437
438         /* sanity checks */
439         if (gpd == NULL) {
440                 return;
441         }
442         if (scene == NULL) {
443                 return;
444         }
445         if (BLI_listbase_is_empty(&gpd->layers)) {
446                 return;
447         }
448         if (oglrender->v3d != NULL && (oglrender->v3d->flag2 & V3D_SHOW_GPENCIL) == 0) {
449                 return;
450         }
451
452         /* save old alpha mode */
453         short oldalphamode = scene->r.alphamode;
454         /* set alpha transparent for gp */
455         scene->r.alphamode = R_ALPHAPREMUL;
456
457         /* saves layer status */
458         short *oldsts = MEM_mallocN(BLI_listbase_count(&gpd->layers) * sizeof(short), "temp_gplayers_flag");
459         int i = 0;
460         for (bGPDlayer *gpl = gpd->layers.first; gpl; gpl = gpl->next) {
461                 oldsts[i] = gpl->flag;
462                 ++i;
463         }
464         /* loop all layers to create separate render */
465         for (bGPDlayer *gpl = gpd->layers.first; gpl; gpl = gpl->next) {
466                 /* dont draw layer if hidden */
467                 if (gpl->flag & GP_LAYER_HIDE)
468                         continue;
469                 /* hide all layer except current */
470                 for (bGPDlayer *gph = gpd->layers.first; gph; gph = gph->next) {
471                         if (gpl != gph) {
472                                 gph->flag |= GP_LAYER_HIDE;
473                         }
474                 }
475
476                 /* render this gp layer */
477                 screen_opengl_render_doit(oglrender, rr);
478
479                 /* add RendePass composite */
480                 RenderPass *rp = RE_create_gp_pass(rr, gpl->info, rv->name);
481
482                 /* copy image data from rectf */
483                 // XXX: Needs conversion.
484                 unsigned char *src = (unsigned char *)RE_RenderViewGetById(rr, oglrender->view_id)->rect32;
485                 if (src != NULL) {
486                         float *dest = rp->rect;
487
488                         int x, y, rectx, recty;
489                         rectx = rr->rectx;
490                         recty = rr->recty;
491                         for (y = 0; y < recty; y++) {
492                                 for (x = 0; x < rectx; x++) {
493                                         unsigned char *pixSrc = src + 4 * (rectx * y + x);
494                                         if (pixSrc[3] > 0) {
495                                                 float *pixDest = dest + 4 * (rectx * y + x);
496                                                 float float_src[4];
497                                                 srgb_to_linearrgb_uchar4(float_src, pixSrc);
498                                                 addAlphaOverFloat(pixDest, float_src);
499                                         }
500                                 }
501                         }
502                 }
503                 /* back layer status */
504                 i = 0;
505                 for (bGPDlayer *gph = gpd->layers.first; gph; gph = gph->next) {
506                         gph->flag = oldsts[i];
507                         ++i;
508                 }
509         }
510         /* free memory */
511         MEM_freeN(oldsts);
512
513         /* back default alpha mode */
514         scene->r.alphamode = oldalphamode;
515 }
516
517 static void screen_opengl_render_apply(OGLRender *oglrender)
518 {
519         RenderResult *rr;
520         RenderView *rv;
521         int view_id;
522         ImBuf *ibuf;
523         void *lock;
524
525         if (oglrender->is_sequencer) {
526                 Scene *scene = oglrender->scene;
527
528                 SeqRenderData context;
529                 SpaceSeq *sseq = oglrender->sseq;
530                 int chanshown = sseq ? sseq->chanshown : 0;
531
532                 BKE_sequencer_new_render_data(
533                         oglrender->bmain->eval_ctx, oglrender->bmain, scene,
534                         oglrender->sizex, oglrender->sizey, 100.0f,
535                         &context);
536
537                 for (view_id = 0; view_id < oglrender->views_len; view_id++) {
538                         context.view_id = view_id;
539                         context.gpu_offscreen = oglrender->ofs;
540                         context.gpu_fx = oglrender->fx;
541                         context.gpu_full_samples = oglrender->ofs_full_samples;
542
543                         oglrender->seq_data.ibufs_arr[view_id] = BKE_sequencer_give_ibuf(&context, CFRA, chanshown);
544                 }
545         }
546
547         rr = RE_AcquireResultRead(oglrender->re);
548         for (rv = rr->views.first, view_id = 0; rv; rv = rv->next, view_id++) {
549                 BLI_assert(view_id < oglrender->views_len);
550                 RE_SetActiveRenderView(oglrender->re, rv->name);
551                 oglrender->view_id = view_id;
552                 /* add grease pencil passes. For sequencer, the render does not include renderpasses
553                  * TODO: The sequencer render of grease pencil should be rethought */
554                 if (!oglrender->is_sequencer) {
555                         add_gpencil_renderpass(oglrender, rr, rv);
556                 }
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                 /* so quad view renders camera */
669                 ED_view3d_context_user_region(C, &oglrender->v3d, &oglrender->ar);
670
671                 oglrender->rv3d = oglrender->ar->regiondata;
672
673                 /* MUST be cleared on exit */
674                 oglrender->scene->customdata_mask_modal = ED_view3d_datamask(oglrender->scene, oglrender->v3d);
675
676                 /* apply immediately in case we're rendering from a script,
677                  * running notifiers again will overwrite */
678                 oglrender->scene->customdata_mask |= oglrender->scene->customdata_mask_modal;
679
680                 if (oglrender->v3d->fx_settings.fx_flag & (GPU_FX_FLAG_DOF | GPU_FX_FLAG_SSAO)) {
681                         oglrender->fx = GPU_fx_compositor_create();
682                 }
683         }
684         else if (is_sequencer) {
685                 /* NOTE: We allow animation of DoF setting for flexibility in edits, so
686                  * we can't check in advance whether we need FX compositor or not.
687                  * We just always allocated it and make sure it doesn't add extra
688                  * overhead rather than memory allocation here if it's not really
689                  * needed.
690                  */
691                 oglrender->fx = GPU_fx_compositor_create();
692         }
693
694         /* create render */
695         oglrender->re = RE_NewSceneRender(scene);
696
697         /* create image and image user */
698         oglrender->ima = BKE_image_verify_viewer(oglrender->bmain, IMA_TYPE_R_RESULT, "Render Result");
699         BKE_image_signal(oglrender->bmain, oglrender->ima, NULL, IMA_SIGNAL_FREE);
700         BKE_image_backup_render(oglrender->scene, oglrender->ima, true);
701
702         oglrender->iuser.scene = scene;
703         oglrender->iuser.ok = 1;
704
705         /* create render result */
706         RE_InitState(oglrender->re, NULL, &scene->r, NULL, sizex, sizey, NULL);
707
708         /* create render views */
709         screen_opengl_views_setup(oglrender);
710
711         /* wm vars */
712         oglrender->wm = wm;
713         oglrender->win = win;
714
715         oglrender->totvideos = 0;
716         oglrender->mh = NULL;
717         oglrender->movie_ctx_arr = NULL;
718
719         if (is_animation) {
720                 TaskScheduler *task_scheduler = BLI_task_scheduler_get();
721                 if (BKE_imtype_is_movie(scene->r.im_format.imtype)) {
722                         task_scheduler = BLI_task_scheduler_create(1);
723                         oglrender->task_scheduler = task_scheduler;
724                         oglrender->task_pool = BLI_task_pool_create_background(task_scheduler,
725                                                                                oglrender);
726                 }
727                 else {
728                         oglrender->task_scheduler = NULL;
729                         oglrender->task_pool = BLI_task_pool_create(task_scheduler,
730                                                                     oglrender);
731                 }
732                 oglrender->pool_ok = true;
733                 BLI_spin_init(&oglrender->reports_lock);
734         }
735         else {
736                 oglrender->task_scheduler = NULL;
737                 oglrender->task_pool = NULL;
738         }
739         oglrender->num_scheduled_frames = 0;
740         BLI_mutex_init(&oglrender->task_mutex);
741         BLI_condition_init(&oglrender->task_condition);
742
743 #ifdef DEBUG_TIME
744         oglrender->time_start = PIL_check_seconds_timer();
745 #endif
746
747         return true;
748 }
749
750 static void screen_opengl_render_end(bContext *C, OGLRender *oglrender)
751 {
752         Main *bmain = CTX_data_main(C);
753         Scene *scene = oglrender->scene;
754         int i;
755
756         if (oglrender->is_animation) {
757                 /* Trickery part for movie output:
758                  *
759                  * We MUST write frames in an exact order, so we only let background
760                  * thread to work on that, and main thread is simply waits for that
761                  * thread to do all the dirty work.
762                  *
763                  * After this loop is done work_and_wait() will have nothing to do,
764                  * so we don't run into wrong order of frames written to the stream.
765                  */
766                 if (BKE_imtype_is_movie(scene->r.im_format.imtype)) {
767                         BLI_mutex_lock(&oglrender->task_mutex);
768                         while (oglrender->num_scheduled_frames > 0) {
769                                 BLI_condition_wait(&oglrender->task_condition,
770                                                    &oglrender->task_mutex);
771                         }
772                         BLI_mutex_unlock(&oglrender->task_mutex);
773                 }
774                 BLI_task_pool_work_and_wait(oglrender->task_pool);
775                 BLI_task_pool_free(oglrender->task_pool);
776                 /* Depending on various things we might or might not use global scheduler. */
777                 if (oglrender->task_scheduler != NULL) {
778                         BLI_task_scheduler_free(oglrender->task_scheduler);
779                 }
780                 BLI_spin_end(&oglrender->reports_lock);
781         }
782         BLI_mutex_end(&oglrender->task_mutex);
783         BLI_condition_end(&oglrender->task_condition);
784
785 #ifdef DEBUG_TIME
786         printf("Total render time: %f\n", PIL_check_seconds_timer() - oglrender->time_start);
787 #endif
788
789         if (oglrender->mh) {
790                 if (BKE_imtype_is_movie(scene->r.im_format.imtype)) {
791                         for (i = 0; i < oglrender->totvideos; i++) {
792                                 oglrender->mh->end_movie(oglrender->movie_ctx_arr[i]);
793                                 oglrender->mh->context_free(oglrender->movie_ctx_arr[i]);
794                         }
795                 }
796
797                 if (oglrender->movie_ctx_arr) {
798                         MEM_freeN(oglrender->movie_ctx_arr);
799                 }
800         }
801
802         if (oglrender->timer) { /* exec will not have a timer */
803                 scene->r.cfra = oglrender->cfrao;
804                 BKE_scene_update_for_newframe(bmain->eval_ctx, bmain, scene, screen_opengl_layers(oglrender));
805
806                 WM_event_remove_timer(oglrender->wm, oglrender->win, oglrender->timer);
807         }
808
809         WM_cursor_modal_restore(oglrender->win);
810
811         WM_event_add_notifier(C, NC_SCENE | ND_RENDER_RESULT, oglrender->scene);
812
813         if (oglrender->fx)
814                 GPU_fx_compositor_destroy(oglrender->fx);
815
816         GPU_offscreen_free(oglrender->ofs);
817
818         if (oglrender->is_sequencer) {
819                 MEM_freeN(oglrender->seq_data.ibufs_arr);
820         }
821
822         oglrender->scene->customdata_mask_modal = 0;
823
824         CTX_wm_area_set(C, oglrender->prevsa);
825         CTX_wm_region_set(C, oglrender->prevar);
826
827         MEM_freeN(oglrender);
828 }
829
830 static void screen_opengl_render_cancel(bContext *C, wmOperator *op)
831 {
832         screen_opengl_render_end(C, op->customdata);
833 }
834
835 /* share between invoke and exec */
836 static bool screen_opengl_render_anim_initialize(bContext *C, wmOperator *op)
837 {
838         /* initialize animation */
839         OGLRender *oglrender;
840         Scene *scene;
841
842         oglrender = op->customdata;
843         scene = oglrender->scene;
844         oglrender->totvideos = BKE_scene_multiview_num_videos_get(&scene->r);
845
846         oglrender->reports = op->reports;
847
848         if (BKE_imtype_is_movie(scene->r.im_format.imtype)) {
849                 size_t width, height;
850                 int i;
851
852                 BKE_scene_multiview_videos_dimensions_get(&scene->r, oglrender->sizex, oglrender->sizey, &width, &height);
853                 oglrender->mh = BKE_movie_handle_get(scene->r.im_format.imtype);
854
855                 if (oglrender->mh == NULL) {
856                         BKE_report(oglrender->reports, RPT_ERROR, "Movie format unsupported");
857                         screen_opengl_render_end(C, oglrender);
858                         return false;
859                 }
860
861                 oglrender->movie_ctx_arr = MEM_mallocN(sizeof(void *) * oglrender->totvideos, "Movies");
862
863                 for (i = 0; i < oglrender->totvideos; i++) {
864                         const char *suffix = BKE_scene_multiview_view_id_suffix_get(&scene->r, i);
865
866                         oglrender->movie_ctx_arr[i] = oglrender->mh->context_create();
867                         if (!oglrender->mh->start_movie(oglrender->movie_ctx_arr[i], scene, &scene->r, oglrender->sizex,
868                                                         oglrender->sizey, oglrender->reports, PRVRANGEON != 0, suffix))
869                         {
870                                 screen_opengl_render_end(C, oglrender);
871                                 return false;
872                         }
873                 }
874         }
875
876         oglrender->cfrao = scene->r.cfra;
877         oglrender->nfra = PSFRA;
878         scene->r.cfra = PSFRA;
879
880         return true;
881 }
882
883 typedef struct WriteTaskData {
884         RenderResult *rr;
885         Scene tmp_scene;
886 } WriteTaskData;
887
888 static void write_result_func(TaskPool * __restrict pool,
889                               void *task_data_v,
890                               int UNUSED(thread_id))
891 {
892         OGLRender *oglrender = (OGLRender *) BLI_task_pool_userdata(pool);
893         WriteTaskData *task_data = (WriteTaskData *) task_data_v;
894         Scene *scene = &task_data->tmp_scene;
895         RenderResult *rr = task_data->rr;
896         const bool is_movie = BKE_imtype_is_movie(scene->r.im_format.imtype);
897         const int cfra = scene->r.cfra;
898         bool ok;
899         /* Don't attempt to write if we've got an error. */
900         if (!oglrender->pool_ok) {
901                 RE_FreeRenderResult(rr);
902                 BLI_mutex_lock(&oglrender->task_mutex);
903                 oglrender->num_scheduled_frames--;
904                 BLI_condition_notify_all(&oglrender->task_condition);
905                 BLI_mutex_unlock(&oglrender->task_mutex);
906                 return;
907         }
908         /* Construct local thread0safe copy of reports structure which we can
909          * safely pass to the underlying functions.
910          */
911         ReportList reports;
912         BKE_reports_init(&reports, oglrender->reports->flag & ~RPT_PRINT);
913         /* Do actual save logic here, depending on the file format.
914          *
915          * NOTE: We have to construct temporary scene with proper scene->r.cfra.
916          * This is because underlying calls do not use r.cfra but use scene
917          * for that.
918          */
919         if (is_movie) {
920                 ok = RE_WriteRenderViewsMovie(&reports,
921                                               rr,
922                                               scene,
923                                               &scene->r,
924                                               oglrender->mh,
925                                               oglrender->movie_ctx_arr,
926                                               oglrender->totvideos,
927                                               PRVRANGEON != 0);
928         }
929         else {
930                 /* TODO(sergey): We can in theory save some CPU ticks here because we
931                  * calculate file name again here.
932                  */
933                 char name[FILE_MAX];
934                 BKE_image_path_from_imformat(name,
935                                              scene->r.pic,
936                                              BKE_main_blendfile_path(oglrender->bmain),
937                                              cfra,
938                                              &scene->r.im_format,
939                                              (scene->r.scemode & R_EXTENSION) != 0,
940                                              true,
941                                              NULL);
942
943                 BKE_render_result_stamp_info(scene, scene->camera, rr, false);
944                 ok = RE_WriteRenderViewsImage(NULL, rr, scene, true, name);
945                 if (!ok) {
946                         BKE_reportf(&reports,
947                                     RPT_ERROR,
948                                     "Write error: cannot save %s",
949                                     name);
950                 }
951         }
952         if (reports.list.first != NULL) {
953                 BLI_spin_lock(&oglrender->reports_lock);
954                 for (Report *report = reports.list.first;
955                      report != NULL;
956                      report = report->next)
957                 {
958                         BKE_report(oglrender->reports,
959                                    report->type,
960                                    report->message);
961                 }
962                 BLI_spin_unlock(&oglrender->reports_lock);
963         }
964         if (!ok) {
965                 oglrender->pool_ok = false;
966         }
967         RE_FreeRenderResult(rr);
968         BLI_mutex_lock(&oglrender->task_mutex);
969         oglrender->num_scheduled_frames--;
970         BLI_condition_notify_all(&oglrender->task_condition);
971         BLI_mutex_unlock(&oglrender->task_mutex);
972 }
973
974 static bool schedule_write_result(OGLRender *oglrender, RenderResult *rr)
975 {
976         if (!oglrender->pool_ok) {
977                 RE_FreeRenderResult(rr);
978                 return false;
979         }
980         Scene *scene = oglrender->scene;
981         WriteTaskData *task_data = MEM_mallocN(sizeof(WriteTaskData), "write task data");
982         task_data->rr = rr;
983         task_data->tmp_scene = *scene;
984         BLI_mutex_lock(&oglrender->task_mutex);
985         oglrender->num_scheduled_frames++;
986         if (oglrender->num_scheduled_frames > MAX_SCHEDULED_FRAMES) {
987                 BLI_condition_wait(&oglrender->task_condition,
988                                    &oglrender->task_mutex);
989         }
990         BLI_mutex_unlock(&oglrender->task_mutex);
991         BLI_task_pool_push(oglrender->task_pool,
992                            write_result_func,
993                            task_data,
994                            true,
995                            TASK_PRIORITY_LOW);
996         return true;
997 }
998
999 static bool screen_opengl_render_anim_step(bContext *C, wmOperator *op)
1000 {
1001         Main *bmain = CTX_data_main(C);
1002         OGLRender *oglrender = op->customdata;
1003         Scene *scene = oglrender->scene;
1004         char name[FILE_MAX];
1005         bool ok = false;
1006         const bool view_context = (oglrender->v3d != NULL);
1007         bool is_movie;
1008         RenderResult *rr;
1009
1010         /* go to next frame */
1011         if (CFRA < oglrender->nfra)
1012                 CFRA++;
1013         while (CFRA < oglrender->nfra) {
1014                 unsigned int lay = screen_opengl_layers(oglrender);
1015
1016                 if (lay & 0xFF000000)
1017                         lay &= 0xFF000000;
1018
1019                 BKE_scene_update_for_newframe(bmain->eval_ctx, bmain, scene, lay);
1020                 CFRA++;
1021         }
1022
1023         is_movie = BKE_imtype_is_movie(scene->r.im_format.imtype);
1024
1025         if (!is_movie) {
1026                 BKE_image_path_from_imformat(
1027                         name, scene->r.pic, BKE_main_blendfile_path(oglrender->bmain), scene->r.cfra,
1028                         &scene->r.im_format, (scene->r.scemode & R_EXTENSION) != 0, true, NULL);
1029
1030                 if ((scene->r.mode & R_NO_OVERWRITE) && BLI_exists(name)) {
1031                         BLI_spin_lock(&oglrender->reports_lock);
1032                         BKE_reportf(op->reports, RPT_INFO, "Skipping existing frame \"%s\"", name);
1033                         BLI_spin_unlock(&oglrender->reports_lock);
1034                         ok = true;
1035                         goto finally;
1036                 }
1037         }
1038
1039         WM_cursor_time(oglrender->win, scene->r.cfra);
1040
1041         BKE_scene_update_for_newframe(bmain->eval_ctx, bmain, scene, screen_opengl_layers(oglrender));
1042
1043         if (view_context) {
1044                 if (oglrender->rv3d->persp == RV3D_CAMOB && oglrender->v3d->camera && oglrender->v3d->scenelock) {
1045                         /* since BKE_scene_update_for_newframe() is used rather
1046                          * then ED_update_for_newframe() the camera needs to be set */
1047                         if (BKE_scene_camera_switch_update(scene)) {
1048                                 oglrender->v3d->camera = scene->camera;
1049                         }
1050                 }
1051         }
1052         else {
1053                 BKE_scene_camera_switch_update(scene);
1054         }
1055
1056         /* render into offscreen buffer */
1057         screen_opengl_render_apply(oglrender);
1058
1059         /* save to disk */
1060         rr = RE_AcquireResultRead(oglrender->re);
1061         RenderResult *new_rr = RE_DuplicateRenderResult(rr);
1062         RE_ReleaseResult(oglrender->re);
1063
1064         ok = schedule_write_result(oglrender, new_rr);
1065
1066 finally:  /* Step the frame and bail early if needed */
1067
1068         /* go to next frame */
1069         oglrender->nfra += scene->r.frame_step;
1070
1071         /* stop at the end or on error */
1072         if (CFRA >= PEFRA || !ok) {
1073                 screen_opengl_render_end(C, op->customdata);
1074                 return 0;
1075         }
1076
1077         return 1;
1078 }
1079
1080
1081 static int screen_opengl_render_modal(bContext *C, wmOperator *op, const wmEvent *event)
1082 {
1083         OGLRender *oglrender = op->customdata;
1084         const bool anim = RNA_boolean_get(op->ptr, "animation");
1085         bool ret;
1086
1087         switch (event->type) {
1088                 case ESCKEY:
1089                         /* cancel */
1090                         oglrender->pool_ok = false;  /* Flag pool for cancel. */
1091                         screen_opengl_render_end(C, op->customdata);
1092                         return OPERATOR_FINISHED;
1093                 case TIMER:
1094                         /* render frame? */
1095                         if (oglrender->timer == event->customdata)
1096                                 break;
1097                         ATTR_FALLTHROUGH;
1098                 default:
1099                         /* nothing to do */
1100                         return OPERATOR_RUNNING_MODAL;
1101         }
1102
1103         /* run first because screen_opengl_render_anim_step can free oglrender */
1104         WM_event_add_notifier(C, NC_SCENE | ND_RENDER_RESULT, oglrender->scene);
1105
1106         if (anim == 0) {
1107                 screen_opengl_render_apply(op->customdata);
1108                 screen_opengl_render_end(C, op->customdata);
1109                 return OPERATOR_FINISHED;
1110         }
1111         else {
1112                 ret = screen_opengl_render_anim_step(C, op);
1113         }
1114
1115         /* stop at the end or on error */
1116         if (ret == false) {
1117                 return OPERATOR_FINISHED;
1118         }
1119
1120         return OPERATOR_RUNNING_MODAL;
1121 }
1122
1123 static int screen_opengl_render_invoke(bContext *C, wmOperator *op, const wmEvent *event)
1124 {
1125         OGLRender *oglrender;
1126         const bool anim = RNA_boolean_get(op->ptr, "animation");
1127
1128         if (!screen_opengl_render_init(C, op))
1129                 return OPERATOR_CANCELLED;
1130
1131         if (anim) {
1132                 if (!screen_opengl_render_anim_initialize(C, op))
1133                         return OPERATOR_CANCELLED;
1134         }
1135
1136         oglrender = op->customdata;
1137         render_view_open(C, event->x, event->y, op->reports);
1138
1139         /* view may be changed above (R_OUTPUT_WINDOW) */
1140         oglrender->win = CTX_wm_window(C);
1141
1142         WM_event_add_modal_handler(C, op);
1143         oglrender->timer = WM_event_add_timer(oglrender->wm, oglrender->win, TIMER, 0.01f);
1144
1145         return OPERATOR_RUNNING_MODAL;
1146 }
1147
1148 /* executes blocking render */
1149 static int screen_opengl_render_exec(bContext *C, wmOperator *op)
1150 {
1151         const bool is_animation = RNA_boolean_get(op->ptr, "animation");
1152
1153         if (!screen_opengl_render_init(C, op))
1154                 return OPERATOR_CANCELLED;
1155
1156         if (!is_animation) { /* same as invoke */
1157                 /* render image */
1158                 screen_opengl_render_apply(op->customdata);
1159                 screen_opengl_render_end(C, op->customdata);
1160
1161                 return OPERATOR_FINISHED;
1162         }
1163         else {
1164                 bool ret = true;
1165
1166                 if (!screen_opengl_render_anim_initialize(C, op))
1167                         return OPERATOR_CANCELLED;
1168
1169                 while (ret) {
1170                         ret = screen_opengl_render_anim_step(C, op);
1171                 }
1172         }
1173
1174         /* no redraw needed, we leave state as we entered it */
1175 //      ED_update_for_newframe(C, 1);
1176         WM_event_add_notifier(C, NC_SCENE | ND_RENDER_RESULT, CTX_data_scene(C));
1177
1178         return OPERATOR_FINISHED;
1179 }
1180
1181 void RENDER_OT_opengl(wmOperatorType *ot)
1182 {
1183         PropertyRNA *prop;
1184
1185         /* identifiers */
1186         ot->name = "OpenGL Render";
1187         ot->description = "OpenGL render active viewport";
1188         ot->idname = "RENDER_OT_opengl";
1189
1190         /* api callbacks */
1191         ot->invoke = screen_opengl_render_invoke;
1192         ot->exec = screen_opengl_render_exec; /* blocking */
1193         ot->modal = screen_opengl_render_modal;
1194         ot->cancel = screen_opengl_render_cancel;
1195
1196         ot->poll = ED_operator_screenactive;
1197
1198         prop = RNA_def_boolean(ot->srna, "animation", 0, "Animation", "Render files from the animation range of this scene");
1199         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
1200         prop = RNA_def_boolean(ot->srna, "sequencer", 0, "Sequencer", "Render using the sequencer's OpenGL display");
1201         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
1202         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)");
1203         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
1204         prop = RNA_def_boolean(ot->srna, "view_context", 1, "View Context", "Use the current 3D view for rendering, else use scene settings");
1205         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
1206
1207 }
1208
1209 /* function for getting an opengl buffer from a View3D, used by sequencer */
1210 // extern void *sequencer_view3d_cb;