Color Management: finish pipeline 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 <GL/glew.h>
35
36 #include "MEM_guardedalloc.h"
37
38 #include "BLI_math.h"
39 #include "BLI_blenlib.h"
40 #include "BLI_dlrbTree.h"
41 #include "BLI_utildefines.h"
42 #include "BLI_jitter.h"
43
44 #include "DNA_scene_types.h"
45 #include "DNA_object_types.h"
46
47 #include "BKE_context.h"
48 #include "BKE_global.h"
49 #include "BKE_image.h"
50 #include "BKE_main.h"
51 #include "BKE_report.h"
52 #include "BKE_scene.h"
53 #include "BKE_sequencer.h"
54 #include "BKE_writeavi.h"
55
56 #include "WM_api.h"
57 #include "WM_types.h"
58
59 #include "ED_screen.h"
60 #include "ED_view3d.h"
61 #include "ED_image.h"
62
63 #include "RE_pipeline.h"
64 #include "IMB_imbuf_types.h"
65 #include "IMB_imbuf.h"
66 #include "IMB_colormanagement.h"
67
68 #include "RNA_access.h"
69 #include "RNA_define.h"
70
71
72 #include "GPU_extensions.h"
73
74 #include "wm_window.h"
75
76 #include "render_intern.h"
77
78 typedef struct OGLRender {
79         Main *bmain;
80         Render *re;
81         Scene *scene;
82
83         View3D *v3d;
84         RegionView3D *rv3d;
85         ARegion *ar;
86
87         ScrArea *prevsa;
88         ARegion *prevar;
89
90         short obcenter_dia_back; /* temp overwrite */
91
92         short is_sequencer;
93         SpaceSeq *sseq;
94
95
96         Image *ima;
97         ImageUser iuser;
98
99         GPUOffScreen *ofs;
100         int sizex, sizey;
101         int write_still;
102
103         ReportList *reports;
104         bMovieHandle *mh;
105         int cfrao, nfra;
106
107         wmTimer *timer; /* use to check if running modal or not (invoke'd or exec'd)*/
108 } OGLRender;
109
110 /* added because v3d is not always valid */
111 static unsigned int screen_opengl_layers(OGLRender *oglrender)
112 {
113         if (oglrender->v3d) {
114                 return oglrender->scene->lay | oglrender->v3d->lay;
115         }
116         else {
117                 return oglrender->scene->lay;
118         }
119 }
120
121 static void screen_opengl_render_apply(OGLRender *oglrender)
122 {
123         Scene *scene = oglrender->scene;
124         ARegion *ar = oglrender->ar;
125         View3D *v3d = oglrender->v3d;
126         RegionView3D *rv3d = oglrender->rv3d;
127         RenderResult *rr;
128         Object *camera = NULL;
129         ImBuf *ibuf;
130         void *lock;
131         float winmat[4][4];
132         int sizex = oglrender->sizex;
133         int sizey = oglrender->sizey;
134         const short view_context = (v3d != NULL);
135
136         rr = RE_AcquireResultRead(oglrender->re);
137
138         if (oglrender->is_sequencer) {
139                 SeqRenderData context;
140                 int chanshown = oglrender->sseq ? oglrender->sseq->chanshown : 0;
141
142                 context = BKE_sequencer_new_render_data(oglrender->bmain, scene, oglrender->sizex, oglrender->sizey, 100.0f);
143
144                 ibuf = BKE_sequencer_give_ibuf(context, CFRA, chanshown);
145
146                 if (ibuf) {
147                         BLI_assert((oglrender->sizex == ibuf->x) && (oglrender->sizey == ibuf->y));
148
149                         if (ibuf->rect_float == NULL) {
150                                 /* internally sequencer working in sRGB space and stores both bytes and float
151                                  * buffers in sRGB space, but if byte->float onversion doesn't happen in sequencer
152                                  * (e.g. when adding image sequence/movie into sequencer) there'll be only
153                                  * byte buffer and profile will still indicate sRGB->linear space conversion is needed
154                                  * here we're ensure there'll be no conversion happen and float buffer would store
155                                  * linear frame (sergey) */
156                                 ibuf->profile = IB_PROFILE_NONE;
157                                 IMB_float_from_rect(ibuf);
158                         }
159
160                         memcpy(rr->rectf, ibuf->rect_float, sizeof(float) * 4 * oglrender->sizex * oglrender->sizey);
161
162                         IMB_freeImBuf(ibuf);
163                 }
164         }
165         else if (view_context) {
166                 GPU_offscreen_bind(oglrender->ofs); /* bind */
167
168                 /* render 3d view */
169                 if (rv3d->persp == RV3D_CAMOB && v3d->camera) {
170                         /*int is_ortho= scene->r.mode & R_ORTHO;*/
171                         camera = v3d->camera;
172                         RE_GetCameraWindow(oglrender->re, camera, scene->r.cfra, winmat);
173                         
174                 }
175                 else {
176                         rctf viewplane;
177                         float clipsta, clipend;
178
179                         int is_ortho = ED_view3d_viewplane_get(v3d, rv3d, sizex, sizey, &viewplane, &clipsta, &clipend);
180                         if (is_ortho) orthographic_m4(winmat, viewplane.xmin, viewplane.xmax, viewplane.ymin, viewplane.ymax, -clipend, clipend);
181                         else perspective_m4(winmat, viewplane.xmin, viewplane.xmax, viewplane.ymin, viewplane.ymax, clipsta, clipend);
182                 }
183
184                 if ((scene->r.mode & R_OSA) == 0) { 
185                         ED_view3d_draw_offscreen(scene, v3d, ar, sizex, sizey, NULL, winmat, TRUE, FALSE);
186                         GPU_offscreen_read_pixels(oglrender->ofs, GL_FLOAT, rr->rectf);
187                 }
188                 else {
189                         /* simple accumulation, less hassle then FSAA FBO's */
190                         static float jit_ofs[32][2];
191                         float winmat_jitter[4][4];
192                         float *accum_buffer = MEM_mallocN(sizex * sizey * sizeof(float) * 4, "accum1");
193                         float *accum_tmp = MEM_mallocN(sizex * sizey * sizeof(float) * 4, "accum2");
194                         int j;
195
196                         BLI_jitter_init(jit_ofs[0], scene->r.osa);
197
198                         /* first sample buffer, also initializes 'rv3d->persmat' */
199                         ED_view3d_draw_offscreen(scene, v3d, ar, sizex, sizey, NULL, winmat, TRUE, FALSE);
200                         GPU_offscreen_read_pixels(oglrender->ofs, GL_FLOAT, accum_buffer);
201
202                         /* skip the first sample */
203                         for (j = 1; j < scene->r.osa; j++) {
204                                 copy_m4_m4(winmat_jitter, winmat);
205                                 window_translate_m4(winmat_jitter, rv3d->persmat,
206                                                     (jit_ofs[j][0] * 2.0f) / sizex,
207                                                     (jit_ofs[j][1] * 2.0f) / sizey);
208
209                                 ED_view3d_draw_offscreen(scene, v3d, ar, sizex, sizey, NULL, winmat_jitter, TRUE, FALSE);
210                                 GPU_offscreen_read_pixels(oglrender->ofs, GL_FLOAT, accum_tmp);
211                                 add_vn_vn(accum_buffer, accum_tmp, sizex * sizey * sizeof(float));
212                         }
213
214                         mul_vn_vn_fl(rr->rectf, accum_buffer, sizex * sizey * sizeof(float), 1.0f / scene->r.osa);
215
216                         MEM_freeN(accum_buffer);
217                         MEM_freeN(accum_tmp);
218                 }
219
220                 GPU_offscreen_unbind(oglrender->ofs); /* unbind */
221         }
222         else {
223                 /* shouldnt suddenly give errors mid-render but possible */
224                 char err_out[256] = "unknown";
225                 ImBuf *ibuf_view = ED_view3d_draw_offscreen_imbuf_simple(scene, scene->camera, oglrender->sizex, oglrender->sizey, IB_rectfloat, OB_SOLID, TRUE, FALSE, err_out);
226                 camera = scene->camera;
227
228                 if (ibuf_view) {
229                         memcpy(rr->rectf, ibuf_view->rect_float, sizeof(float) * 4 * oglrender->sizex * oglrender->sizey);
230                         IMB_freeImBuf(ibuf_view);
231                 }
232                 else {
233                         fprintf(stderr, "%s: failed to get buffer, %s\n", __func__, err_out);
234                 }
235         }
236         
237         /* rr->rectf is now filled with image data */
238
239         if ((scene->r.stamp & R_STAMP_ALL) && (scene->r.stamp & R_STAMP_DRAW))
240                 BKE_stamp_buf(scene, camera, NULL, rr->rectf, rr->rectx, rr->recty, 4);
241
242         /* note on color management:
243          *
244          * OpenGL renders into sRGB colors, but render buffers are expected to be
245          * linear So we convert to linear here, so the conversion back to bytes can make it
246          * sRGB (or other display space) again, and so that e.g. openexr saving also saves the
247          * correct linear float buffer.
248          */
249
250         IMB_buffer_float_from_float(rr->rectf, rr->rectf,
251                                     4, IB_PROFILE_LINEAR_RGB, IB_PROFILE_SRGB, FALSE,
252                                     oglrender->sizex, oglrender->sizey, oglrender->sizex, oglrender->sizex);
253
254         RE_ReleaseResult(oglrender->re);
255
256         /* update byte from float buffer */
257         ibuf = BKE_image_acquire_ibuf(oglrender->ima, &oglrender->iuser, &lock);
258
259         if (ibuf) {
260                 image_buffer_rect_update(scene, rr, ibuf, NULL);
261
262                 if (oglrender->write_still) {
263                         char name[FILE_MAX];
264                         int ok;
265
266                         if (scene->r.im_format.planes == R_IMF_CHAN_DEPTH_8) {
267                                 IMB_color_to_bw(ibuf);
268                         }
269
270                         BKE_makepicstring(name, scene->r.pic, oglrender->bmain->name, scene->r.cfra, scene->r.im_format.imtype, scene->r.scemode & R_EXTENSION, FALSE);
271                         ok = BKE_imbuf_write_as(ibuf, name, &scene->r.im_format, TRUE); /* no need to stamp here */
272                         if (ok) printf("OpenGL Render written to '%s'\n", name);
273                         else printf("OpenGL Render failed to write '%s'\n", name);
274                 }
275         }
276         
277         BKE_image_release_ibuf(oglrender->ima, lock);
278 }
279
280 static int screen_opengl_render_init(bContext *C, wmOperator *op)
281 {
282         /* new render clears all callbacks */
283         Scene *scene = CTX_data_scene(C);
284         ScrArea *prevsa = CTX_wm_area(C);
285         ARegion *prevar = CTX_wm_region(C);
286         RenderResult *rr;
287         GPUOffScreen *ofs;
288         OGLRender *oglrender;
289         int sizex, sizey;
290         short is_view_context = RNA_boolean_get(op->ptr, "view_context");
291         const short is_animation = RNA_boolean_get(op->ptr, "animation");
292         const short is_sequencer = RNA_boolean_get(op->ptr, "sequencer");
293         const short is_write_still = RNA_boolean_get(op->ptr, "write_still");
294         char err_out[256] = "unknown";
295
296         if (G.background) {
297                 BKE_report(op->reports, RPT_ERROR, "Can't use OpenGL render in background mode (no opengl context)");
298                 return 0;
299         }
300
301         /* ensure we have a 3d view */
302
303         if (!ED_view3d_context_activate(C)) {
304                 RNA_boolean_set(op->ptr, "view_context", FALSE);
305                 is_view_context = 0;
306         }
307
308         /* only one render job at a time */
309         if (WM_jobs_test(CTX_wm_manager(C), scene, WM_JOB_TYPE_RENDER))
310                 return 0;
311         
312         if (!is_view_context && scene->camera == NULL) {
313                 BKE_report(op->reports, RPT_ERROR, "Scene has no camera");
314                 return 0;
315         }
316
317         if (!is_animation && is_write_still && BKE_imtype_is_movie(scene->r.im_format.imtype)) {
318                 BKE_report(op->reports, RPT_ERROR, "Can't write a single file with an animation format selected");
319                 return 0;
320         }
321
322         /* stop all running jobs, currently previews frustrate Render */
323         WM_jobs_stop_all(CTX_wm_manager(C));
324
325         /* create offscreen buffer */
326         sizex = (scene->r.size * scene->r.xsch) / 100;
327         sizey = (scene->r.size * scene->r.ysch) / 100;
328
329         /* corrects render size with actual size, not every card supports non-power-of-two dimensions */
330         ofs = GPU_offscreen_create(sizex, sizey, err_out);
331
332         if (!ofs) {
333                 BKE_reportf(op->reports, RPT_ERROR, "Failed to create OpenGL offscreen buffer, %s", err_out);
334                 return 0;
335         }
336
337         /* handle UI stuff */
338         WM_cursor_wait(1);
339
340         /* allocate opengl render */
341         oglrender = MEM_callocN(sizeof(OGLRender), "OGLRender");
342         op->customdata = oglrender;
343
344         oglrender->ofs = ofs;
345         oglrender->sizex = sizex;
346         oglrender->sizey = sizey;
347         oglrender->bmain = CTX_data_main(C);
348         oglrender->scene = scene;
349         oglrender->cfrao = scene->r.cfra;
350
351         oglrender->write_still = is_write_still && !is_animation;
352
353         oglrender->is_sequencer = is_sequencer;
354         if (is_sequencer) {
355                 oglrender->sseq = CTX_wm_space_seq(C);
356         }
357
358
359         oglrender->obcenter_dia_back = U.obcenter_dia;
360         U.obcenter_dia = 0;
361
362         oglrender->prevsa = prevsa;
363         oglrender->prevar = prevar;
364
365         if (is_view_context) {
366                 ED_view3d_context_user_region(C, &oglrender->v3d, &oglrender->ar); /* so quad view renders camera */
367                 oglrender->rv3d = oglrender->ar->regiondata;
368
369                 /* MUST be cleared on exit */
370                 oglrender->scene->customdata_mask_modal = (ED_view3d_datamask(oglrender->scene, oglrender->v3d) |
371                                                            ED_view3d_object_datamask(oglrender->scene));
372
373                 /* apply immediately in case we're rendering from a script,
374                  * running notifiers again will overwrite */
375                 oglrender->scene->customdata_mask |= oglrender->scene->customdata_mask_modal;
376
377         }
378
379         /* create render */
380         oglrender->re = RE_NewRender(scene->id.name);
381
382         /* create image and image user */
383         oglrender->ima = BKE_image_verify_viewer(IMA_TYPE_R_RESULT, "Render Result");
384         BKE_image_signal(oglrender->ima, NULL, IMA_SIGNAL_FREE);
385         BKE_image_backup_render(oglrender->scene, oglrender->ima);
386
387         oglrender->iuser.scene = scene;
388         oglrender->iuser.ok = 1;
389
390         /* create render result */
391         RE_InitState(oglrender->re, NULL, &scene->r, NULL, sizex, sizey, NULL);
392
393         rr = RE_AcquireResultWrite(oglrender->re);
394         if (rr->rectf == NULL)
395                 rr->rectf = MEM_callocN(sizeof(float) * 4 * sizex * sizey, "screen_opengl_render_init rect");
396         RE_ReleaseResult(oglrender->re);
397
398         return 1;
399 }
400
401 static void screen_opengl_render_end(bContext *C, OGLRender *oglrender)
402 {
403         Main *bmain = CTX_data_main(C);
404         Scene *scene = oglrender->scene;
405
406         if (oglrender->mh) {
407                 if (BKE_imtype_is_movie(scene->r.im_format.imtype))
408                         oglrender->mh->end_movie();
409         }
410
411         if (oglrender->timer) { /* exec will not have a timer */
412                 scene->r.cfra = oglrender->cfrao;
413                 BKE_scene_update_for_newframe(bmain, scene, screen_opengl_layers(oglrender));
414
415                 WM_event_remove_timer(CTX_wm_manager(C), CTX_wm_window(C), oglrender->timer);
416         }
417
418         WM_cursor_wait(0);
419         WM_event_add_notifier(C, NC_SCENE | ND_RENDER_RESULT, oglrender->scene);
420
421         U.obcenter_dia = oglrender->obcenter_dia_back;
422
423         GPU_offscreen_free(oglrender->ofs);
424
425         oglrender->scene->customdata_mask_modal = 0;
426
427         CTX_wm_area_set(C, oglrender->prevsa);
428         CTX_wm_region_set(C, oglrender->prevar);
429
430         MEM_freeN(oglrender);
431 }
432
433 static int screen_opengl_render_cancel(bContext *C, wmOperator *op)
434 {
435         screen_opengl_render_end(C, op->customdata);
436
437         return OPERATOR_CANCELLED;
438 }
439
440 /* share between invoke and exec */
441 static int screen_opengl_render_anim_initialize(bContext *C, wmOperator *op)
442 {
443         /* initialize animation */
444         OGLRender *oglrender;
445         Scene *scene;
446
447         oglrender = op->customdata;
448         scene = oglrender->scene;
449
450         oglrender->reports = op->reports;
451         oglrender->mh = BKE_movie_handle_get(scene->r.im_format.imtype);
452         if (BKE_imtype_is_movie(scene->r.im_format.imtype)) {
453                 if (!oglrender->mh->start_movie(scene, &scene->r, oglrender->sizex, oglrender->sizey, oglrender->reports)) {
454                         screen_opengl_render_end(C, oglrender);
455                         return 0;
456                 }
457         }
458
459         oglrender->cfrao = scene->r.cfra;
460         oglrender->nfra = PSFRA;
461         scene->r.cfra = PSFRA;
462
463         return 1;
464 }
465 static int screen_opengl_render_anim_step(bContext *C, wmOperator *op)
466 {
467         Main *bmain = CTX_data_main(C);
468         OGLRender *oglrender = op->customdata;
469         Scene *scene = oglrender->scene;
470         ImBuf *ibuf;
471         void *lock;
472         char name[FILE_MAX];
473         int ok = 0;
474         const short view_context = (oglrender->v3d != NULL);
475         Object *camera = NULL;
476         int is_movie;
477
478         /* go to next frame */
479         if (CFRA < oglrender->nfra)
480                 CFRA++;
481         while (CFRA < oglrender->nfra) {
482                 unsigned int lay = screen_opengl_layers(oglrender);
483
484                 if (lay & 0xFF000000)
485                         lay &= 0xFF000000;
486
487                 BKE_scene_update_for_newframe(bmain, scene, lay);
488                 CFRA++;
489         }
490
491         is_movie = BKE_imtype_is_movie(scene->r.im_format.imtype);
492
493         if (!is_movie) {
494                 BKE_makepicstring(name, scene->r.pic, oglrender->bmain->name, scene->r.cfra, scene->r.im_format.imtype, scene->r.scemode & R_EXTENSION, TRUE);
495
496                 if ((scene->r.mode & R_NO_OVERWRITE) && BLI_exists(name)) {
497                         printf("skipping existing frame \"%s\"\n", name);
498
499                         /* go to next frame */
500                         oglrender->nfra += scene->r.frame_step;
501
502                         return 1;
503                 }
504         }
505
506         /* update animated image textures for gpu, etc,
507          * call before BKE_scene_update_for_newframe so modifiers with textures don't lag 1 frame */
508         ED_image_update_frame(bmain, CFRA);
509
510         BKE_scene_update_for_newframe(bmain, scene, screen_opengl_layers(oglrender));
511
512         if (view_context) {
513                 if (oglrender->rv3d->persp == RV3D_CAMOB && oglrender->v3d->camera && oglrender->v3d->scenelock) {
514                         /* since BKE_scene_update_for_newframe() is used rather
515                          * then ED_update_for_newframe() the camera needs to be set */
516                         if (BKE_scene_camera_switch_update(scene)) {
517                                 oglrender->v3d->camera = scene->camera;
518                         }
519
520                         camera = oglrender->v3d->camera;
521                 }
522         }
523         else {
524                 BKE_scene_camera_switch_update(scene);
525
526                 camera = scene->camera;
527         }
528
529         /* render into offscreen buffer */
530         screen_opengl_render_apply(oglrender);
531
532         /* save to disk */
533         ibuf = BKE_image_acquire_ibuf(oglrender->ima, &oglrender->iuser, &lock);
534
535         if (ibuf) {
536                 int needs_free = FALSE;
537
538                 if (is_movie || !BKE_imtype_supports_float(scene->r.im_format.imtype)) {
539                         ImBuf *colormanage_ibuf = IMB_dupImBuf(ibuf);
540
541                         IMB_display_buffer_to_imbuf_rect(colormanage_ibuf, &scene->view_settings, &scene->display_settings);
542                         imb_freerectfloatImBuf(colormanage_ibuf);
543
544                         // IMB_freeImBuf(ibuf); /* owned by the image */
545                         ibuf = colormanage_ibuf;
546                         needs_free = TRUE;
547                 }
548
549                 /* color -> grayscale */
550                 /* editing directly would alter the render view */
551                 if (scene->r.im_format.planes == R_IMF_PLANES_BW) {
552                          ImBuf *ibuf_bw = IMB_dupImBuf(ibuf);
553                         IMB_color_to_bw(ibuf_bw);
554
555                         if (needs_free)
556                                 IMB_freeImBuf(ibuf);
557
558                         ibuf = ibuf_bw;
559                 }
560                 else {
561                         /* this is lightweight & doesnt re-alloc the buffers, only do this
562                          * to save the correct bit depth since the image is always RGBA */
563                         ImBuf *ibuf_cpy = IMB_allocImBuf(ibuf->x, ibuf->y, scene->r.im_format.planes, 0);
564                         ibuf_cpy->rect = ibuf->rect;
565                         ibuf_cpy->rect_float = ibuf->rect_float;
566                         ibuf_cpy->zbuf_float = ibuf->zbuf_float;
567
568                         if (needs_free) {
569                                 ibuf_cpy->mall = ibuf->mall;
570                                 ibuf->mall = 0;
571                                 IMB_freeImBuf(ibuf);
572                         }
573
574                         ibuf = ibuf_cpy;
575                 }
576
577                 if (is_movie) {
578                         ok = oglrender->mh->append_movie(&scene->r, SFRA, CFRA, (int *)ibuf->rect,
579                                                          oglrender->sizex, oglrender->sizey, oglrender->reports);
580                         if (ok) {
581                                 printf("Append frame %d", scene->r.cfra);
582                                 BKE_reportf(op->reports, RPT_INFO, "Appended frame: %d", scene->r.cfra);
583                         }
584                 }
585                 else {
586                         ok = BKE_imbuf_write_stamp(scene, camera, ibuf, name, &scene->r.im_format);
587
588                         if (ok == 0) {
589                                 printf("Write error: cannot save %s\n", name);
590                                 BKE_reportf(op->reports, RPT_ERROR, "Write error: cannot save %s", name);
591                         }
592                         else {
593                                 printf("Saved: %s", name);
594                                 BKE_reportf(op->reports, RPT_INFO, "Saved file: %s", name);
595                         }
596                 }
597
598                 /* imbuf knows which rects are not part of ibuf */
599                 IMB_freeImBuf(ibuf);
600         }
601
602         BKE_image_release_ibuf(oglrender->ima, lock);
603
604         /* movie stats prints have no line break */
605         printf("\n");
606
607         /* go to next frame */
608         oglrender->nfra += scene->r.frame_step;
609
610         /* stop at the end or on error */
611         if (CFRA >= PEFRA || !ok) {
612                 screen_opengl_render_end(C, op->customdata);
613                 return 0;
614         }
615
616         return 1;
617 }
618
619
620 static int screen_opengl_render_modal(bContext *C, wmOperator *op, wmEvent *event)
621 {
622         OGLRender *oglrender = op->customdata;
623         int anim = RNA_boolean_get(op->ptr, "animation");
624         int ret;
625
626         switch (event->type) {
627                 case ESCKEY:
628                         /* cancel */
629                         screen_opengl_render_end(C, op->customdata);
630                         return OPERATOR_FINISHED;
631                 case TIMER:
632                         /* render frame? */
633                         if (oglrender->timer == event->customdata)
634                                 break;
635                 default:
636                         /* nothing to do */
637                         return OPERATOR_RUNNING_MODAL;
638         }
639
640         /* run first because screen_opengl_render_anim_step can free oglrender */
641         WM_event_add_notifier(C, NC_SCENE | ND_RENDER_RESULT, oglrender->scene);
642         
643         if (anim == 0) {
644                 screen_opengl_render_apply(op->customdata);
645                 screen_opengl_render_end(C, op->customdata);
646                 return OPERATOR_FINISHED;
647         }
648         else
649                 ret = screen_opengl_render_anim_step(C, op);
650
651         /* stop at the end or on error */
652         if (ret == 0) {
653                 return OPERATOR_FINISHED;
654         }
655
656         return OPERATOR_RUNNING_MODAL;
657 }
658
659 static int screen_opengl_render_invoke(bContext *C, wmOperator *op, wmEvent *event)
660 {
661         OGLRender *oglrender;
662         int anim = RNA_boolean_get(op->ptr, "animation");
663
664         if (!screen_opengl_render_init(C, op))
665                 return OPERATOR_CANCELLED;
666
667         if (anim) {
668                 if (!screen_opengl_render_anim_initialize(C, op))
669                         return OPERATOR_CANCELLED;
670         }
671         
672         oglrender = op->customdata;
673         render_view_open(C, event->x, event->y);
674         
675         WM_event_add_modal_handler(C, op);
676         oglrender->timer = WM_event_add_timer(CTX_wm_manager(C), CTX_wm_window(C), TIMER, 0.01f);
677         
678         return OPERATOR_RUNNING_MODAL;
679 }
680
681 /* executes blocking render */
682 static int screen_opengl_render_exec(bContext *C, wmOperator *op)
683 {
684         const short is_animation = RNA_boolean_get(op->ptr, "animation");
685
686         if (!screen_opengl_render_init(C, op))
687                 return OPERATOR_CANCELLED;
688
689         if (!is_animation) { /* same as invoke */
690                 /* render image */
691                 screen_opengl_render_apply(op->customdata);
692                 screen_opengl_render_end(C, op->customdata);
693
694                 return OPERATOR_FINISHED;
695         }
696         else {
697                 int ret = 1;
698
699                 if (!screen_opengl_render_anim_initialize(C, op))
700                         return OPERATOR_CANCELLED;
701
702                 while (ret) {
703                         ret = screen_opengl_render_anim_step(C, op);
704                 }
705         }
706
707         // no redraw needed, we leave state as we entered it
708 //      ED_update_for_newframe(C, 1);
709         WM_event_add_notifier(C, NC_SCENE | ND_RENDER_RESULT, CTX_data_scene(C));
710
711         return OPERATOR_FINISHED;
712 }
713
714 void RENDER_OT_opengl(wmOperatorType *ot)
715 {
716         PropertyRNA *prop;
717
718         /* identifiers */
719         ot->name = "OpenGL Render";
720         ot->description = "OpenGL render active viewport";
721         ot->idname = "RENDER_OT_opengl";
722
723         /* api callbacks */
724         ot->invoke = screen_opengl_render_invoke;
725         ot->exec = screen_opengl_render_exec; /* blocking */
726         ot->modal = screen_opengl_render_modal;
727         ot->cancel = screen_opengl_render_cancel;
728
729         ot->poll = ED_operator_screenactive;
730
731         prop = RNA_def_boolean(ot->srna, "animation", 0, "Animation", "Render files from the animation range of this scene");
732         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
733         prop = RNA_def_boolean(ot->srna, "sequencer", 0, "Sequencer", "Render using the sequencer's OpenGL display");
734         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
735         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)");
736         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
737         prop = RNA_def_boolean(ot->srna, "view_context", 1, "View Context", "Use the current 3D view for rendering, else use scene settings");
738         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
739
740 }
741
742 /* function for getting an opengl buffer from a View3D, used by sequencer */
743 // extern void *sequencer_view3d_cb;