524e36b3fc9ebbc4784b6d7617cb68500b846a18
[blender-staging.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 #include <math.h>
28 #include <string.h>
29 #include <stddef.h>
30
31 #include <GL/glew.h>
32
33 #include "MEM_guardedalloc.h"
34
35 #include "BLI_math.h"
36 #include "BLI_blenlib.h"
37 #include "BLI_editVert.h"
38 #include "BLI_dlrbTree.h"
39
40 #include "DNA_scene_types.h"
41 #include "DNA_object_types.h"
42
43 #include "BKE_blender.h"
44 #include "BKE_object.h"
45 #include "BKE_context.h"
46 #include "BKE_global.h"
47 #include "BKE_image.h"
48 #include "BKE_library.h"
49 #include "BKE_main.h"
50 #include "BKE_mesh.h"
51 #include "BKE_multires.h"
52 #include "BKE_report.h"
53 #include "BKE_scene.h"
54 #include "BKE_screen.h"
55 #include "BKE_utildefines.h"
56 #include "BKE_writeavi.h"
57
58 #include "WM_api.h"
59 #include "WM_types.h"
60
61 #include "ED_screen.h"
62 #include "ED_view3d.h"
63 #include "ED_image.h"
64
65 #include "RE_pipeline.h"
66 #include "IMB_imbuf_types.h"
67 #include "IMB_imbuf.h"
68
69 #include "RNA_access.h"
70 #include "RNA_define.h"
71
72
73 #include "GPU_extensions.h"
74
75 #include "wm_window.h"
76
77 #include "render_intern.h"
78
79 typedef struct OGLRender {
80         Render *re;
81         Scene *scene;
82
83         View3D *v3d;
84         RegionView3D *rv3d;
85         ARegion *ar;
86
87         Image *ima;
88         ImageUser iuser;
89
90         GPUOffScreen *ofs;
91         int sizex, sizey;
92
93         ReportList *reports;
94         bMovieHandle *mh;
95         int cfrao, nfra;
96
97         wmTimer *timer; /* use to check if running modal or not (invoke'd or exec'd)*/
98 } OGLRender;
99
100 /* added because v3d is not always valid */
101 static unsigned int screen_opengl_layers(OGLRender *oglrender)
102 {
103         if(oglrender->v3d) {
104                 return oglrender->scene->lay | oglrender->v3d->lay;
105         }
106         else {
107                 return oglrender->scene->lay;
108         }
109 }
110
111 static void screen_opengl_render_apply(OGLRender *oglrender)
112 {
113         Scene *scene= oglrender->scene;
114         ARegion *ar= oglrender->ar;
115         View3D *v3d= oglrender->v3d;
116         RegionView3D *rv3d= oglrender->rv3d;
117         RenderResult *rr;
118         ImBuf *ibuf;
119         void *lock;
120         float winmat[4][4];
121         int sizex= oglrender->sizex;
122         int sizey= oglrender->sizey;
123         int view_context = (v3d != NULL);
124
125         rr= RE_AcquireResultRead(oglrender->re);
126         
127         if(view_context) {
128                 GPU_offscreen_bind(oglrender->ofs); /* bind */
129
130                 /* render 3d view */
131                 if(rv3d->persp==RV3D_CAMOB && v3d->camera) {
132                         RE_GetCameraWindow(oglrender->re, v3d->camera, scene->r.cfra, winmat);
133                         ED_view3d_draw_offscreen(scene, v3d, ar, sizex, sizey, NULL, winmat);
134                 }
135                 else {
136                         ED_view3d_draw_offscreen(scene, v3d, ar, sizex, sizey, NULL, NULL);
137                 }
138         
139                 glReadPixels(0, 0, sizex, sizey, GL_RGBA, GL_FLOAT, rr->rectf);
140
141                 GPU_offscreen_unbind(oglrender->ofs); /* unbind */
142         }
143         else {
144                 ImBuf *ibuf_view= ED_view3d_draw_offscreen_imbuf_simple(scene, oglrender->sizex, oglrender->sizey, OB_SOLID);
145                 IMB_float_from_rect(ibuf_view);
146
147                 memcpy(rr->rectf, ibuf_view->rect_float, sizeof(float) * 4 * oglrender->sizex * oglrender->sizey);
148
149                 IMB_freeImBuf(ibuf_view);
150         }
151         
152         /* rr->rectf is now filled with image data */
153
154         if((scene->r.stamp & R_STAMP_ALL) && (scene->r.stamp & R_STAMP_DRAW))
155                 BKE_stamp_buf(scene, NULL, rr->rectf, rr->rectx, rr->recty, 4);
156
157         RE_ReleaseResult(oglrender->re);
158
159         /* update byte from float buffer */
160         ibuf= BKE_image_acquire_ibuf(oglrender->ima, &oglrender->iuser, &lock);
161         if(ibuf) image_buffer_rect_update(NULL, rr, ibuf, NULL);
162         BKE_image_release_ibuf(oglrender->ima, lock);
163 }
164
165 static int screen_opengl_render_init(bContext *C, wmOperator *op)
166 {
167         /* new render clears all callbacks */
168         Scene *scene= CTX_data_scene(C);
169         RenderResult *rr;
170         GPUOffScreen *ofs;
171         OGLRender *oglrender;
172         int sizex, sizey;
173         int view_context= RNA_boolean_get(op->ptr, "view_context");
174
175         /* ensure we have a 3d view */
176         
177         if(!ED_view3d_context_activate(C)) {
178                 RNA_boolean_set(op->ptr, "view_context", 0);
179                 view_context = 0;
180         }
181
182         /* only one render job at a time */
183         if(WM_jobs_test(CTX_wm_manager(C), scene))
184                 return 0;
185         
186         if(!view_context && scene->camera==NULL) {
187                 BKE_report(op->reports, RPT_ERROR, "Scene has no camera.");
188                 return 0;
189         }
190
191         /* stop all running jobs, currently previews frustrate Render */
192         WM_jobs_stop_all(CTX_wm_manager(C));
193
194         /* handle UI stuff */
195         WM_cursor_wait(1);
196
197         /* create offscreen buffer */
198         sizex= (scene->r.size*scene->r.xsch)/100;
199         sizey= (scene->r.size*scene->r.ysch)/100;
200
201         ofs= GPU_offscreen_create(sizex, sizey);
202
203         if(!ofs) {
204                 BKE_report(op->reports, RPT_ERROR, "Failed to create OpenGL offscreen buffer.");
205                 return 0;
206         }
207
208         /* allocate opengl render */
209         oglrender= MEM_callocN(sizeof(OGLRender), "OGLRender");
210         op->customdata= oglrender;
211
212         oglrender->ofs= ofs;
213         oglrender->sizex= sizex;
214         oglrender->sizey= sizey;
215         oglrender->scene= scene;
216
217         if(view_context) {
218                 oglrender->v3d= CTX_wm_view3d(C);
219                 oglrender->ar= CTX_wm_region(C);
220                 oglrender->rv3d= CTX_wm_region_view3d(C);
221         }
222
223         /* create image and image user */
224         oglrender->ima= BKE_image_verify_viewer(IMA_TYPE_R_RESULT, "Render Result");
225         BKE_image_signal(oglrender->ima, NULL, IMA_SIGNAL_FREE);
226
227         oglrender->iuser.scene= scene;
228         oglrender->iuser.ok= 1;
229
230         /* create render and render result */
231         oglrender->re= RE_NewRender(scene->id.name);
232         RE_InitState(oglrender->re, NULL, &scene->r, NULL, sizex, sizey, NULL);
233
234         rr= RE_AcquireResultWrite(oglrender->re);
235         if(rr->rectf==NULL)
236                 rr->rectf= MEM_callocN(sizeof(float)*4*sizex*sizey, "screen_opengl_render_init rect");
237         RE_ReleaseResult(oglrender->re);
238
239         return 1;
240 }
241
242 static void screen_opengl_render_end(bContext *C, OGLRender *oglrender)
243 {
244         Main *bmain= CTX_data_main(C);
245         Scene *scene= oglrender->scene;
246
247         if(oglrender->mh) {
248                 if(BKE_imtype_is_movie(scene->r.imtype))
249                         oglrender->mh->end_movie();
250         }
251
252         if(oglrender->timer) { /* exec will not have a timer */
253                 scene->r.cfra= oglrender->cfrao;
254                 scene_update_for_newframe(bmain, scene, screen_opengl_layers(oglrender));
255
256                 WM_event_remove_timer(CTX_wm_manager(C), CTX_wm_window(C), oglrender->timer);
257         }
258
259         WM_cursor_wait(0);
260         WM_event_add_notifier(C, NC_SCENE|ND_RENDER_RESULT, oglrender->scene);
261
262         GPU_offscreen_free(oglrender->ofs);
263
264         MEM_freeN(oglrender);
265 }
266
267 static int screen_opengl_render_cancel(bContext *C, wmOperator *op)
268 {
269         screen_opengl_render_end(C, op->customdata);
270
271         return OPERATOR_CANCELLED;
272 }
273
274 /* share between invoke and exec */
275 static int screen_opengl_render_anim_initialize(bContext *C, wmOperator *op)
276 {
277         /* initialize animation */
278         OGLRender *oglrender;
279         Scene *scene;
280
281         oglrender= op->customdata;
282         scene= oglrender->scene;
283
284         oglrender->reports= op->reports;
285         oglrender->mh= BKE_get_movie_handle(scene->r.imtype);
286         if(BKE_imtype_is_movie(scene->r.imtype)) {
287                 if(!oglrender->mh->start_movie(scene, &scene->r, oglrender->sizex, oglrender->sizey, oglrender->reports)) {
288                         screen_opengl_render_end(C, oglrender);
289                         return 0;
290                 }
291         }
292
293         oglrender->cfrao= scene->r.cfra;
294         oglrender->nfra= SFRA;
295         scene->r.cfra= SFRA;
296
297         return 1;
298 }
299 static int screen_opengl_render_anim_step(bContext *C, wmOperator *op)
300 {
301         Main *bmain= CTX_data_main(C);
302         OGLRender *oglrender= op->customdata;
303         Scene *scene= oglrender->scene;
304         ImBuf *ibuf;
305         void *lock;
306         char name[FILE_MAXDIR+FILE_MAXFILE];
307         int ok= 0;
308         int view_context = (oglrender->v3d != NULL);
309
310         /* update animated image textures for gpu, etc,
311          * call before scene_update_for_newframe so modifiers with textuers dont lag 1 frame */
312         ED_image_update_frame(C);
313
314         /* go to next frame */
315         while(CFRA<oglrender->nfra) {
316                 unsigned int lay= screen_opengl_layers(oglrender);
317
318                 if(lay & 0xFF000000)
319                         lay &= 0xFF000000;
320
321                 scene_update_for_newframe(bmain, scene, lay);
322                 CFRA++;
323         }
324
325         scene_update_for_newframe(bmain, scene, screen_opengl_layers(oglrender));
326
327         if(view_context) {
328                 if(oglrender->rv3d->persp==RV3D_CAMOB && oglrender->v3d->camera && oglrender->v3d->scenelock) {
329                         /* since scene_update_for_newframe() is used rather
330                          * then ED_update_for_newframe() the camera needs to be set */
331                         if(scene_camera_switch_update(scene))
332                                 oglrender->v3d->camera= scene->camera;
333                 }
334         }
335         else {
336                 scene_camera_switch_update(scene);
337         }
338
339         /* render into offscreen buffer */
340         screen_opengl_render_apply(oglrender);
341
342         /* save to disk */
343         ibuf= BKE_image_acquire_ibuf(oglrender->ima, &oglrender->iuser, &lock);
344
345         if(ibuf) {
346                 if(BKE_imtype_is_movie(scene->r.imtype)) {
347                         ok= oglrender->mh->append_movie(&scene->r, CFRA, (int*)ibuf->rect, oglrender->sizex, oglrender->sizey, oglrender->reports);
348                         if(ok) {
349                                 printf("Append frame %d", scene->r.cfra);
350                                 BKE_reportf(op->reports, RPT_INFO, "Appended frame: %d", scene->r.cfra);
351                         }
352                 }
353                 else {
354                         BKE_makepicstring(name, scene->r.pic, scene->r.cfra, scene->r.imtype, scene->r.scemode & R_EXTENSION);
355                         ok= BKE_write_ibuf(scene, ibuf, name, scene->r.imtype, scene->r.subimtype, scene->r.quality);
356
357                         if(ok==0) {
358                                 printf("Write error: cannot save %s\n", name);
359                                 BKE_reportf(op->reports, RPT_ERROR, "Write error: cannot save %s", name);
360                         }
361                         else {
362                                 printf("Saved: %s", name);
363                                 BKE_reportf(op->reports, RPT_INFO, "Saved file: %s", name);
364                         }
365                 }
366         }
367
368         BKE_image_release_ibuf(oglrender->ima, lock);
369
370         /* movie stats prints have no line break */
371         printf("\n");
372
373         /* go to next frame */
374         oglrender->nfra += scene->r.frame_step;
375         scene->r.cfra++;
376
377         /* stop at the end or on error */
378         if(scene->r.cfra > EFRA || !ok) {
379                 screen_opengl_render_end(C, op->customdata);
380                 return 0;
381         }
382
383         return 1;
384 }
385
386
387 static int screen_opengl_render_modal(bContext *C, wmOperator *op, wmEvent *event)
388 {
389         OGLRender *oglrender= op->customdata;
390
391         int ret;
392
393         switch(event->type) {
394                 case ESCKEY:
395                         /* cancel */
396                         screen_opengl_render_end(C, op->customdata);
397                         return OPERATOR_FINISHED;
398                 case TIMER:
399                         /* render frame? */
400                         if(oglrender->timer == event->customdata)
401                                 break;
402                 default:
403                         /* nothing to do */
404                         return OPERATOR_RUNNING_MODAL;
405         }
406
407         /* run first because screen_opengl_render_anim_step can free oglrender */
408         WM_event_add_notifier(C, NC_SCENE|ND_RENDER_RESULT, oglrender->scene);
409         
410         ret= screen_opengl_render_anim_step(C, op);
411
412         /* stop at the end or on error */
413         if(ret == 0) {
414                 return OPERATOR_FINISHED;
415         }
416
417         return OPERATOR_RUNNING_MODAL;
418 }
419
420 static int screen_opengl_render_invoke(bContext *C, wmOperator *op, wmEvent *event)
421 {
422         int anim= RNA_boolean_get(op->ptr, "animation");
423
424         if(!screen_opengl_render_init(C, op))
425                 return OPERATOR_CANCELLED;
426
427         if(!anim) {
428                 /* render image */
429                 screen_opengl_render_apply(op->customdata);
430                 screen_opengl_render_end(C, op->customdata);
431                 screen_set_image_output(C, event->x, event->y);
432
433                 return OPERATOR_FINISHED;
434         }
435         else {
436                 OGLRender *oglrender= op->customdata;
437
438                 if(!screen_opengl_render_anim_initialize(C, op))
439                         return OPERATOR_CANCELLED;
440
441                 screen_set_image_output(C, event->x, event->y);
442
443                 WM_event_add_modal_handler(C, op);
444                 oglrender->timer= WM_event_add_timer(CTX_wm_manager(C), CTX_wm_window(C), TIMER, 0.01f);
445
446                 return OPERATOR_RUNNING_MODAL;
447         }
448 }
449
450 /* executes blocking render */
451 static int screen_opengl_render_exec(bContext *C, wmOperator *op)
452 {
453         int anim= RNA_boolean_get(op->ptr, "animation");
454
455         if(!screen_opengl_render_init(C, op))
456                 return OPERATOR_CANCELLED;
457
458         if(!anim) { /* same as invoke */
459                 /* render image */
460                 screen_opengl_render_apply(op->customdata);
461                 screen_opengl_render_end(C, op->customdata);
462
463                 return OPERATOR_FINISHED;
464         }
465         else {
466                 int ret= 1;
467
468                 if(!screen_opengl_render_anim_initialize(C, op))
469                         return OPERATOR_CANCELLED;
470
471                 while(ret) {
472                         ret= screen_opengl_render_anim_step(C, op);
473                 }
474         }
475
476         // no redraw needed, we leave state as we entered it
477 //      ED_update_for_newframe(C, 1);
478         WM_event_add_notifier(C, NC_SCENE|ND_RENDER_RESULT, CTX_data_scene(C));
479
480         return OPERATOR_FINISHED;
481 }
482
483 void RENDER_OT_opengl(wmOperatorType *ot)
484 {
485         /* identifiers */
486         ot->name= "OpenGL Render";
487         ot->description= "OpenGL render active viewport";
488         ot->idname= "RENDER_OT_opengl";
489
490         /* api callbacks */
491         ot->invoke= screen_opengl_render_invoke;
492         ot->exec= screen_opengl_render_exec; /* blocking */
493         ot->modal= screen_opengl_render_modal;
494         ot->cancel= screen_opengl_render_cancel;
495
496         ot->poll= ED_operator_screenactive;
497
498         RNA_def_boolean(ot->srna, "animation", 0, "Animation", "");
499         RNA_def_boolean(ot->srna, "view_context", 1, "View Context", "Use the current 3D view for rendering, else use scene settings.");
500 }
501
502 /* function for getting an opengl buffer from a View3D, used by sequencer */
503 // extern void *sequencer_view3d_cb;