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