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