Workbench: API Changes
[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_threads.h"
42 #include "BLI_task.h"
43
44 #include "DNA_scene_types.h"
45 #include "DNA_object_types.h"
46 #include "DNA_gpencil_types.h"
47
48 #include "BKE_camera.h"
49 #include "BKE_context.h"
50 #include "BKE_global.h"
51 #include "BKE_image.h"
52 #include "BKE_main.h"
53 #include "BKE_report.h"
54 #include "BKE_scene.h"
55 #include "BKE_sequencer.h"
56 #include "BKE_writeavi.h"
57
58 #include "DEG_depsgraph.h"
59
60 #include "DRW_engine.h"
61
62 #include "WM_api.h"
63 #include "WM_types.h"
64
65 #include "ED_screen.h"
66 #include "ED_view3d.h"
67 #include "ED_gpencil.h"
68
69 #include "RE_pipeline.h"
70 #include "IMB_imbuf_types.h"
71 #include "IMB_imbuf.h"
72 #include "IMB_colormanagement.h"
73
74 #include "RNA_access.h"
75 #include "RNA_define.h"
76
77 #include "GPU_framebuffer.h"
78 #include "GPU_glew.h"
79 #include "GPU_matrix.h"
80
81 #include "render_intern.h"
82
83 /* Define this to get timing information. */
84 // #undef DEBUG_TIME
85
86 #ifdef DEBUG_TIME
87 #  include "PIL_time.h"
88 #endif
89
90 // TODO(sergey): Find better approximation of the scheduled frames.
91 // For really highres renders it might fail still.
92 #define MAX_SCHEDULED_FRAMES 8
93
94 typedef struct OGLRender {
95         Main *bmain;
96         Render *re;
97         Scene *scene;
98         WorkSpace *workspace;
99         ViewLayer *view_layer;
100         Depsgraph *depsgraph;
101
102         View3D *v3d;
103         RegionView3D *rv3d;
104         ARegion *ar;
105
106         ScrArea *prevsa;
107         ARegion *prevar;
108
109         int views_len;  /* multi-view views */
110
111         bool is_sequencer;
112         SpaceSeq *sseq;
113         struct {
114                 ImBuf **ibufs_arr;
115         } seq_data;
116
117
118         Image *ima;
119         ImageUser iuser;
120
121         GPUOffScreen *ofs;
122         int ofs_samples;
123         bool ofs_full_samples;
124         int sizex, sizey;
125         int write_still;
126
127         ReportList *reports;
128         bMovieHandle *mh;
129         int cfrao, nfra;
130
131         int totvideos;
132
133         /* quick lookup */
134         int view_id;
135
136         /* wm vars for timer and progress cursor */
137         wmWindowManager *wm;
138         wmWindow *win;
139
140         wmTimer *timer; /* use to check if running modal or not (invoke'd or exec'd)*/
141         void **movie_ctx_arr;
142
143         TaskScheduler *task_scheduler;
144         TaskPool *task_pool;
145         bool pool_ok;
146         bool is_animation;
147         SpinLock reports_lock;
148         unsigned int num_scheduled_frames;
149         ThreadMutex task_mutex;
150         ThreadCondition task_condition;
151
152 #ifdef DEBUG_TIME
153         double time_start;
154 #endif
155 } OGLRender;
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(const bContext *C, OGLRender *oglrender, RenderResult *rr)
270 {
271         Depsgraph *depsgraph = CTX_data_depsgraph(C);
272         Scene *scene = oglrender->scene;
273         ARegion *ar = oglrender->ar;
274         View3D *v3d = oglrender->v3d;
275         RegionView3D *rv3d = oglrender->rv3d;
276         Object *camera = NULL;
277         int sizex = oglrender->sizex;
278         int sizey = oglrender->sizey;
279         const short view_context = (v3d != NULL);
280         bool draw_sky = (scene->r.alphamode == R_ADDSKY);
281         float *rectf = 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                         DRW_opengl_context_enable();
323                         GPU_offscreen_bind(oglrender->ofs, true);
324
325                         glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
326                         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
327
328                         wmOrtho2(0, sizex, 0, sizey);
329                         gpuTranslate2f(sizex / 2, sizey / 2);
330
331                         G.f |= G_RENDER_OGL;
332                         ED_gpencil_draw_ex(scene, gpd, sizex, sizey, scene->r.cfra, SPACE_SEQ);
333                         G.f &= ~G_RENDER_OGL;
334
335                         gp_rect = MEM_mallocN(sizex * sizey * sizeof(unsigned char) * 4, "offscreen rect");
336                         GPU_offscreen_read_pixels(oglrender->ofs, GL_UNSIGNED_BYTE, gp_rect);
337
338                         for (i = 0; i < sizex * sizey * 4; i += 4) {
339                                 blend_color_mix_byte(&render_rect[i], &render_rect[i], &gp_rect[i]);
340                         }
341                         GPU_offscreen_unbind(oglrender->ofs, true);
342                         DRW_opengl_context_disable();
343
344                         MEM_freeN(gp_rect);
345                 }
346         }
347         else {
348                 /* shouldnt suddenly give errors mid-render but possible */
349                 char err_out[256] = "unknown";
350                 ImBuf *ibuf_view;
351                 const int alpha_mode = (draw_sky) ? R_ADDSKY : R_ALPHAPREMUL;
352
353                 unsigned int draw_flags = V3D_OFSDRAW_NONE;
354                 draw_flags |= (oglrender->ofs_full_samples) ? V3D_OFSDRAW_USE_FULL_SAMPLE : 0;
355
356                 if (view_context) {
357                         ibuf_view = ED_view3d_draw_offscreen_imbuf(
358                                depsgraph, scene, v3d->drawtype,
359                                v3d, ar, sizex, sizey,
360                                IB_rectfloat, draw_flags, alpha_mode, oglrender->ofs_samples, viewname,
361                                oglrender->ofs, err_out);
362
363                         /* for stamp only */
364                         if (rv3d->persp == RV3D_CAMOB && v3d->camera) {
365                                 camera = BKE_camera_multiview_render(oglrender->scene, v3d->camera, viewname);
366                         }
367                 }
368                 else {
369                         draw_flags |= V3D_OFSDRAW_USE_GPENCIL;
370                         ibuf_view = ED_view3d_draw_offscreen_imbuf_simple(
371                                 depsgraph, scene, OB_SOLID,
372                                 scene->camera, oglrender->sizex, oglrender->sizey,
373                                 IB_rectfloat, draw_flags,
374                                 alpha_mode, oglrender->ofs_samples, viewname,
375                                 oglrender->ofs, err_out);
376                         camera = scene->camera;
377                 }
378
379                 if (ibuf_view) {
380                         ibuf_result = ibuf_view;
381                         rectf = (float *)ibuf_view->rect_float;
382                 }
383                 else {
384                         fprintf(stderr, "%s: failed to get buffer, %s\n", __func__, err_out);
385                 }
386         }
387
388         if (ibuf_result != NULL) {
389                 if ((scene->r.stamp & R_STAMP_ALL) && (scene->r.stamp & R_STAMP_DRAW)) {
390                         BKE_image_stamp_buf(scene, camera, NULL, NULL, rectf, rr->rectx, rr->recty, 4);
391                 }
392                 RE_render_result_rect_from_ibuf(rr, &scene->r, ibuf_result, oglrender->view_id);
393                 IMB_freeImBuf(ibuf_result);
394         }
395 }
396
397 static void screen_opengl_render_write(OGLRender *oglrender)
398 {
399         Scene *scene = oglrender->scene;
400         RenderResult *rr;
401         bool ok;
402         char name[FILE_MAX];
403
404         rr = RE_AcquireResultRead(oglrender->re);
405
406         BKE_image_path_from_imformat(
407                 name, scene->r.pic, BKE_main_blendfile_path(oglrender->bmain), scene->r.cfra,
408                 &scene->r.im_format, (scene->r.scemode & R_EXTENSION) != 0, false, NULL);
409
410         /* write images as individual images or stereo */
411         BKE_render_result_stamp_info(scene, scene->camera, rr, false);
412         ok = RE_WriteRenderViewsImage(oglrender->reports, rr, scene, false, name);
413
414         RE_ReleaseResultImage(oglrender->re);
415
416         if (ok) printf("OpenGL Render written to '%s'\n", name);
417         else printf("OpenGL Render failed to write '%s'\n", name);
418 }
419
420 static void addAlphaOverFloat(float dest[4], const float source[4])
421 {
422         /* d = s + (1-alpha_s)d*/
423         float mul;
424
425         mul = 1.0f - source[3];
426
427         dest[0] = (mul * dest[0]) + source[0];
428         dest[1] = (mul * dest[1]) + source[1];
429         dest[2] = (mul * dest[2]) + source[2];
430         dest[3] = (mul * dest[3]) + source[3];
431
432 }
433
434 /* add renderlayer and renderpass for each grease pencil layer for using in composition */
435 static void add_gpencil_renderpass(const bContext *C, OGLRender *oglrender, RenderResult *rr, RenderView *rv)
436 {
437         bGPdata *gpd = oglrender->scene->gpd;
438         Scene *scene = oglrender->scene;
439
440         /* sanity checks */
441         if (gpd == NULL) {
442                 return;
443         }
444         if (scene == NULL) {
445                 return;
446         }
447         if (BLI_listbase_is_empty(&gpd->layers)) {
448                 return;
449         }
450         if (oglrender->v3d != NULL && (oglrender->v3d->flag2 & V3D_SHOW_GPENCIL) == 0) {
451                 return;
452         }
453
454         /* save old alpha mode */
455         short oldalphamode = scene->r.alphamode;
456         /* set alpha transparent for gp */
457         scene->r.alphamode = R_ALPHAPREMUL;
458
459         /* saves layer status */
460         short *oldsts = MEM_mallocN(BLI_listbase_count(&gpd->layers) * sizeof(short), "temp_gplayers_flag");
461         int i = 0;
462         for (bGPDlayer *gpl = gpd->layers.first; gpl; gpl = gpl->next) {
463                 oldsts[i] = gpl->flag;
464                 ++i;
465         }
466         /* loop all layers to create separate render */
467         for (bGPDlayer *gpl = gpd->layers.first; gpl; gpl = gpl->next) {
468                 /* dont draw layer if hidden */
469                 if (gpl->flag & GP_LAYER_HIDE)
470                         continue;
471                 /* hide all layer except current */
472                 for (bGPDlayer *gph = gpd->layers.first; gph; gph = gph->next) {
473                         if (gpl != gph) {
474                                 gph->flag |= GP_LAYER_HIDE;
475                         }
476                 }
477
478                 /* render this gp layer */
479                 screen_opengl_render_doit(C, oglrender, rr);
480
481                 /* add RendePass composite */
482                 RenderPass *rp = RE_create_gp_pass(rr, gpl->info, rv->name);
483
484                 /* copy image data from rectf */
485                 // XXX: Needs conversion.
486                 unsigned char *src = (unsigned char *)RE_RenderViewGetById(rr, oglrender->view_id)->rect32;
487                 if (src != NULL) {
488                         float *dest = rp->rect;
489
490                         int x, y, rectx, recty;
491                         rectx = rr->rectx;
492                         recty = rr->recty;
493                         for (y = 0; y < recty; y++) {
494                                 for (x = 0; x < rectx; x++) {
495                                         unsigned char *pixSrc = src + 4 * (rectx * y + x);
496                                         if (pixSrc[3] > 0) {
497                                                 float *pixDest = dest + 4 * (rectx * y + x);
498                                                 float float_src[4];
499                                                 srgb_to_linearrgb_uchar4(float_src, pixSrc);
500                                                 addAlphaOverFloat(pixDest, float_src);
501                                         }
502                                 }
503                         }
504                 }
505                 /* back layer status */
506                 i = 0;
507                 for (bGPDlayer *gph = gpd->layers.first; gph; gph = gph->next) {
508                         gph->flag = oldsts[i];
509                         ++i;
510                 }
511         }
512         /* free memory */
513         MEM_freeN(oldsts);
514
515         /* back default alpha mode */
516         scene->r.alphamode = oldalphamode;
517 }
518
519 static void screen_opengl_render_apply(const bContext *C, OGLRender *oglrender)
520 {
521         RenderResult *rr;
522         RenderView *rv;
523         int view_id;
524         ImBuf *ibuf;
525         void *lock;
526
527         if (oglrender->is_sequencer) {
528                 Scene *scene = oglrender->scene;
529
530                 SeqRenderData context;
531                 SpaceSeq *sseq = oglrender->sseq;
532                 int chanshown = sseq ? sseq->chanshown : 0;
533
534                 BKE_sequencer_new_render_data(
535                         oglrender->bmain, oglrender->depsgraph, scene,
536                         oglrender->sizex, oglrender->sizey, 100.0f, false,
537                         &context);
538
539                 for (view_id = 0; view_id < oglrender->views_len; view_id++) {
540                         context.view_id = view_id;
541                         context.gpu_offscreen = oglrender->ofs;
542                         context.gpu_full_samples = oglrender->ofs_full_samples;
543
544                         oglrender->seq_data.ibufs_arr[view_id] = BKE_sequencer_give_ibuf(&context, CFRA, chanshown);
545                 }
546         }
547
548         rr = RE_AcquireResultRead(oglrender->re);
549         for (rv = rr->views.first, view_id = 0; rv; rv = rv->next, view_id++) {
550                 BLI_assert(view_id < oglrender->views_len);
551                 RE_SetActiveRenderView(oglrender->re, rv->name);
552                 oglrender->view_id = view_id;
553                 /* add grease pencil passes. For sequencer, the render does not include renderpasses
554                  * TODO: The sequencer render of grease pencil should be rethought */
555                 if (!oglrender->is_sequencer) {
556                         add_gpencil_renderpass(C, oglrender, rr, rv);
557                 }
558                 /* render composite */
559                 screen_opengl_render_doit(C, oglrender, rr);
560         }
561
562         RE_ReleaseResult(oglrender->re);
563
564         ibuf = BKE_image_acquire_ibuf(oglrender->ima, &oglrender->iuser, &lock);
565         if (ibuf) {
566                 ibuf->userflags |= IB_DISPLAY_BUFFER_INVALID;
567         }
568         BKE_image_release_ibuf(oglrender->ima, ibuf, lock);
569
570         if (oglrender->write_still) {
571                 screen_opengl_render_write(oglrender);
572         }
573 }
574
575 static bool screen_opengl_fullsample_enabled(Scene *scene)
576 {
577         if (scene->r.scemode & R_FULL_SAMPLE) {
578                 return true;
579         }
580         else {
581                 /* XXX TODO:
582                  * Technically if the hardware supports MSAA we could keep using Blender 2.7x approach.
583                  * However anti-aliasing without full_sample is not playing well even in 2.7x.
584                  *
585                  * For example, if you enable depth of field, there is aliasing, even if the viewport is fine.
586                  * For 2.8x this is more complicated because so many things rely on shader.
587                  * So until we fix the gpu_framebuffer anti-aliasing suupport we need to force full sample.
588                  */
589                 return true;
590         }
591 }
592
593 static bool screen_opengl_render_init(bContext *C, wmOperator *op)
594 {
595         /* new render clears all callbacks */
596         wmWindowManager *wm = CTX_wm_manager(C);
597         wmWindow *win = CTX_wm_window(C);
598         WorkSpace *workspace = CTX_wm_workspace(C);
599
600         Scene *scene = CTX_data_scene(C);
601         ScrArea *prevsa = CTX_wm_area(C);
602         ARegion *prevar = CTX_wm_region(C);
603         GPUOffScreen *ofs;
604         OGLRender *oglrender;
605         int sizex, sizey;
606         const int samples = (scene->r.mode & R_OSA) ? scene->r.osa : 0;
607         const bool full_samples = (samples != 0) && screen_opengl_fullsample_enabled(scene);
608         bool is_view_context = RNA_boolean_get(op->ptr, "view_context");
609         const bool is_animation = RNA_boolean_get(op->ptr, "animation");
610         const bool is_sequencer = RNA_boolean_get(op->ptr, "sequencer");
611         const bool is_write_still = RNA_boolean_get(op->ptr, "write_still");
612         char err_out[256] = "unknown";
613
614         if (G.background) {
615                 BKE_report(op->reports, RPT_ERROR, "Cannot use OpenGL render in background mode (no opengl context)");
616                 return false;
617         }
618
619         /* only one render job at a time */
620         if (WM_jobs_test(wm, scene, WM_JOB_TYPE_RENDER))
621                 return false;
622
623         if (is_sequencer) {
624                 is_view_context = false;
625         }
626         else {
627                 /* ensure we have a 3d view */
628                 if (!ED_view3d_context_activate(C)) {
629                         RNA_boolean_set(op->ptr, "view_context", false);
630                         is_view_context = false;
631                 }
632
633                 if (!is_view_context && scene->camera == NULL) {
634                         BKE_report(op->reports, RPT_ERROR, "Scene has no camera");
635                         return false;
636                 }
637         }
638
639         if (!is_animation && is_write_still && BKE_imtype_is_movie(scene->r.im_format.imtype)) {
640                 BKE_report(op->reports, RPT_ERROR, "Cannot write a single file with an animation format selected");
641                 return false;
642         }
643
644         /* stop all running jobs, except screen one. currently previews frustrate Render */
645         WM_jobs_kill_all_except(wm, CTX_wm_screen(C));
646
647         /* create offscreen buffer */
648         sizex = (scene->r.size * scene->r.xsch) / 100;
649         sizey = (scene->r.size * scene->r.ysch) / 100;
650
651         /* corrects render size with actual size, not every card supports non-power-of-two dimensions */
652         DRW_opengl_context_enable(); /* Offscreen creation needs to be done in DRW context. */
653         ofs = GPU_offscreen_create(sizex, sizey, full_samples ? 0 : samples, true, true, err_out);
654         DRW_opengl_context_disable();
655
656         if (!ofs) {
657                 BKE_reportf(op->reports, RPT_ERROR, "Failed to create OpenGL off-screen buffer, %s", err_out);
658                 return false;
659         }
660
661         /* allocate opengl render */
662         oglrender = MEM_callocN(sizeof(OGLRender), "OGLRender");
663         op->customdata = oglrender;
664
665         oglrender->ofs = ofs;
666         oglrender->ofs_samples = samples;
667         oglrender->ofs_full_samples = full_samples;
668         oglrender->sizex = sizex;
669         oglrender->sizey = sizey;
670         oglrender->bmain = CTX_data_main(C);
671         oglrender->scene = scene;
672         oglrender->workspace = workspace;
673         oglrender->view_layer = CTX_data_view_layer(C);
674         oglrender->depsgraph = CTX_data_depsgraph(C);
675         oglrender->cfrao = scene->r.cfra;
676
677         oglrender->write_still = is_write_still && !is_animation;
678         oglrender->is_animation = is_animation;
679
680         oglrender->views_len = BKE_scene_multiview_num_views_get(&scene->r);
681
682         oglrender->is_sequencer = is_sequencer;
683         if (is_sequencer) {
684                 oglrender->sseq = CTX_wm_space_seq(C);
685                 ImBuf **ibufs_arr = MEM_callocN(sizeof(*ibufs_arr) * oglrender->views_len, __func__);
686                 oglrender->seq_data.ibufs_arr = ibufs_arr;
687         }
688
689         oglrender->prevsa = prevsa;
690         oglrender->prevar = prevar;
691
692         if (is_view_context) {
693                 ED_view3d_context_user_region(C, &oglrender->v3d, &oglrender->ar); /* so quad view renders camera */
694                 oglrender->rv3d = oglrender->ar->regiondata;
695
696                 /* MUST be cleared on exit */
697                 oglrender->scene->customdata_mask_modal = ED_view3d_datamask(oglrender->scene, oglrender->v3d);
698
699                 /* apply immediately in case we're rendering from a script,
700                  * running notifiers again will overwrite */
701                 oglrender->scene->customdata_mask |= oglrender->scene->customdata_mask_modal;
702         }
703
704         /* create render */
705         oglrender->re = RE_NewSceneRender(scene);
706
707         /* create image and image user */
708         oglrender->ima = BKE_image_verify_viewer(IMA_TYPE_R_RESULT, "Render Result");
709         BKE_image_signal(oglrender->ima, NULL, IMA_SIGNAL_FREE);
710         BKE_image_backup_render(oglrender->scene, oglrender->ima, true);
711
712         oglrender->iuser.scene = scene;
713         oglrender->iuser.ok = 1;
714
715         /* create render result */
716         RE_InitState(oglrender->re, NULL, &scene->r, &scene->view_layers, NULL, sizex, sizey, NULL);
717
718         /* create render views */
719         screen_opengl_views_setup(oglrender);
720
721         /* wm vars */
722         oglrender->wm = wm;
723         oglrender->win = win;
724
725         oglrender->totvideos = 0;
726         oglrender->mh = NULL;
727         oglrender->movie_ctx_arr = NULL;
728
729         if (is_animation) {
730                 TaskScheduler *task_scheduler = BLI_task_scheduler_get();
731                 if (BKE_imtype_is_movie(scene->r.im_format.imtype)) {
732                         task_scheduler = BLI_task_scheduler_create(1);
733                         oglrender->task_scheduler = task_scheduler;
734                         oglrender->task_pool = BLI_task_pool_create_background(task_scheduler,
735                                                                                oglrender);
736                 }
737                 else {
738                         oglrender->task_scheduler = NULL;
739                         oglrender->task_pool = BLI_task_pool_create(task_scheduler,
740                                                                     oglrender);
741                 }
742                 oglrender->pool_ok = true;
743                 BLI_spin_init(&oglrender->reports_lock);
744         }
745         else {
746                 oglrender->task_scheduler = NULL;
747                 oglrender->task_pool = NULL;
748         }
749         oglrender->num_scheduled_frames = 0;
750         BLI_mutex_init(&oglrender->task_mutex);
751         BLI_condition_init(&oglrender->task_condition);
752
753 #ifdef DEBUG_TIME
754         oglrender->time_start = PIL_check_seconds_timer();
755 #endif
756
757         return true;
758 }
759
760 static void screen_opengl_render_end(bContext *C, OGLRender *oglrender)
761 {
762         Main *bmain = CTX_data_main(C);
763         Scene *scene = oglrender->scene;
764         int i;
765
766         if (oglrender->is_animation) {
767                 /* Trickery part for movie output:
768                  *
769                  * We MUST write frames in an exact order, so we only let background
770                  * thread to work on that, and main thread is simply waits for that
771                  * thread to do all the dirty work.
772                  *
773                  * After this loop is done work_and_wait() will have nothing to do,
774                  * so we don't run into wrong order of frames written to the stream.
775                  */
776                 if (BKE_imtype_is_movie(scene->r.im_format.imtype)) {
777                         BLI_mutex_lock(&oglrender->task_mutex);
778                         while (oglrender->num_scheduled_frames > 0) {
779                                 BLI_condition_wait(&oglrender->task_condition,
780                                                    &oglrender->task_mutex);
781                         }
782                         BLI_mutex_unlock(&oglrender->task_mutex);
783                 }
784                 BLI_task_pool_work_and_wait(oglrender->task_pool);
785                 BLI_task_pool_free(oglrender->task_pool);
786                 /* Depending on various things we might or might not use global scheduler. */
787                 if (oglrender->task_scheduler != NULL) {
788                         BLI_task_scheduler_free(oglrender->task_scheduler);
789                 }
790                 BLI_spin_end(&oglrender->reports_lock);
791         }
792         BLI_mutex_end(&oglrender->task_mutex);
793         BLI_condition_end(&oglrender->task_condition);
794
795 #ifdef DEBUG_TIME
796         printf("Total render time: %f\n", PIL_check_seconds_timer() - oglrender->time_start);
797 #endif
798
799         if (oglrender->mh) {
800                 if (BKE_imtype_is_movie(scene->r.im_format.imtype)) {
801                         for (i = 0; i < oglrender->totvideos; i++) {
802                                 oglrender->mh->end_movie(oglrender->movie_ctx_arr[i]);
803                                 oglrender->mh->context_free(oglrender->movie_ctx_arr[i]);
804                         }
805                 }
806
807                 if (oglrender->movie_ctx_arr) {
808                         MEM_freeN(oglrender->movie_ctx_arr);
809                 }
810         }
811
812         if (oglrender->timer) { /* exec will not have a timer */
813                 Depsgraph *depsgraph = oglrender->depsgraph;
814                 scene->r.cfra = oglrender->cfrao;
815                 BKE_scene_graph_update_for_newframe(depsgraph, bmain);
816
817                 WM_event_remove_timer(oglrender->wm, oglrender->win, oglrender->timer);
818         }
819
820         WM_cursor_modal_restore(oglrender->win);
821
822         WM_event_add_notifier(C, NC_SCENE | ND_RENDER_RESULT, oglrender->scene);
823
824         DRW_opengl_context_enable();
825         GPU_offscreen_free(oglrender->ofs);
826         DRW_opengl_context_disable();
827
828         if (oglrender->is_sequencer) {
829                 MEM_freeN(oglrender->seq_data.ibufs_arr);
830         }
831
832         oglrender->scene->customdata_mask_modal = 0;
833
834         CTX_wm_area_set(C, oglrender->prevsa);
835         CTX_wm_region_set(C, oglrender->prevar);
836
837         MEM_freeN(oglrender);
838 }
839
840 static void screen_opengl_render_cancel(bContext *C, wmOperator *op)
841 {
842         screen_opengl_render_end(C, op->customdata);
843 }
844
845 /* share between invoke and exec */
846 static bool screen_opengl_render_anim_initialize(bContext *C, wmOperator *op)
847 {
848         /* initialize animation */
849         OGLRender *oglrender;
850         Scene *scene;
851
852         oglrender = op->customdata;
853         scene = oglrender->scene;
854         oglrender->totvideos = BKE_scene_multiview_num_videos_get(&scene->r);
855
856         oglrender->reports = op->reports;
857
858         if (BKE_imtype_is_movie(scene->r.im_format.imtype)) {
859                 size_t width, height;
860                 int i;
861
862                 BKE_scene_multiview_videos_dimensions_get(&scene->r, oglrender->sizex, oglrender->sizey, &width, &height);
863                 oglrender->mh = BKE_movie_handle_get(scene->r.im_format.imtype);
864
865                 if (oglrender->mh == NULL) {
866                         BKE_report(oglrender->reports, RPT_ERROR, "Movie format unsupported");
867                         screen_opengl_render_end(C, oglrender);
868                         return false;
869                 }
870
871                 oglrender->movie_ctx_arr = MEM_mallocN(sizeof(void *) * oglrender->totvideos, "Movies");
872
873                 for (i = 0; i < oglrender->totvideos; i++) {
874                         const char *suffix = BKE_scene_multiview_view_id_suffix_get(&scene->r, i);
875
876                         oglrender->movie_ctx_arr[i] = oglrender->mh->context_create();
877                         if (!oglrender->mh->start_movie(oglrender->movie_ctx_arr[i], scene, &scene->r, oglrender->sizex,
878                                                         oglrender->sizey, oglrender->reports, PRVRANGEON != 0, suffix))
879                         {
880                                 screen_opengl_render_end(C, oglrender);
881                                 return false;
882                         }
883                 }
884         }
885
886         oglrender->cfrao = scene->r.cfra;
887         oglrender->nfra = PSFRA;
888         scene->r.cfra = PSFRA;
889
890         return true;
891 }
892
893 typedef struct WriteTaskData {
894         RenderResult *rr;
895         Scene tmp_scene;
896 } WriteTaskData;
897
898 static void write_result_func(TaskPool * __restrict pool,
899                               void *task_data_v,
900                               int UNUSED(thread_id))
901 {
902         OGLRender *oglrender = (OGLRender *) BLI_task_pool_userdata(pool);
903         WriteTaskData *task_data = (WriteTaskData *) task_data_v;
904         Scene *scene = &task_data->tmp_scene;
905         RenderResult *rr = task_data->rr;
906         const bool is_movie = BKE_imtype_is_movie(scene->r.im_format.imtype);
907         const int cfra = scene->r.cfra;
908         bool ok;
909         /* Don't attempt to write if we've got an error. */
910         if (!oglrender->pool_ok) {
911                 RE_FreeRenderResult(rr);
912                 BLI_mutex_lock(&oglrender->task_mutex);
913                 oglrender->num_scheduled_frames--;
914                 BLI_condition_notify_all(&oglrender->task_condition);
915                 BLI_mutex_unlock(&oglrender->task_mutex);
916                 return;
917         }
918         /* Construct local thread0safe copy of reports structure which we can
919          * safely pass to the underlying functions.
920          */
921         ReportList reports;
922         BKE_reports_init(&reports, oglrender->reports->flag & ~RPT_PRINT);
923         /* Do actual save logic here, depending on the file format.
924          *
925          * NOTE: We have to construct temporary scene with proper scene->r.cfra.
926          * This is because underlying calls do not use r.cfra but use scene
927          * for that.
928          */
929         if (is_movie) {
930                 ok = RE_WriteRenderViewsMovie(&reports,
931                                               rr,
932                                               scene,
933                                               &scene->r,
934                                               oglrender->mh,
935                                               oglrender->movie_ctx_arr,
936                                               oglrender->totvideos,
937                                               PRVRANGEON != 0);
938         }
939         else {
940                 /* TODO(sergey): We can in theory save some CPU ticks here because we
941                  * calculate file name again here.
942                  */
943                 char name[FILE_MAX];
944                 BKE_image_path_from_imformat(name,
945                                              scene->r.pic,
946                                              BKE_main_blendfile_path(oglrender->bmain),
947                                              cfra,
948                                              &scene->r.im_format,
949                                              (scene->r.scemode & R_EXTENSION) != 0,
950                                              true,
951                                              NULL);
952
953                 BKE_render_result_stamp_info(scene, scene->camera, rr, false);
954                 ok = RE_WriteRenderViewsImage(NULL, rr, scene, true, name);
955                 if (!ok) {
956                         BKE_reportf(&reports,
957                                     RPT_ERROR,
958                                     "Write error: cannot save %s",
959                                     name);
960                 }
961         }
962         if (reports.list.first != NULL) {
963                 BLI_spin_lock(&oglrender->reports_lock);
964                 for (Report *report = reports.list.first;
965                      report != NULL;
966                      report = report->next)
967                 {
968                         BKE_report(oglrender->reports,
969                                    report->type,
970                                    report->message);
971                 }
972                 BLI_spin_unlock(&oglrender->reports_lock);
973         }
974         if (!ok) {
975                 oglrender->pool_ok = false;
976         }
977         RE_FreeRenderResult(rr);
978         BLI_mutex_lock(&oglrender->task_mutex);
979         oglrender->num_scheduled_frames--;
980         BLI_condition_notify_all(&oglrender->task_condition);
981         BLI_mutex_unlock(&oglrender->task_mutex);
982 }
983
984 static bool schedule_write_result(OGLRender *oglrender, RenderResult *rr)
985 {
986         if (!oglrender->pool_ok) {
987                 RE_FreeRenderResult(rr);
988                 return false;
989         }
990         Scene *scene = oglrender->scene;
991         WriteTaskData *task_data = MEM_mallocN(sizeof(WriteTaskData), "write task data");
992         task_data->rr = rr;
993         task_data->tmp_scene = *scene;
994         BLI_mutex_lock(&oglrender->task_mutex);
995         oglrender->num_scheduled_frames++;
996         if (oglrender->num_scheduled_frames > MAX_SCHEDULED_FRAMES) {
997                 BLI_condition_wait(&oglrender->task_condition,
998                                    &oglrender->task_mutex);
999         }
1000         BLI_mutex_unlock(&oglrender->task_mutex);
1001         BLI_task_pool_push(oglrender->task_pool,
1002                            write_result_func,
1003                            task_data,
1004                            true,
1005                            TASK_PRIORITY_LOW);
1006         return true;
1007 }
1008
1009 static bool screen_opengl_render_anim_step(bContext *C, wmOperator *op)
1010 {
1011         Main *bmain = CTX_data_main(C);
1012         OGLRender *oglrender = op->customdata;
1013         Scene *scene = oglrender->scene;
1014         Depsgraph *depsgraph = oglrender->depsgraph;
1015         char name[FILE_MAX];
1016         bool ok = false;
1017         const bool view_context = (oglrender->v3d != NULL);
1018         bool is_movie;
1019         RenderResult *rr;
1020
1021         /* go to next frame */
1022         if (CFRA < oglrender->nfra)
1023                 CFRA++;
1024         while (CFRA < oglrender->nfra) {
1025                 BKE_scene_graph_update_for_newframe(depsgraph, bmain);
1026                 CFRA++;
1027         }
1028
1029         is_movie = BKE_imtype_is_movie(scene->r.im_format.imtype);
1030
1031         if (!is_movie) {
1032                 BKE_image_path_from_imformat(
1033                         name, scene->r.pic, BKE_main_blendfile_path(oglrender->bmain), scene->r.cfra,
1034                         &scene->r.im_format, (scene->r.scemode & R_EXTENSION) != 0, true, NULL);
1035
1036                 if ((scene->r.mode & R_NO_OVERWRITE) && BLI_exists(name)) {
1037                         BLI_spin_lock(&oglrender->reports_lock);
1038                         BKE_reportf(op->reports, RPT_INFO, "Skipping existing frame \"%s\"", name);
1039                         BLI_spin_unlock(&oglrender->reports_lock);
1040                         ok = true;
1041                         goto finally;
1042                 }
1043         }
1044
1045         WM_cursor_time(oglrender->win, scene->r.cfra);
1046
1047         BKE_scene_graph_update_for_newframe(depsgraph, bmain);
1048
1049         if (view_context) {
1050                 if (oglrender->rv3d->persp == RV3D_CAMOB && oglrender->v3d->camera && oglrender->v3d->scenelock) {
1051                         /* since BKE_scene_graph_update_for_newframe() is used rather
1052                          * then ED_update_for_newframe() the camera needs to be set */
1053                         if (BKE_scene_camera_switch_update(scene)) {
1054                                 oglrender->v3d->camera = scene->camera;
1055                         }
1056                 }
1057         }
1058         else {
1059                 BKE_scene_camera_switch_update(scene);
1060         }
1061
1062         /* render into offscreen buffer */
1063         screen_opengl_render_apply(C, oglrender);
1064
1065         /* save to disk */
1066         rr = RE_AcquireResultRead(oglrender->re);
1067         RenderResult *new_rr = RE_DuplicateRenderResult(rr);
1068         RE_ReleaseResult(oglrender->re);
1069
1070         ok = schedule_write_result(oglrender, new_rr);
1071
1072 finally:  /* Step the frame and bail early if needed */
1073
1074         /* go to next frame */
1075         oglrender->nfra += scene->r.frame_step;
1076
1077         /* stop at the end or on error */
1078         if (CFRA >= PEFRA || !ok) {
1079                 screen_opengl_render_end(C, op->customdata);
1080                 return 0;
1081         }
1082
1083         return 1;
1084 }
1085
1086
1087 static int screen_opengl_render_modal(bContext *C, wmOperator *op, const wmEvent *event)
1088 {
1089         OGLRender *oglrender = op->customdata;
1090         const bool anim = RNA_boolean_get(op->ptr, "animation");
1091         bool ret;
1092
1093         switch (event->type) {
1094                 case ESCKEY:
1095                         /* cancel */
1096                         oglrender->pool_ok = false;  /* Flag pool for cancel. */
1097                         screen_opengl_render_end(C, op->customdata);
1098                         return OPERATOR_FINISHED;
1099                 case TIMER:
1100                         /* render frame? */
1101                         if (oglrender->timer == event->customdata)
1102                                 break;
1103                         ATTR_FALLTHROUGH;
1104                 default:
1105                         /* nothing to do */
1106                         return OPERATOR_RUNNING_MODAL;
1107         }
1108
1109         /* run first because screen_opengl_render_anim_step can free oglrender */
1110         WM_event_add_notifier(C, NC_SCENE | ND_RENDER_RESULT, oglrender->scene);
1111
1112         if (anim == 0) {
1113                 screen_opengl_render_apply(C, op->customdata);
1114                 screen_opengl_render_end(C, op->customdata);
1115                 return OPERATOR_FINISHED;
1116         }
1117         else {
1118                 ret = screen_opengl_render_anim_step(C, op);
1119         }
1120
1121         /* stop at the end or on error */
1122         if (ret == false) {
1123                 return OPERATOR_FINISHED;
1124         }
1125
1126         return OPERATOR_RUNNING_MODAL;
1127 }
1128
1129 static int screen_opengl_render_invoke(bContext *C, wmOperator *op, const wmEvent *event)
1130 {
1131         OGLRender *oglrender;
1132         const bool anim = RNA_boolean_get(op->ptr, "animation");
1133
1134         if (!screen_opengl_render_init(C, op))
1135                 return OPERATOR_CANCELLED;
1136
1137         if (anim) {
1138                 if (!screen_opengl_render_anim_initialize(C, op))
1139                         return OPERATOR_CANCELLED;
1140         }
1141
1142         oglrender = op->customdata;
1143         render_view_open(C, event->x, event->y, op->reports);
1144
1145         /* view may be changed above (R_OUTPUT_WINDOW) */
1146         oglrender->win = CTX_wm_window(C);
1147
1148         WM_event_add_modal_handler(C, op);
1149         oglrender->timer = WM_event_add_timer(oglrender->wm, oglrender->win, TIMER, 0.01f);
1150
1151         return OPERATOR_RUNNING_MODAL;
1152 }
1153
1154 /* executes blocking render */
1155 static int screen_opengl_render_exec(bContext *C, wmOperator *op)
1156 {
1157         const bool is_animation = RNA_boolean_get(op->ptr, "animation");
1158
1159         if (!screen_opengl_render_init(C, op))
1160                 return OPERATOR_CANCELLED;
1161
1162         if (!is_animation) { /* same as invoke */
1163                 /* render image */
1164                 screen_opengl_render_apply(C, op->customdata);
1165                 screen_opengl_render_end(C, op->customdata);
1166
1167                 return OPERATOR_FINISHED;
1168         }
1169         else {
1170                 bool ret = true;
1171
1172                 if (!screen_opengl_render_anim_initialize(C, op))
1173                         return OPERATOR_CANCELLED;
1174
1175                 while (ret) {
1176                         ret = screen_opengl_render_anim_step(C, op);
1177                 }
1178         }
1179
1180         /* no redraw needed, we leave state as we entered it */
1181 //      ED_update_for_newframe(C);
1182         WM_event_add_notifier(C, NC_SCENE | ND_RENDER_RESULT, CTX_data_scene(C));
1183
1184         return OPERATOR_FINISHED;
1185 }
1186
1187 void RENDER_OT_opengl(wmOperatorType *ot)
1188 {
1189         PropertyRNA *prop;
1190
1191         /* identifiers */
1192         ot->name = "OpenGL Render";
1193         ot->description = "OpenGL render active viewport";
1194         ot->idname = "RENDER_OT_opengl";
1195
1196         /* api callbacks */
1197         ot->invoke = screen_opengl_render_invoke;
1198         ot->exec = screen_opengl_render_exec; /* blocking */
1199         ot->modal = screen_opengl_render_modal;
1200         ot->cancel = screen_opengl_render_cancel;
1201
1202         ot->poll = ED_operator_screenactive;
1203
1204         prop = RNA_def_boolean(ot->srna, "animation", 0, "Animation", "Render files from the animation range of this scene");
1205         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
1206         prop = RNA_def_boolean(ot->srna, "sequencer", 0, "Sequencer", "Render using the sequencer's OpenGL display");
1207         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
1208         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)");
1209         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
1210         prop = RNA_def_boolean(ot->srna, "view_context", 1, "View Context", "Use the current 3D view for rendering, else use scene settings");
1211         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
1212
1213 }
1214
1215 /* function for getting an opengl buffer from a View3D, used by sequencer */
1216 // extern void *sequencer_view3d_cb;