43fb41a5e3d28fd389e159f2045c7c31dade19c2
[blender.git] / source / blender / editors / render / render_opengl.c
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2008 Blender Foundation.
21  * All rights reserved.
22  *
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/editors/render/render_opengl.c
28  *  \ingroup edrend
29  */
30
31
32 #include <math.h>
33 #include <string.h>
34 #include <stddef.h>
35
36 #include <GL/glew.h>
37
38 #include "MEM_guardedalloc.h"
39
40 #include "BLI_math.h"
41 #include "BLI_blenlib.h"
42 #include "BLI_editVert.h"
43 #include "BLI_dlrbTree.h"
44 #include "BLI_utildefines.h"
45
46 #include "DNA_scene_types.h"
47 #include "DNA_object_types.h"
48
49 #include "BKE_context.h"
50 #include "BKE_image.h"
51 #include "BKE_main.h"
52 #include "BKE_report.h"
53 #include "BKE_scene.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
67 #include "RNA_access.h"
68 #include "RNA_define.h"
69
70
71 #include "GPU_extensions.h"
72
73 #include "wm_window.h"
74
75 #include "render_intern.h"
76
77 typedef struct OGLRender {
78         Render *re;
79         Scene *scene;
80
81         View3D *v3d;
82         RegionView3D *rv3d;
83         ARegion *ar;
84
85         Image *ima;
86         ImageUser iuser;
87
88         GPUOffScreen *ofs;
89         int sizex, sizey;
90         int write_still;
91
92         ReportList *reports;
93         bMovieHandle *mh;
94         int cfrao, nfra;
95
96         wmTimer *timer; /* use to check if running modal or not (invoke'd or exec'd)*/
97 } OGLRender;
98
99 /* added because v3d is not always valid */
100 static unsigned int screen_opengl_layers(OGLRender *oglrender)
101 {
102         if(oglrender->v3d) {
103                 return oglrender->scene->lay | oglrender->v3d->lay;
104         }
105         else {
106                 return oglrender->scene->lay;
107         }
108 }
109
110 static void screen_opengl_render_apply(OGLRender *oglrender)
111 {
112         Scene *scene= oglrender->scene;
113         ARegion *ar= oglrender->ar;
114         View3D *v3d= oglrender->v3d;
115         RegionView3D *rv3d= oglrender->rv3d;
116         RenderResult *rr;
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         /* note on color management:
127          * looked into how best to deal with color management here and found heres how it should work.
128          *
129          * OpenGL materials etc are color corrected, so a float buffer from the graphics card is
130          * color corrected, without running any conversion functions.
131          * 
132          * With color correction disabled blender expects the rr->rectf to be non-color managed so
133          * just do a direct copy from the byte array to the rectf with no conversion too.
134          * notice IMB_float_from_rect has the profile set so no conversion is done.
135          */
136
137         if(view_context) {
138                 GPU_offscreen_bind(oglrender->ofs); /* bind */
139
140                 /* render 3d view */
141                 if(rv3d->persp==RV3D_CAMOB && v3d->camera) {
142                         /*int is_ortho= scene->r.mode & R_ORTHO;*/
143                         RE_GetCameraWindow(oglrender->re, v3d->camera, scene->r.cfra, winmat);
144                         
145                 }
146                 else {
147                         rctf viewplane;
148                         float clipsta, clipend;
149
150                         int is_ortho= get_view3d_viewplane(v3d, rv3d, sizex, sizey, &viewplane, &clipsta, &clipend, NULL);
151                         if(is_ortho) orthographic_m4(winmat, viewplane.xmin, viewplane.xmax, viewplane.ymin, viewplane.ymax, -clipend, clipend);
152                         else  perspective_m4(winmat, viewplane.xmin, viewplane.xmax, viewplane.ymin, viewplane.ymax, clipsta, clipend);
153                 }
154
155                 if((scene->r.mode & R_OSA) == 0) { 
156                         ED_view3d_draw_offscreen(scene, v3d, ar, sizex, sizey, NULL, winmat);
157                         glReadPixels(0, 0, sizex, sizey, GL_RGBA, GL_FLOAT, rr->rectf);
158                 }
159                 else {
160                         /* simple accumulation, less hassle then FSAA FBO's */
161 #                       define SAMPLES 5 /* fixed, easy to have more but for now this is ok */
162                         const float jit_ofs[SAMPLES][2] = {{0, 0}, {0.5f, 0.5f}, {-0.5f,-0.5f}, {-0.5f, 0.5f}, {0.5f, -0.5f}};
163                         float winmat_jitter[4][4];
164                         float *accum_buffer= MEM_mallocN(sizex * sizey * sizeof(float) * 4, "accum1");
165                         float *accum_tmp= MEM_mallocN(sizex * sizey * sizeof(float) * 4, "accum2");
166                         int j;
167
168                         /* first sample buffer, also initializes 'rv3d->persmat' */
169                         ED_view3d_draw_offscreen(scene, v3d, ar, sizex, sizey, NULL, winmat);
170                         glReadPixels(0, 0, sizex, sizey, GL_RGBA, GL_FLOAT, accum_buffer);
171
172                         /* skip the first sample */
173                         for(j=1; j < SAMPLES; j++) {
174                                 copy_m4_m4(winmat_jitter, winmat);
175                                 window_translate_m4(winmat_jitter, rv3d->persmat, jit_ofs[j][0] / sizex, jit_ofs[j][1] / sizey);
176
177                                 ED_view3d_draw_offscreen(scene, v3d, ar, sizex, sizey, NULL, winmat_jitter);
178                                 glReadPixels(0, 0, sizex, sizey, GL_RGBA, GL_FLOAT, accum_tmp);
179                                 add_vn_vn(accum_buffer, accum_tmp, sizex*sizey*sizeof(float));
180                         }
181
182                         mul_vn_vn_fl(rr->rectf, accum_buffer, sizex*sizey*sizeof(float), 1.0/SAMPLES);
183
184                         MEM_freeN(accum_buffer);
185                         MEM_freeN(accum_tmp);
186                 }
187
188                 GPU_offscreen_unbind(oglrender->ofs); /* unbind */
189         }
190         else {
191                 /* shouldnt suddenly give errors mid-render but possible */
192                 char err_out[256]= "unknown";
193                 ImBuf *ibuf_view= ED_view3d_draw_offscreen_imbuf_simple(scene, oglrender->sizex, oglrender->sizey, IB_rectfloat, OB_SOLID, err_out);
194
195                 if(ibuf_view) {
196                         memcpy(rr->rectf, ibuf_view->rect_float, sizeof(float) * 4 * oglrender->sizex * oglrender->sizey);
197                         IMB_freeImBuf(ibuf_view);
198                 }
199                 else {
200                         fprintf(stderr, "screen_opengl_render_apply: failed to get buffer, %s\n", err_out);
201                 }
202         }
203         
204         /* rr->rectf is now filled with image data */
205
206         if((scene->r.stamp & R_STAMP_ALL) && (scene->r.stamp & R_STAMP_DRAW))
207                 BKE_stamp_buf(scene, NULL, rr->rectf, rr->rectx, rr->recty, 4);
208
209         RE_ReleaseResult(oglrender->re);
210
211         /* update byte from float buffer */
212         ibuf= BKE_image_acquire_ibuf(oglrender->ima, &oglrender->iuser, &lock);
213
214         if(ibuf) {
215                 image_buffer_rect_update(NULL, rr, ibuf, NULL);
216
217                 if(oglrender->write_still) {
218                         char name[FILE_MAX];
219                         int ok;
220                         BKE_makepicstring(name, scene->r.pic, scene->r.cfra, scene->r.imtype, scene->r.scemode & R_EXTENSION, FALSE);
221                         ok= BKE_write_ibuf(scene, ibuf, name, scene->r.imtype, scene->r.subimtype, scene->r.quality);
222                         if(ok)  printf("OpenGL Render written to '%s'\n", name);
223                         else    printf("OpenGL Render failed to write '%s'\n", name);
224                 }
225         }
226         
227         BKE_image_release_ibuf(oglrender->ima, lock);
228 }
229
230 static int screen_opengl_render_init(bContext *C, wmOperator *op)
231 {
232         /* new render clears all callbacks */
233         Scene *scene= CTX_data_scene(C);
234         RenderResult *rr;
235         GPUOffScreen *ofs;
236         OGLRender *oglrender;
237         int sizex, sizey;
238         short is_view_context= RNA_boolean_get(op->ptr, "view_context");
239         const short is_animation= RNA_boolean_get(op->ptr, "animation");
240         const short is_write_still= RNA_boolean_get(op->ptr, "write_still");
241         char err_out[256]= "unknown";
242
243         /* ensure we have a 3d view */
244
245         if(!ED_view3d_context_activate(C)) {
246                 RNA_boolean_set(op->ptr, "view_context", 0);
247                 is_view_context= 0;
248         }
249
250         /* only one render job at a time */
251         if(WM_jobs_test(CTX_wm_manager(C), scene))
252                 return 0;
253         
254         if(!is_view_context && scene->camera==NULL) {
255                 BKE_report(op->reports, RPT_ERROR, "Scene has no camera.");
256                 return 0;
257         }
258
259         if(!is_animation && is_write_still && BKE_imtype_is_movie(scene->r.imtype)) {
260                 BKE_report(op->reports, RPT_ERROR, "Can't write a single file with an animation format selected.");
261                 return 0;
262         }
263
264         /* stop all running jobs, currently previews frustrate Render */
265         WM_jobs_stop_all(CTX_wm_manager(C));
266
267         /* handle UI stuff */
268         WM_cursor_wait(1);
269
270         /* create offscreen buffer */
271         sizex= (scene->r.size*scene->r.xsch)/100;
272         sizey= (scene->r.size*scene->r.ysch)/100;
273
274         /* corrects render size with actual size, not every card supports non-power-of-two dimensions */
275         ofs= GPU_offscreen_create(&sizex, &sizey, err_out);
276
277         if(!ofs) {
278                 BKE_reportf(op->reports, RPT_ERROR, "Failed to create OpenGL offscreen buffer, %s", err_out);
279                 return 0;
280         }
281
282         /* allocate opengl render */
283         oglrender= MEM_callocN(sizeof(OGLRender), "OGLRender");
284         op->customdata= oglrender;
285
286         oglrender->ofs= ofs;
287         oglrender->sizex= sizex;
288         oglrender->sizey= sizey;
289         oglrender->scene= scene;
290
291         oglrender->write_still= is_write_still && !is_animation;
292
293         if(is_view_context) {
294                 oglrender->v3d= CTX_wm_view3d(C);
295                 oglrender->ar= CTX_wm_region(C);
296                 oglrender->rv3d= CTX_wm_region_view3d(C);
297         }
298
299         /* create image and image user */
300         oglrender->ima= BKE_image_verify_viewer(IMA_TYPE_R_RESULT, "Render Result");
301         BKE_image_signal(oglrender->ima, NULL, IMA_SIGNAL_FREE);
302
303         oglrender->iuser.scene= scene;
304         oglrender->iuser.ok= 1;
305
306         /* create render and render result */
307         oglrender->re= RE_NewRender(scene->id.name);
308         RE_InitState(oglrender->re, NULL, &scene->r, NULL, sizex, sizey, NULL);
309
310         rr= RE_AcquireResultWrite(oglrender->re);
311         if(rr->rectf==NULL)
312                 rr->rectf= MEM_callocN(sizeof(float)*4*sizex*sizey, "screen_opengl_render_init rect");
313         RE_ReleaseResult(oglrender->re);
314
315         return 1;
316 }
317
318 static void screen_opengl_render_end(bContext *C, OGLRender *oglrender)
319 {
320         Main *bmain= CTX_data_main(C);
321         Scene *scene= oglrender->scene;
322
323         if(oglrender->mh) {
324                 if(BKE_imtype_is_movie(scene->r.imtype))
325                         oglrender->mh->end_movie();
326         }
327
328         if(oglrender->timer) { /* exec will not have a timer */
329                 scene->r.cfra= oglrender->cfrao;
330                 scene_update_for_newframe(bmain, scene, screen_opengl_layers(oglrender));
331
332                 WM_event_remove_timer(CTX_wm_manager(C), CTX_wm_window(C), oglrender->timer);
333         }
334
335         WM_cursor_wait(0);
336         WM_event_add_notifier(C, NC_SCENE|ND_RENDER_RESULT, oglrender->scene);
337
338         GPU_offscreen_free(oglrender->ofs);
339
340         MEM_freeN(oglrender);
341 }
342
343 static int screen_opengl_render_cancel(bContext *C, wmOperator *op)
344 {
345         screen_opengl_render_end(C, op->customdata);
346
347         return OPERATOR_CANCELLED;
348 }
349
350 /* share between invoke and exec */
351 static int screen_opengl_render_anim_initialize(bContext *C, wmOperator *op)
352 {
353         /* initialize animation */
354         OGLRender *oglrender;
355         Scene *scene;
356
357         oglrender= op->customdata;
358         scene= oglrender->scene;
359
360         oglrender->reports= op->reports;
361         oglrender->mh= BKE_get_movie_handle(scene->r.imtype);
362         if(BKE_imtype_is_movie(scene->r.imtype)) {
363                 if(!oglrender->mh->start_movie(scene, &scene->r, oglrender->sizex, oglrender->sizey, oglrender->reports)) {
364                         screen_opengl_render_end(C, oglrender);
365                         return 0;
366                 }
367         }
368
369         oglrender->cfrao= scene->r.cfra;
370         oglrender->nfra= PSFRA;
371         scene->r.cfra= PSFRA;
372
373         return 1;
374 }
375 static int screen_opengl_render_anim_step(bContext *C, wmOperator *op)
376 {
377         Main *bmain= CTX_data_main(C);
378         OGLRender *oglrender= op->customdata;
379         Scene *scene= oglrender->scene;
380         ImBuf *ibuf;
381         void *lock;
382         char name[FILE_MAXDIR+FILE_MAXFILE];
383         int ok= 0;
384         const short  view_context= (oglrender->v3d != NULL);
385
386         /* update animated image textures for gpu, etc,
387          * call before scene_update_for_newframe so modifiers with textuers dont lag 1 frame */
388         ED_image_update_frame(bmain, scene->r.cfra);
389
390         /* go to next frame */
391         while(CFRA<oglrender->nfra) {
392                 unsigned int lay= screen_opengl_layers(oglrender);
393
394                 if(lay & 0xFF000000)
395                         lay &= 0xFF000000;
396
397                 scene_update_for_newframe(bmain, scene, lay);
398                 CFRA++;
399         }
400
401         scene_update_for_newframe(bmain, scene, screen_opengl_layers(oglrender));
402
403         if(view_context) {
404                 if(oglrender->rv3d->persp==RV3D_CAMOB && oglrender->v3d->camera && oglrender->v3d->scenelock) {
405                         /* since scene_update_for_newframe() is used rather
406                          * then ED_update_for_newframe() the camera needs to be set */
407                         if(scene_camera_switch_update(scene))
408                                 oglrender->v3d->camera= scene->camera;
409                 }
410         }
411         else {
412                 scene_camera_switch_update(scene);
413         }
414
415         /* render into offscreen buffer */
416         screen_opengl_render_apply(oglrender);
417
418         /* save to disk */
419         ibuf= BKE_image_acquire_ibuf(oglrender->ima, &oglrender->iuser, &lock);
420
421         if(ibuf) {
422                 if(BKE_imtype_is_movie(scene->r.imtype)) {
423                         ok= oglrender->mh->append_movie(&scene->r, CFRA, (int*)ibuf->rect, oglrender->sizex, oglrender->sizey, oglrender->reports);
424                         if(ok) {
425                                 printf("Append frame %d", scene->r.cfra);
426                                 BKE_reportf(op->reports, RPT_INFO, "Appended frame: %d", scene->r.cfra);
427                         }
428                 }
429                 else {
430                         BKE_makepicstring(name, scene->r.pic, scene->r.cfra, scene->r.imtype, scene->r.scemode & R_EXTENSION, TRUE);
431                         ok= BKE_write_ibuf(scene, ibuf, name, scene->r.imtype, scene->r.subimtype, scene->r.quality);
432
433                         if(ok==0) {
434                                 printf("Write error: cannot save %s\n", name);
435                                 BKE_reportf(op->reports, RPT_ERROR, "Write error: cannot save %s", name);
436                         }
437                         else {
438                                 printf("Saved: %s", name);
439                                 BKE_reportf(op->reports, RPT_INFO, "Saved file: %s", name);
440                         }
441                 }
442         }
443
444         BKE_image_release_ibuf(oglrender->ima, lock);
445
446         /* movie stats prints have no line break */
447         printf("\n");
448
449         /* go to next frame */
450         oglrender->nfra += scene->r.frame_step;
451         scene->r.cfra++;
452
453         /* stop at the end or on error */
454         if(scene->r.cfra > PEFRA || !ok) {
455                 screen_opengl_render_end(C, op->customdata);
456                 return 0;
457         }
458
459         return 1;
460 }
461
462
463 static int screen_opengl_render_modal(bContext *C, wmOperator *op, wmEvent *event)
464 {
465         OGLRender *oglrender= op->customdata;
466         int anim= RNA_boolean_get(op->ptr, "animation");
467         int ret;
468
469         switch(event->type) {
470                 case ESCKEY:
471                         /* cancel */
472                         screen_opengl_render_end(C, op->customdata);
473                         return OPERATOR_FINISHED;
474                 case TIMER:
475                         /* render frame? */
476                         if(oglrender->timer == event->customdata)
477                                 break;
478                 default:
479                         /* nothing to do */
480                         return OPERATOR_RUNNING_MODAL;
481         }
482
483         /* run first because screen_opengl_render_anim_step can free oglrender */
484         WM_event_add_notifier(C, NC_SCENE|ND_RENDER_RESULT, oglrender->scene);
485         
486         if(anim == 0) {
487                 screen_opengl_render_apply(op->customdata);
488                 screen_opengl_render_end(C, op->customdata);
489                 return OPERATOR_FINISHED;
490         }
491         else
492                 ret= screen_opengl_render_anim_step(C, op);
493
494         /* stop at the end or on error */
495         if(ret == 0) {
496                 return OPERATOR_FINISHED;
497         }
498
499         return OPERATOR_RUNNING_MODAL;
500 }
501
502 static int screen_opengl_render_invoke(bContext *C, wmOperator *op, wmEvent *event)
503 {
504         OGLRender *oglrender;
505         int anim= RNA_boolean_get(op->ptr, "animation");
506
507         if(!screen_opengl_render_init(C, op))
508                 return OPERATOR_CANCELLED;
509
510         if(anim) {
511                 if(!screen_opengl_render_anim_initialize(C, op))
512                         return OPERATOR_CANCELLED;
513         }
514         
515         oglrender= op->customdata;
516         screen_set_image_output(C, event->x, event->y);
517         
518         WM_event_add_modal_handler(C, op);
519         oglrender->timer= WM_event_add_timer(CTX_wm_manager(C), CTX_wm_window(C), TIMER, 0.01f);
520         
521         return OPERATOR_RUNNING_MODAL;
522 }
523
524 /* executes blocking render */
525 static int screen_opengl_render_exec(bContext *C, wmOperator *op)
526 {
527         const short is_animation= RNA_boolean_get(op->ptr, "animation");
528
529         if(!screen_opengl_render_init(C, op))
530                 return OPERATOR_CANCELLED;
531
532         if(!is_animation) { /* same as invoke */
533                 /* render image */
534                 screen_opengl_render_apply(op->customdata);
535                 screen_opengl_render_end(C, op->customdata);
536
537                 return OPERATOR_FINISHED;
538         }
539         else {
540                 int ret= 1;
541
542                 if(!screen_opengl_render_anim_initialize(C, op))
543                         return OPERATOR_CANCELLED;
544
545                 while(ret) {
546                         ret= screen_opengl_render_anim_step(C, op);
547                 }
548         }
549
550         // no redraw needed, we leave state as we entered it
551 //      ED_update_for_newframe(C, 1);
552         WM_event_add_notifier(C, NC_SCENE|ND_RENDER_RESULT, CTX_data_scene(C));
553
554         return OPERATOR_FINISHED;
555 }
556
557 void RENDER_OT_opengl(wmOperatorType *ot)
558 {
559         /* identifiers */
560         ot->name= "OpenGL Render";
561         ot->description= "OpenGL render active viewport";
562         ot->idname= "RENDER_OT_opengl";
563
564         /* api callbacks */
565         ot->invoke= screen_opengl_render_invoke;
566         ot->exec= screen_opengl_render_exec; /* blocking */
567         ot->modal= screen_opengl_render_modal;
568         ot->cancel= screen_opengl_render_cancel;
569
570         ot->poll= ED_operator_screenactive;
571
572         RNA_def_boolean(ot->srna, "animation", 0, "Animation", "Render files from the animation range of this scene");
573         RNA_def_boolean(ot->srna, "write_still", 0, "Write Image", "Save rendered the image to the output path (used only when animation is disabled)");
574         RNA_def_boolean(ot->srna, "view_context", 1, "View Context", "Use the current 3D view for rendering, else use scene settings.");
575 }
576
577 /* function for getting an opengl buffer from a View3D, used by sequencer */
578 // extern void *sequencer_view3d_cb;