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