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