[#26482] Solidify / Animation Opengl render. no texture.
[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                 /* MUST be cleared on exit */
299                 oglrender->scene->customdata_mask_modal= ED_view3d_datamask(oglrender->scene, oglrender->v3d);
300         }
301
302         /* create image and image user */
303         oglrender->ima= BKE_image_verify_viewer(IMA_TYPE_R_RESULT, "Render Result");
304         BKE_image_signal(oglrender->ima, NULL, IMA_SIGNAL_FREE);
305
306         oglrender->iuser.scene= scene;
307         oglrender->iuser.ok= 1;
308
309         /* create render and render result */
310         oglrender->re= RE_NewRender(scene->id.name);
311         RE_InitState(oglrender->re, NULL, &scene->r, NULL, sizex, sizey, NULL);
312
313         rr= RE_AcquireResultWrite(oglrender->re);
314         if(rr->rectf==NULL)
315                 rr->rectf= MEM_callocN(sizeof(float)*4*sizex*sizey, "screen_opengl_render_init rect");
316         RE_ReleaseResult(oglrender->re);
317
318         return 1;
319 }
320
321 static void screen_opengl_render_end(bContext *C, OGLRender *oglrender)
322 {
323         Main *bmain= CTX_data_main(C);
324         Scene *scene= oglrender->scene;
325
326         if(oglrender->mh) {
327                 if(BKE_imtype_is_movie(scene->r.imtype))
328                         oglrender->mh->end_movie();
329         }
330
331         if(oglrender->timer) { /* exec will not have a timer */
332                 scene->r.cfra= oglrender->cfrao;
333                 scene_update_for_newframe(bmain, scene, screen_opengl_layers(oglrender));
334
335                 WM_event_remove_timer(CTX_wm_manager(C), CTX_wm_window(C), oglrender->timer);
336         }
337
338         WM_cursor_wait(0);
339         WM_event_add_notifier(C, NC_SCENE|ND_RENDER_RESULT, oglrender->scene);
340
341         GPU_offscreen_free(oglrender->ofs);
342
343         oglrender->scene->customdata_mask_modal= 0;
344
345         MEM_freeN(oglrender);
346 }
347
348 static int screen_opengl_render_cancel(bContext *C, wmOperator *op)
349 {
350         screen_opengl_render_end(C, op->customdata);
351
352         return OPERATOR_CANCELLED;
353 }
354
355 /* share between invoke and exec */
356 static int screen_opengl_render_anim_initialize(bContext *C, wmOperator *op)
357 {
358         /* initialize animation */
359         OGLRender *oglrender;
360         Scene *scene;
361
362         oglrender= op->customdata;
363         scene= oglrender->scene;
364
365         oglrender->reports= op->reports;
366         oglrender->mh= BKE_get_movie_handle(scene->r.imtype);
367         if(BKE_imtype_is_movie(scene->r.imtype)) {
368                 if(!oglrender->mh->start_movie(scene, &scene->r, oglrender->sizex, oglrender->sizey, oglrender->reports)) {
369                         screen_opengl_render_end(C, oglrender);
370                         return 0;
371                 }
372         }
373
374         oglrender->cfrao= scene->r.cfra;
375         oglrender->nfra= PSFRA;
376         scene->r.cfra= PSFRA;
377
378         return 1;
379 }
380 static int screen_opengl_render_anim_step(bContext *C, wmOperator *op)
381 {
382         Main *bmain= CTX_data_main(C);
383         OGLRender *oglrender= op->customdata;
384         Scene *scene= oglrender->scene;
385         ImBuf *ibuf;
386         void *lock;
387         char name[FILE_MAXDIR+FILE_MAXFILE];
388         int ok= 0;
389         const short  view_context= (oglrender->v3d != NULL);
390
391         /* update animated image textures for gpu, etc,
392          * call before scene_update_for_newframe so modifiers with textuers dont lag 1 frame */
393         ED_image_update_frame(bmain, scene->r.cfra);
394
395         /* go to next frame */
396         while(CFRA<oglrender->nfra) {
397                 unsigned int lay= screen_opengl_layers(oglrender);
398
399                 if(lay & 0xFF000000)
400                         lay &= 0xFF000000;
401
402                 scene_update_for_newframe(bmain, scene, lay);
403                 CFRA++;
404         }
405
406         scene_update_for_newframe(bmain, scene, screen_opengl_layers(oglrender));
407
408         if(view_context) {
409                 if(oglrender->rv3d->persp==RV3D_CAMOB && oglrender->v3d->camera && oglrender->v3d->scenelock) {
410                         /* since scene_update_for_newframe() is used rather
411                          * then ED_update_for_newframe() the camera needs to be set */
412                         if(scene_camera_switch_update(scene))
413                                 oglrender->v3d->camera= scene->camera;
414                 }
415         }
416         else {
417                 scene_camera_switch_update(scene);
418         }
419
420         /* render into offscreen buffer */
421         screen_opengl_render_apply(oglrender);
422
423         /* save to disk */
424         ibuf= BKE_image_acquire_ibuf(oglrender->ima, &oglrender->iuser, &lock);
425
426         if(ibuf) {
427                 if(BKE_imtype_is_movie(scene->r.imtype)) {
428                         ok= oglrender->mh->append_movie(&scene->r, CFRA, (int*)ibuf->rect, oglrender->sizex, oglrender->sizey, oglrender->reports);
429                         if(ok) {
430                                 printf("Append frame %d", scene->r.cfra);
431                                 BKE_reportf(op->reports, RPT_INFO, "Appended frame: %d", scene->r.cfra);
432                         }
433                 }
434                 else {
435                         BKE_makepicstring(name, scene->r.pic, scene->r.cfra, scene->r.imtype, scene->r.scemode & R_EXTENSION, TRUE);
436                         ok= BKE_write_ibuf(scene, ibuf, name, scene->r.imtype, scene->r.subimtype, scene->r.quality);
437
438                         if(ok==0) {
439                                 printf("Write error: cannot save %s\n", name);
440                                 BKE_reportf(op->reports, RPT_ERROR, "Write error: cannot save %s", name);
441                         }
442                         else {
443                                 printf("Saved: %s", name);
444                                 BKE_reportf(op->reports, RPT_INFO, "Saved file: %s", name);
445                         }
446                 }
447         }
448
449         BKE_image_release_ibuf(oglrender->ima, lock);
450
451         /* movie stats prints have no line break */
452         printf("\n");
453
454         /* go to next frame */
455         oglrender->nfra += scene->r.frame_step;
456         scene->r.cfra++;
457
458         /* stop at the end or on error */
459         if(scene->r.cfra > PEFRA || !ok) {
460                 screen_opengl_render_end(C, op->customdata);
461                 return 0;
462         }
463
464         return 1;
465 }
466
467
468 static int screen_opengl_render_modal(bContext *C, wmOperator *op, wmEvent *event)
469 {
470         OGLRender *oglrender= op->customdata;
471         int anim= RNA_boolean_get(op->ptr, "animation");
472         int ret;
473
474         switch(event->type) {
475                 case ESCKEY:
476                         /* cancel */
477                         screen_opengl_render_end(C, op->customdata);
478                         return OPERATOR_FINISHED;
479                 case TIMER:
480                         /* render frame? */
481                         if(oglrender->timer == event->customdata)
482                                 break;
483                 default:
484                         /* nothing to do */
485                         return OPERATOR_RUNNING_MODAL;
486         }
487
488         /* run first because screen_opengl_render_anim_step can free oglrender */
489         WM_event_add_notifier(C, NC_SCENE|ND_RENDER_RESULT, oglrender->scene);
490         
491         if(anim == 0) {
492                 screen_opengl_render_apply(op->customdata);
493                 screen_opengl_render_end(C, op->customdata);
494                 return OPERATOR_FINISHED;
495         }
496         else
497                 ret= screen_opengl_render_anim_step(C, op);
498
499         /* stop at the end or on error */
500         if(ret == 0) {
501                 return OPERATOR_FINISHED;
502         }
503
504         return OPERATOR_RUNNING_MODAL;
505 }
506
507 static int screen_opengl_render_invoke(bContext *C, wmOperator *op, wmEvent *event)
508 {
509         OGLRender *oglrender;
510         int anim= RNA_boolean_get(op->ptr, "animation");
511
512         if(!screen_opengl_render_init(C, op))
513                 return OPERATOR_CANCELLED;
514
515         if(anim) {
516                 if(!screen_opengl_render_anim_initialize(C, op))
517                         return OPERATOR_CANCELLED;
518         }
519         
520         oglrender= op->customdata;
521         screen_set_image_output(C, event->x, event->y);
522         
523         WM_event_add_modal_handler(C, op);
524         oglrender->timer= WM_event_add_timer(CTX_wm_manager(C), CTX_wm_window(C), TIMER, 0.01f);
525         
526         return OPERATOR_RUNNING_MODAL;
527 }
528
529 /* executes blocking render */
530 static int screen_opengl_render_exec(bContext *C, wmOperator *op)
531 {
532         const short is_animation= RNA_boolean_get(op->ptr, "animation");
533
534         if(!screen_opengl_render_init(C, op))
535                 return OPERATOR_CANCELLED;
536
537         if(!is_animation) { /* same as invoke */
538                 /* render image */
539                 screen_opengl_render_apply(op->customdata);
540                 screen_opengl_render_end(C, op->customdata);
541
542                 return OPERATOR_FINISHED;
543         }
544         else {
545                 int ret= 1;
546
547                 if(!screen_opengl_render_anim_initialize(C, op))
548                         return OPERATOR_CANCELLED;
549
550                 while(ret) {
551                         ret= screen_opengl_render_anim_step(C, op);
552                 }
553         }
554
555         // no redraw needed, we leave state as we entered it
556 //      ED_update_for_newframe(C, 1);
557         WM_event_add_notifier(C, NC_SCENE|ND_RENDER_RESULT, CTX_data_scene(C));
558
559         return OPERATOR_FINISHED;
560 }
561
562 void RENDER_OT_opengl(wmOperatorType *ot)
563 {
564         /* identifiers */
565         ot->name= "OpenGL Render";
566         ot->description= "OpenGL render active viewport";
567         ot->idname= "RENDER_OT_opengl";
568
569         /* api callbacks */
570         ot->invoke= screen_opengl_render_invoke;
571         ot->exec= screen_opengl_render_exec; /* blocking */
572         ot->modal= screen_opengl_render_modal;
573         ot->cancel= screen_opengl_render_cancel;
574
575         ot->poll= ED_operator_screenactive;
576
577         RNA_def_boolean(ot->srna, "animation", 0, "Animation", "Render files from the animation range of this scene");
578         RNA_def_boolean(ot->srna, "write_still", 0, "Write Image", "Save rendered the image to the output path (used only when animation is disabled)");
579         RNA_def_boolean(ot->srna, "view_context", 1, "View Context", "Use the current 3D view for rendering, else use scene settings.");
580 }
581
582 /* function for getting an opengl buffer from a View3D, used by sequencer */
583 // extern void *sequencer_view3d_cb;