merge from trunk 38379
[blender.git] / source / blender / editors / render / render_internal.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_internal.c
28  *  \ingroup edrend
29  */
30
31
32 #include <math.h>
33 #include <string.h>
34 #include <stddef.h>
35
36 #include "MEM_guardedalloc.h"
37
38 #include "BLI_blenlib.h"
39 #include "BLI_math.h"
40 #include "BLI_threads.h"
41 #include "BLI_rand.h"
42 #include "BLI_utildefines.h"
43
44 #include "BLF_api.h"
45
46 #include "DNA_scene_types.h"
47
48 #include "BKE_blender.h"
49 #include "BKE_context.h"
50 #include "BKE_global.h"
51 #include "BKE_image.h"
52 #include "BKE_library.h"
53 #include "BKE_main.h"
54 #include "BKE_node.h"
55 #include "BKE_multires.h"
56 #include "BKE_report.h"
57 #include "BKE_sequencer.h"
58 #include "BKE_screen.h"
59 #include "BKE_scene.h"
60
61 #include "WM_api.h"
62 #include "WM_types.h"
63
64 #include "ED_screen.h"
65 #include "ED_object.h"
66
67 #include "RE_pipeline.h"
68 #include "IMB_imbuf.h"
69 #include "IMB_imbuf_types.h"
70
71 #include "RNA_access.h"
72 #include "RNA_define.h"
73
74 #include "wm_window.h"
75
76 #include "render_intern.h"
77
78 /* Render Callbacks */
79
80 /* called inside thread! */
81 void image_buffer_rect_update(Scene *scene, RenderResult *rr, ImBuf *ibuf, volatile rcti *renrect)
82 {
83         float x1, y1, *rectf= NULL;
84         int ymin, ymax, xmin, xmax;
85         int rymin, rxmin, do_color_management;
86         char *rectc;
87
88         /* if renrect argument, we only refresh scanlines */
89         if(renrect) {
90                 /* if ymax==recty, rendering of layer is ready, we should not draw, other things happen... */
91                 if(rr->renlay==NULL || renrect->ymax>=rr->recty)
92                         return;
93
94                 /* xmin here is first subrect x coord, xmax defines subrect width */
95                 xmin = renrect->xmin + rr->crop;
96                 xmax = renrect->xmax - xmin + rr->crop;
97                 if(xmax<2)
98                         return;
99
100                 ymin= renrect->ymin + rr->crop;
101                 ymax= renrect->ymax - ymin + rr->crop;
102                 if(ymax<2)
103                         return;
104                 renrect->ymin= renrect->ymax;
105
106         }
107         else {
108                 xmin = ymin = rr->crop;
109                 xmax = rr->rectx - 2*rr->crop;
110                 ymax = rr->recty - 2*rr->crop;
111         }
112
113         /* xmin ymin is in tile coords. transform to ibuf */
114         rxmin= rr->tilerect.xmin + xmin;
115         if(rxmin >= ibuf->x) return;
116         rymin= rr->tilerect.ymin + ymin;
117         if(rymin >= ibuf->y) return;
118
119         if(rxmin + xmax > ibuf->x)
120                 xmax= ibuf->x - rxmin;
121         if(rymin + ymax > ibuf->y)
122                 ymax= ibuf->y - rymin;
123
124         if(xmax < 1 || ymax < 1) return;
125
126         /* find current float rect for display, first case is after composit... still weak */
127         if(rr->rectf)
128                 rectf= rr->rectf;
129         else {
130                 if(rr->rect32)
131                         return;
132                 else {
133                         if(rr->renlay==NULL || rr->renlay->rectf==NULL) return;
134                         rectf= rr->renlay->rectf;
135                 }
136         }
137         if(rectf==NULL) return;
138
139         if(ibuf->rect==NULL)
140                 imb_addrectImBuf(ibuf);
141         
142         rectf+= 4*(rr->rectx*ymin + xmin);
143         rectc= (char *)(ibuf->rect + ibuf->x*rymin + rxmin);
144
145         do_color_management = (scene && (scene->r.color_mgt_flag & R_COLOR_MANAGEMENT));
146         
147         /* XXX make nice consistent functions for this */
148         for(y1= 0; y1<ymax; y1++) {
149                 float *rf= rectf;
150                 float srgb[3];
151                 char *rc= rectc;
152                 const float dither = ibuf->dither / 255.0f;
153
154                 /* XXX temp. because crop offset */
155                 if(rectc >= (char *)(ibuf->rect)) {
156                         for(x1= 0; x1<xmax; x1++, rf += 4, rc+=4) {
157                                 /* color management */
158                                 if(do_color_management) {
159                                         srgb[0]= linearrgb_to_srgb(rf[0]);
160                                         srgb[1]= linearrgb_to_srgb(rf[1]);
161                                         srgb[2]= linearrgb_to_srgb(rf[2]);
162                                 }
163                                 else {
164                                         copy_v3_v3(srgb, rf);
165                                 }
166
167                                 /* dither */
168                                 if(dither != 0.0f) {
169                                         const float d = (BLI_frand()-0.5f)*dither;
170
171                                         srgb[0] += d;
172                                         srgb[1] += d;
173                                         srgb[2] += d;
174                                 }
175
176                                 /* write */
177                                 rc[0]= FTOCHAR(srgb[0]);
178                                 rc[1]= FTOCHAR(srgb[1]);
179                                 rc[2]= FTOCHAR(srgb[2]);
180                                 rc[3]= FTOCHAR(rf[3]);
181                         }
182                 }
183
184                 rectf += 4*rr->rectx;
185                 rectc += 4*ibuf->x;
186         }
187 }
188
189 /* ****************************** render invoking ***************** */
190
191 /* set callbacks, exported to sequence render too.
192  Only call in foreground (UI) renders. */
193
194 /* executes blocking render */
195 static int screen_render_exec(bContext *C, wmOperator *op)
196 {
197         Scene *scene= CTX_data_scene(C);
198         Render *re= RE_NewRender(scene->id.name);
199         Image *ima;
200         View3D *v3d= CTX_wm_view3d(C);
201         Main *mainp= CTX_data_main(C);
202         unsigned int lay= (v3d)? v3d->lay: scene->lay;
203         const short is_animation= RNA_boolean_get(op->ptr, "animation");
204         const short is_write_still= RNA_boolean_get(op->ptr, "write_still");
205         struct Object *camera_override= v3d ? V3D_CAMERA_LOCAL(v3d) : NULL;
206
207         if(!is_animation && is_write_still && BKE_imtype_is_movie(scene->r.imtype)) {
208                 BKE_report(op->reports, RPT_ERROR, "Can't write a single file with an animation format selected.");
209                 return OPERATOR_CANCELLED;
210         }
211
212         G.afbreek= 0;
213         RE_test_break_cb(re, NULL, (int (*)(void *)) blender_test_break);
214
215         ima= BKE_image_verify_viewer(IMA_TYPE_R_RESULT, "Render Result");
216         BKE_image_signal(ima, NULL, IMA_SIGNAL_FREE);
217         BKE_image_backup_render(scene, ima);
218
219         /* cleanup sequencer caches before starting user triggered render.
220            otherwise, invalidated cache entries can make their way into
221            the output rendering. We can't put that into RE_BlenderFrame,
222            since sequence rendering can call that recursively... (peter) */
223         seq_stripelem_cache_cleanup();
224
225         RE_SetReports(re, op->reports);
226
227         if(is_animation)
228                 RE_BlenderAnim(re, mainp, scene, camera_override, lay, scene->r.sfra, scene->r.efra, scene->r.frame_step);
229         else
230                 RE_BlenderFrame(re, mainp, scene, NULL, camera_override, lay, scene->r.cfra, is_write_still);
231
232         RE_SetReports(re, NULL);
233
234         // no redraw needed, we leave state as we entered it
235         ED_update_for_newframe(mainp, scene, CTX_wm_screen(C), 1);
236
237         WM_event_add_notifier(C, NC_SCENE|ND_RENDER_RESULT, scene);
238
239         return OPERATOR_FINISHED;
240 }
241
242 typedef struct RenderJob {
243         Main *main;
244         Scene *scene;
245         Render *re;
246         wmWindow *win;
247         SceneRenderLayer *srl;
248         struct Object *camera_override;
249         int lay;
250         short anim, write_still;
251         Image *image;
252         ImageUser iuser;
253         short *stop;
254         short *do_update;
255         float *progress;
256         ReportList *reports;
257 } RenderJob;
258
259 static void render_freejob(void *rjv)
260 {
261         RenderJob *rj= rjv;
262
263         MEM_freeN(rj);
264 }
265
266 /* str is IMA_MAX_RENDER_TEXT in size */
267 static void make_renderinfo_string(RenderStats *rs, Scene *scene, char *str)
268 {
269         char info_time_str[32]; // used to be extern to header_info.c
270         uintptr_t mem_in_use, mmap_in_use, peak_memory;
271         float megs_used_memory, mmap_used_memory, megs_peak_memory;
272         char *spos= str;
273
274         mem_in_use= MEM_get_memory_in_use();
275         mmap_in_use= MEM_get_mapped_memory_in_use();
276         peak_memory = MEM_get_peak_memory();
277
278         megs_used_memory= (mem_in_use-mmap_in_use)/(1024.0*1024.0);
279         mmap_used_memory= (mmap_in_use)/(1024.0*1024.0);
280         megs_peak_memory = (peak_memory)/(1024.0*1024.0);
281
282         if(scene->lay & 0xFF000000)
283                 spos+= sprintf(spos, "Localview | ");
284         else if(scene->r.scemode & R_SINGLE_LAYER)
285                 spos+= sprintf(spos, "Single Layer | ");
286
287         if(rs->statstr) {
288                 spos+= sprintf(spos, "%s ", rs->statstr);
289         }
290         else {
291                 spos+= sprintf(spos, "Fra:%d  Ve:%d Fa:%d ", (scene->r.cfra), rs->totvert, rs->totface);
292                 if(rs->tothalo) spos+= sprintf(spos, "Ha:%d ", rs->tothalo);
293                 if(rs->totstrand) spos+= sprintf(spos, "St:%d ", rs->totstrand);
294                 spos+= sprintf(spos, "La:%d Mem:%.2fM (%.2fM, peak %.2fM) ", rs->totlamp, megs_used_memory, mmap_used_memory, megs_peak_memory);
295
296                 if(rs->curfield)
297                         spos+= sprintf(spos, "Field %d ", rs->curfield);
298                 if(rs->curblur)
299                         spos+= sprintf(spos, "Blur %d ", rs->curblur);
300         }
301
302         BLI_timestr(rs->lastframetime, info_time_str);
303         spos+= sprintf(spos, "Time:%s ", info_time_str);
304
305         if(rs->curfsa)
306                 spos+= sprintf(spos, "| Full Sample %d ", rs->curfsa);
307         
308         if(rs->infostr && rs->infostr[0])
309                 spos+= sprintf(spos, "| %s ", rs->infostr);
310
311         /* very weak... but 512 characters is quite safe */
312         if(spos >= str+IMA_MAX_RENDER_TEXT)
313                 if (G.f & G_DEBUG)
314                         printf("WARNING! renderwin text beyond limit \n");
315
316 }
317
318 static void image_renderinfo_cb(void *rjv, RenderStats *rs)
319 {
320         RenderJob *rj= rjv;
321         RenderResult *rr;
322
323         rr= RE_AcquireResultRead(rj->re);
324
325         if(rr) {
326                 /* malloc OK here, stats_draw is not in tile threads */
327                 if(rr->text==NULL)
328                         rr->text= MEM_callocN(IMA_MAX_RENDER_TEXT, "rendertext");
329
330                 make_renderinfo_string(rs, rj->scene, rr->text);
331         }
332
333         RE_ReleaseResult(rj->re);
334
335         /* make jobs timer to send notifier */
336         *(rj->do_update)= 1;
337
338 }
339
340 static void render_progress_update(void *rjv, float progress)
341 {
342         RenderJob *rj= rjv;
343         
344         if(rj->progress)
345                 *rj->progress = progress;
346 }
347
348 static void image_rect_update(void *rjv, RenderResult *rr, volatile rcti *renrect)
349 {
350         RenderJob *rj= rjv;
351         Image *ima= rj->image;
352         ImBuf *ibuf;
353         void *lock;
354
355         /* only update if we are displaying the slot being rendered */
356         if(ima->render_slot != ima->last_render_slot)
357                 return;
358
359         ibuf= BKE_image_acquire_ibuf(ima, &rj->iuser, &lock);
360         if(ibuf) {
361                 image_buffer_rect_update(rj->scene, rr, ibuf, renrect);
362
363                 /* make jobs timer to send notifier */
364                 *(rj->do_update)= 1;
365         }
366         BKE_image_release_ibuf(ima, lock);
367 }
368
369 static void render_startjob(void *rjv, short *stop, short *do_update, float *progress)
370 {
371         RenderJob *rj= rjv;
372
373         rj->stop= stop;
374         rj->do_update= do_update;
375         rj->progress= progress;
376
377         RE_SetReports(rj->re, rj->reports);
378
379         if(rj->anim)
380                 RE_BlenderAnim(rj->re, rj->main, rj->scene, rj->camera_override, rj->lay, rj->scene->r.sfra, rj->scene->r.efra, rj->scene->r.frame_step);
381         else
382                 RE_BlenderFrame(rj->re, rj->main, rj->scene, rj->srl, rj->camera_override, rj->lay, rj->scene->r.cfra, rj->write_still);
383
384         RE_SetReports(rj->re, NULL);
385 }
386
387 static void render_endjob(void *rjv)
388 {
389         RenderJob *rj= rjv;     
390
391         /* this render may be used again by the sequencer without the active 'Render' where the callbacks
392          * would be re-assigned. assign dummy callbacks to avoid referencing freed renderjobs bug [#24508] */
393         RE_InitRenderCB(rj->re);
394
395         if(rj->main != G.main)
396                 free_main(rj->main);
397
398         /* else the frame will not update for the original value */
399         if(!(rj->scene->r.scemode & R_NO_FRAME_UPDATE))
400                 ED_update_for_newframe(G.main, rj->scene, rj->win->screen, 1);
401         
402         /* XXX above function sets all tags in nodes */
403         ntreeClearTags(rj->scene->nodetree);
404         
405         /* potentially set by caller */
406         rj->scene->r.scemode &= ~R_NO_FRAME_UPDATE;
407         
408         if(rj->srl) {
409                 NodeTagIDChanged(rj->scene->nodetree, &rj->scene->id);
410                 WM_main_add_notifier(NC_NODE|NA_EDITED, rj->scene);
411         }
412         
413         /* XXX render stability hack */
414         G.rendering = 0;
415         WM_main_add_notifier(NC_WINDOW, NULL);
416 }
417
418 /* called by render, check job 'stop' value or the global */
419 static int render_breakjob(void *rjv)
420 {
421         RenderJob *rj= rjv;
422
423         if(G.afbreek)
424                 return 1;
425         if(rj->stop && *(rj->stop))
426                 return 1;
427         return 0;
428 }
429
430 /* runs in thread, no cursor setting here works. careful with notifiers too (malloc conflicts) */
431 /* maybe need a way to get job send notifer? */
432 static void render_drawlock(void *UNUSED(rjv), int lock)
433 {
434         BKE_spacedata_draw_locks(lock);
435         
436 }
437
438 /* catch esc */
439 static int screen_render_modal(bContext *C, wmOperator *UNUSED(op), wmEvent *event)
440 {
441         /* no running blender, remove handler and pass through */
442         if(0==WM_jobs_test(CTX_wm_manager(C), CTX_data_scene(C))) {
443                 return OPERATOR_FINISHED|OPERATOR_PASS_THROUGH;
444         }
445
446         /* running render */
447         switch (event->type) {
448                 case ESCKEY:
449                         return OPERATOR_RUNNING_MODAL;
450                         break;
451         }
452         return OPERATOR_PASS_THROUGH;
453 }
454
455 /* using context, starts job */
456 static int screen_render_invoke(bContext *C, wmOperator *op, wmEvent *event)
457 {
458         /* new render clears all callbacks */
459         Main *mainp;
460         Scene *scene= CTX_data_scene(C);
461         SceneRenderLayer *srl=NULL;
462         bScreen *screen= CTX_wm_screen(C);
463         View3D *v3d= CTX_wm_view3d(C);
464         Render *re;
465         wmJob *steve;
466         RenderJob *rj;
467         Image *ima;
468         int jobflag;
469         const short is_animation= RNA_boolean_get(op->ptr, "animation");
470         const short is_write_still= RNA_boolean_get(op->ptr, "write_still");
471         struct Object *camera_override= v3d ? V3D_CAMERA_LOCAL(v3d) : NULL;
472         
473         /* only one render job at a time */
474         if(WM_jobs_test(CTX_wm_manager(C), scene))
475                 return OPERATOR_CANCELLED;
476
477         if(!RE_is_rendering_allowed(scene, camera_override, op->reports)) {
478                 return OPERATOR_CANCELLED;
479         }
480
481         if(!is_animation && is_write_still && BKE_imtype_is_movie(scene->r.imtype)) {
482                 BKE_report(op->reports, RPT_ERROR, "Can't write a single file with an animation format selected.");
483                 return OPERATOR_CANCELLED;
484         }       
485         
486         /* stop all running jobs, currently previews frustrate Render */
487         WM_jobs_stop_all(CTX_wm_manager(C));
488
489         /* get main */
490         if(G.rt == 101) {
491                 /* thread-safety experiment, copy main from the undo buffer */
492                 mainp= BKE_undo_get_main(&scene);
493         }
494         else
495                 mainp= CTX_data_main(C);
496
497         /* cancel animation playback */
498         if (screen->animtimer)
499                 ED_screen_animation_play(C, 0, 0);
500         
501         /* handle UI stuff */
502         WM_cursor_wait(1);
503
504         /* flush multires changes (for sculpt) */
505         multires_force_render_update(CTX_data_active_object(C));
506
507         /* cleanup sequencer caches before starting user triggered render.
508            otherwise, invalidated cache entries can make their way into
509            the output rendering. We can't put that into RE_BlenderFrame,
510            since sequence rendering can call that recursively... (peter) */
511         seq_stripelem_cache_cleanup();
512
513         /* get editmode results */
514         ED_object_exit_editmode(C, 0);  /* 0 = does not exit editmode */
515
516         // store spare
517         // get view3d layer, local layer, make this nice api call to render
518         // store spare
519
520         /* ensure at least 1 area shows result */
521         render_view_open(C, event->x, event->y);
522
523         jobflag= WM_JOB_EXCL_RENDER|WM_JOB_PRIORITY|WM_JOB_PROGRESS;
524         
525         /* single layer re-render */
526         if(RNA_property_is_set(op->ptr, "layer")) {
527                 SceneRenderLayer *rl;
528                 Scene *scn;
529                 char scene_name[MAX_ID_NAME-2], rl_name[RE_MAXNAME];
530
531                 RNA_string_get(op->ptr, "layer", rl_name);
532                 RNA_string_get(op->ptr, "scene", scene_name);
533
534                 scn = (Scene *)BLI_findstring(&mainp->scene, scene_name, offsetof(ID, name) + 2);
535                 rl = (SceneRenderLayer *)BLI_findstring(&scene->r.layers, rl_name, offsetof(SceneRenderLayer, name));
536                 
537                 if (scn && rl) {
538                         /* camera switch wont have updated */
539                         scn->r.cfra= scene->r.cfra;
540                         scene_camera_switch_update(scn);
541
542                         scene = scn;
543                         srl = rl;
544                 }
545                 jobflag |= WM_JOB_SUSPEND;
546         }
547
548         /* job custom data */
549         rj= MEM_callocN(sizeof(RenderJob), "render job");
550         rj->main= mainp;
551         rj->scene= scene;
552         rj->win= CTX_wm_window(C);
553         rj->srl = srl;
554         rj->camera_override = camera_override;
555         rj->lay = (v3d)? v3d->lay: scene->lay;
556         rj->anim= is_animation;
557         rj->write_still= is_write_still && !is_animation;
558         rj->iuser.scene= scene;
559         rj->iuser.ok= 1;
560         rj->reports= op->reports;
561
562         /* setup job */
563         steve= WM_jobs_get(CTX_wm_manager(C), CTX_wm_window(C), scene, "Render", jobflag);
564         WM_jobs_customdata(steve, rj, render_freejob);
565         WM_jobs_timer(steve, 0.2, NC_SCENE|ND_RENDER_RESULT, 0);
566         WM_jobs_callbacks(steve, render_startjob, NULL, NULL, render_endjob);
567
568         /* get a render result image, and make sure it is empty */
569         ima= BKE_image_verify_viewer(IMA_TYPE_R_RESULT, "Render Result");
570         BKE_image_signal(ima, NULL, IMA_SIGNAL_FREE);
571         BKE_image_backup_render(rj->scene, ima);
572         rj->image= ima;
573
574         /* setup new render */
575         re= RE_NewRender(scene->id.name);
576         RE_test_break_cb(re, rj, render_breakjob);
577         RE_draw_lock_cb(re, rj, render_drawlock);
578         RE_display_draw_cb(re, rj, image_rect_update);
579         RE_stats_draw_cb(re, rj, image_renderinfo_cb);
580         RE_progress_cb(re, rj, render_progress_update);
581
582         rj->re= re;
583         G.afbreek= 0;
584
585         WM_jobs_start(CTX_wm_manager(C), steve);
586
587         WM_cursor_wait(0);
588         WM_event_add_notifier(C, NC_SCENE|ND_RENDER_RESULT, scene);
589
590         /* we set G.rendering here already instead of only in the job, this ensure
591            main loop or other scene updates are disabled in time, since they may
592            have started before the job thread */
593         G.rendering = 1;
594
595         /* add modal handler for ESC */
596         WM_event_add_modal_handler(C, op);
597
598         return OPERATOR_RUNNING_MODAL;
599 }
600
601 /* contextual render, using current scene, view3d? */
602 void RENDER_OT_render(wmOperatorType *ot)
603 {
604         /* identifiers */
605         ot->name= _("Render");
606         ot->description= _("Render active scene");
607         ot->idname= "RENDER_OT_render";
608
609         /* api callbacks */
610         ot->invoke= screen_render_invoke;
611         ot->modal= screen_render_modal;
612         ot->exec= screen_render_exec;
613
614         /*ot->poll= ED_operator_screenactive;*/ /* this isnt needed, causes failer in background mode */
615
616         RNA_def_boolean(ot->srna, "animation", 0, _("Animation"), _("Render files from the animation range of this scene"));
617         RNA_def_boolean(ot->srna, "write_still", 0, _("Write Image"), _("Save rendered the image to the output path (used only when animation is disabled)"));
618         RNA_def_string(ot->srna, "layer", "", RE_MAXNAME, _("Render Layer"), _("Single render layer to re-render"));
619         RNA_def_string(ot->srna, "scene", "", MAX_ID_NAME-2, _("Scene"), _("Re-render single layer in this scene"));
620 }
621