2 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
18 * The Original Code is Copyright (C) 2008 Blender Foundation.
19 * All rights reserved.
22 * ***** END GPL LICENSE BLOCK *****
25 /** \file blender/editors/render/render_internal.c
34 #include "MEM_guardedalloc.h"
36 #include "BLI_blenlib.h"
38 #include "BLI_threads.h"
40 #include "BLI_utildefines.h"
42 #include "DNA_scene_types.h"
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"
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"
60 #include "ED_screen.h"
61 #include "ED_object.h"
63 #include "RE_pipeline.h"
64 #include "IMB_colormanagement.h"
65 #include "IMB_imbuf.h"
66 #include "IMB_imbuf_types.h"
68 #include "RNA_access.h"
69 #include "RNA_define.h"
71 #include "wm_window.h"
73 #include "render_intern.h"
75 /* Render Callbacks */
77 /* called inside thread! */
78 void image_buffer_rect_update(Scene *scene, RenderResult *rr, ImBuf *ibuf, volatile rcti *renrect)
81 int ymin, ymax, xmin, xmax;
82 int rymin, rxmin, predivide, profile_from;
85 /* if renrect argument, we only refresh scanlines */
87 /* if ymax==recty, rendering of layer is ready, we should not draw, other things happen... */
88 if (rr->renlay == NULL || renrect->ymax >= rr->recty)
91 /* xmin here is first subrect x coord, xmax defines subrect width */
92 xmin = renrect->xmin + rr->crop;
93 xmax = renrect->xmax - xmin + rr->crop;
97 ymin = renrect->ymin + rr->crop;
98 ymax = renrect->ymax - ymin + rr->crop;
101 renrect->ymin = renrect->ymax;
105 xmin = ymin = rr->crop;
106 xmax = rr->rectx - 2 * rr->crop;
107 ymax = rr->recty - 2 * rr->crop;
110 /* xmin ymin is in tile coords. transform to ibuf */
111 rxmin = rr->tilerect.xmin + xmin;
112 if (rxmin >= ibuf->x) return;
113 rymin = rr->tilerect.ymin + ymin;
114 if (rymin >= ibuf->y) return;
116 if (rxmin + xmax > ibuf->x)
117 xmax = ibuf->x - rxmin;
118 if (rymin + ymax > ibuf->y)
119 ymax = ibuf->y - rymin;
121 if (xmax < 1 || ymax < 1) return;
123 /* find current float rect for display, first case is after composite... still weak */
130 if (rr->renlay == NULL || rr->renlay->rectf == NULL) return;
131 rectf = rr->renlay->rectf;
134 if (rectf == NULL) return;
136 if (ibuf->rect == NULL)
137 imb_addrectImBuf(ibuf);
139 rectf += 4 * (rr->rectx * ymin + xmin);
140 rectc = (unsigned char *)(ibuf->rect + ibuf->x * rymin + rxmin);
142 if (scene && (scene->r.color_mgt_flag & R_COLOR_MANAGEMENT)) {
143 profile_from = IB_PROFILE_LINEAR_RGB;
144 predivide = (scene->r.color_mgt_flag & R_COLOR_MANAGEMENT_PREDIVIDE);
147 profile_from = IB_PROFILE_SRGB;
151 IMB_buffer_byte_from_float(rectc, rectf,
152 4, ibuf->dither, IB_PROFILE_SRGB, profile_from, predivide,
153 xmax, ymax, ibuf->x, rr->rectx);
155 IMB_partial_display_buffer_update(ibuf, rectf, rr->rectx, rxmin, rymin,
156 rxmin, rymin, rxmin + xmax, rymin + ymax);
159 /* ****************************** render invoking ***************** */
161 /* set callbacks, exported to sequence render too.
162 * Only call in foreground (UI) renders. */
164 static void screen_render_scene_layer_set(wmOperator *op, Main *mainp, Scene **scene, SceneRenderLayer **srl)
166 /* single layer re-render */
167 if (RNA_struct_property_is_set(op->ptr, "scene")) {
169 char scene_name[MAX_ID_NAME - 2];
171 RNA_string_get(op->ptr, "scene", scene_name);
172 scn = (Scene *)BLI_findstring(&mainp->scene, scene_name, offsetof(ID, name) + 2);
175 /* camera switch wont have updated */
176 scn->r.cfra = (*scene)->r.cfra;
177 BKE_scene_camera_switch_update(scn);
183 if (RNA_struct_property_is_set(op->ptr, "layer")) {
184 SceneRenderLayer *rl;
185 char rl_name[RE_MAXNAME];
187 RNA_string_get(op->ptr, "layer", rl_name);
188 rl = (SceneRenderLayer *)BLI_findstring(&(*scene)->r.layers, rl_name, offsetof(SceneRenderLayer, name));
195 /* executes blocking render */
196 static int screen_render_exec(bContext *C, wmOperator *op)
198 Scene *scene = CTX_data_scene(C);
199 SceneRenderLayer *srl = NULL;
202 View3D *v3d = CTX_wm_view3d(C);
203 Main *mainp = CTX_data_main(C);
205 const short is_animation = RNA_boolean_get(op->ptr, "animation");
206 const short is_write_still = RNA_boolean_get(op->ptr, "write_still");
207 struct Object *camera_override = v3d ? V3D_CAMERA_LOCAL(v3d) : NULL;
209 /* custom scene and single layer re-render */
210 screen_render_scene_layer_set(op, mainp, &scene, &srl);
212 if (!is_animation && is_write_still && BKE_imtype_is_movie(scene->r.im_format.imtype)) {
213 BKE_report(op->reports, RPT_ERROR, "Can't write a single file with an animation format selected");
214 return OPERATOR_CANCELLED;
217 re = RE_NewRender(scene->id.name);
218 lay = (v3d) ? v3d->lay : scene->lay;
221 RE_test_break_cb(re, NULL, (int (*)(void *))blender_test_break);
223 ima = BKE_image_verify_viewer(IMA_TYPE_R_RESULT, "Render Result");
224 BKE_image_signal(ima, NULL, IMA_SIGNAL_FREE);
225 BKE_image_backup_render(scene, ima);
227 /* cleanup sequencer caches before starting user triggered render.
228 * otherwise, invalidated cache entries can make their way into
229 * the output rendering. We can't put that into RE_BlenderFrame,
230 * since sequence rendering can call that recursively... (peter) */
231 seq_stripelem_cache_cleanup();
233 RE_SetReports(re, op->reports);
236 RE_BlenderAnim(re, mainp, scene, camera_override, lay, scene->r.sfra, scene->r.efra, scene->r.frame_step);
238 RE_BlenderFrame(re, mainp, scene, srl, camera_override, lay, scene->r.cfra, is_write_still);
240 RE_SetReports(re, NULL);
242 // no redraw needed, we leave state as we entered it
243 ED_update_for_newframe(mainp, scene, 1);
245 WM_event_add_notifier(C, NC_SCENE | ND_RENDER_RESULT, scene);
247 return OPERATOR_FINISHED;
250 typedef struct RenderJob {
255 SceneRenderLayer *srl;
256 struct Object *camera_override;
258 short anim, write_still;
267 static void render_freejob(void *rjv)
274 /* str is IMA_MAX_RENDER_TEXT in size */
275 static void make_renderinfo_string(RenderStats *rs, Scene *scene, char *str)
277 char info_time_str[32]; // used to be extern to header_info.c
278 uintptr_t mem_in_use, mmap_in_use, peak_memory;
279 float megs_used_memory, mmap_used_memory, megs_peak_memory;
282 mem_in_use = MEM_get_memory_in_use();
283 mmap_in_use = MEM_get_mapped_memory_in_use();
284 peak_memory = MEM_get_peak_memory();
286 megs_used_memory = (mem_in_use - mmap_in_use) / (1024.0 * 1024.0);
287 mmap_used_memory = (mmap_in_use) / (1024.0 * 1024.0);
288 megs_peak_memory = (peak_memory) / (1024.0 * 1024.0);
290 if (scene->lay & 0xFF000000)
291 spos += sprintf(spos, "Localview | ");
292 else if (scene->r.scemode & R_SINGLE_LAYER)
293 spos += sprintf(spos, "Single Layer | ");
295 spos += sprintf(spos, "Frame:%d ", (scene->r.cfra));
298 spos += sprintf(spos, "| %s ", rs->statstr);
301 if (rs->totvert) spos += sprintf(spos, "Ve:%d ", rs->totvert);
302 if (rs->totface) spos += sprintf(spos, "Fa:%d ", rs->totface);
303 if (rs->tothalo) spos += sprintf(spos, "Ha:%d ", rs->tothalo);
304 if (rs->totstrand) spos += sprintf(spos, "St:%d ", rs->totstrand);
305 if (rs->totlamp) spos += sprintf(spos, "La:%d ", rs->totlamp);
306 spos += sprintf(spos, "Mem:%.2fM (%.2fM, peak %.2fM) ", megs_used_memory, mmap_used_memory, megs_peak_memory);
309 spos += sprintf(spos, "Field %d ", rs->curfield);
311 spos += sprintf(spos, "Blur %d ", rs->curblur);
314 BLI_timestr(rs->lastframetime, info_time_str);
315 spos += sprintf(spos, "Time:%s ", info_time_str);
318 spos += sprintf(spos, "| Full Sample %d ", rs->curfsa);
320 if (rs->infostr && rs->infostr[0])
321 spos += sprintf(spos, "| %s ", rs->infostr);
323 /* very weak... but 512 characters is quite safe */
324 if (spos >= str + IMA_MAX_RENDER_TEXT)
325 if (G.debug & G_DEBUG)
326 printf("WARNING! renderwin text beyond limit\n");
330 static void image_renderinfo_cb(void *rjv, RenderStats *rs)
335 rr = RE_AcquireResultRead(rj->re);
338 /* malloc OK here, stats_draw is not in tile threads */
339 if (rr->text == NULL)
340 rr->text = MEM_callocN(IMA_MAX_RENDER_TEXT, "rendertext");
342 make_renderinfo_string(rs, rj->scene, rr->text);
345 RE_ReleaseResult(rj->re);
347 /* make jobs timer to send notifier */
348 *(rj->do_update) = TRUE;
352 static void render_progress_update(void *rjv, float progress)
356 if (rj->progress && *rj->progress != progress) {
357 *rj->progress = progress;
359 /* make jobs timer to send notifier */
360 *(rj->do_update) = TRUE;
364 static void image_rect_update(void *rjv, RenderResult *rr, volatile rcti *renrect)
367 Image *ima = rj->image;
371 /* only update if we are displaying the slot being rendered */
372 if (ima->render_slot != ima->last_render_slot)
375 ibuf = BKE_image_acquire_ibuf(ima, &rj->iuser, &lock);
377 image_buffer_rect_update(rj->scene, rr, ibuf, renrect);
379 /* make jobs timer to send notifier */
380 *(rj->do_update) = TRUE;
382 BKE_image_release_ibuf(ima, lock);
385 static void render_startjob(void *rjv, short *stop, short *do_update, float *progress)
390 rj->do_update = do_update;
391 rj->progress = progress;
393 RE_SetReports(rj->re, rj->reports);
396 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);
398 RE_BlenderFrame(rj->re, rj->main, rj->scene, rj->srl, rj->camera_override, rj->lay, rj->scene->r.cfra, rj->write_still);
400 RE_SetReports(rj->re, NULL);
403 static void render_endjob(void *rjv)
407 /* this render may be used again by the sequencer without the active 'Render' where the callbacks
408 * would be re-assigned. assign dummy callbacks to avoid referencing freed renderjobs bug [#24508] */
409 RE_InitRenderCB(rj->re);
411 if (rj->main != G.main)
414 /* else the frame will not update for the original value */
415 if (!(rj->scene->r.scemode & R_NO_FRAME_UPDATE)) {
416 /* possible this fails of loading new file while rendering */
417 if (G.main->wm.first) {
418 ED_update_for_newframe(G.main, rj->scene, 1);
422 /* XXX above function sets all tags in nodes */
423 ntreeCompositClearTags(rj->scene->nodetree);
425 /* potentially set by caller */
426 rj->scene->r.scemode &= ~R_NO_FRAME_UPDATE;
429 nodeUpdateID(rj->scene->nodetree, &rj->scene->id);
430 WM_main_add_notifier(NC_NODE | NA_EDITED, rj->scene);
433 /* XXX render stability hack */
435 WM_main_add_notifier(NC_WINDOW, NULL);
438 /* called by render, check job 'stop' value or the global */
439 static int render_breakjob(void *rjv)
445 if (rj->stop && *(rj->stop))
450 /* runs in thread, no cursor setting here works. careful with notifiers too (malloc conflicts) */
451 /* maybe need a way to get job send notifer? */
452 static void render_drawlock(void *UNUSED(rjv), int lock)
454 BKE_spacedata_draw_locks(lock);
459 static int screen_render_modal(bContext *C, wmOperator *UNUSED(op), wmEvent *event)
461 /* no running blender, remove handler and pass through */
462 if (0 == WM_jobs_test(CTX_wm_manager(C), CTX_data_scene(C))) {
463 return OPERATOR_FINISHED | OPERATOR_PASS_THROUGH;
467 switch (event->type) {
469 return OPERATOR_RUNNING_MODAL;
472 return OPERATOR_PASS_THROUGH;
475 /* using context, starts job */
476 static int screen_render_invoke(bContext *C, wmOperator *op, wmEvent *event)
478 /* new render clears all callbacks */
480 Scene *scene = CTX_data_scene(C);
481 SceneRenderLayer *srl = NULL;
482 View3D *v3d = CTX_wm_view3d(C);
488 const short is_animation = RNA_boolean_get(op->ptr, "animation");
489 const short is_write_still = RNA_boolean_get(op->ptr, "write_still");
490 struct Object *camera_override = v3d ? V3D_CAMERA_LOCAL(v3d) : NULL;
493 /* only one render job at a time */
494 if (WM_jobs_test(CTX_wm_manager(C), scene))
495 return OPERATOR_CANCELLED;
497 if (!RE_is_rendering_allowed(scene, camera_override, op->reports)) {
498 return OPERATOR_CANCELLED;
501 if (!is_animation && is_write_still && BKE_imtype_is_movie(scene->r.im_format.imtype)) {
502 BKE_report(op->reports, RPT_ERROR, "Can't write a single file with an animation format selected");
503 return OPERATOR_CANCELLED;
506 /* stop all running jobs, currently previews frustrate Render */
507 WM_jobs_stop_all(CTX_wm_manager(C));
511 /* thread-safety experiment, copy main from the undo buffer */
512 mainp = BKE_undo_get_main(&scene);
515 mainp = CTX_data_main(C);
517 /* cancel animation playback */
518 if (ED_screen_animation_playing(CTX_wm_manager(C)))
519 ED_screen_animation_play(C, 0, 0);
521 /* handle UI stuff */
524 /* flush multires changes (for sculpt) */
525 multires_force_render_update(CTX_data_active_object(C));
527 /* cleanup sequencer caches before starting user triggered render.
528 * otherwise, invalidated cache entries can make their way into
529 * the output rendering. We can't put that into RE_BlenderFrame,
530 * since sequence rendering can call that recursively... (peter) */
531 seq_stripelem_cache_cleanup();
533 /* get editmode results */
534 ED_object_exit_editmode(C, 0); /* 0 = does not exit editmode */
537 // get view3d layer, local layer, make this nice api call to render
540 /* ensure at least 1 area shows result */
541 render_view_open(C, event->x, event->y);
543 jobflag = WM_JOB_EXCL_RENDER | WM_JOB_PRIORITY | WM_JOB_PROGRESS;
545 /* custom scene and single layer re-render */
546 screen_render_scene_layer_set(op, mainp, &scene, &srl);
548 if (RNA_struct_property_is_set(op->ptr, "layer"))
549 jobflag |= WM_JOB_SUSPEND;
551 /* job custom data */
552 rj = MEM_callocN(sizeof(RenderJob), "render job");
555 rj->win = CTX_wm_window(C);
557 rj->camera_override = camera_override;
558 rj->lay = (v3d) ? v3d->lay : scene->lay;
559 rj->anim = is_animation;
560 rj->write_still = is_write_still && !is_animation;
561 rj->iuser.scene = scene;
563 rj->reports = op->reports;
566 if (RE_seq_render_active(scene, &scene->r)) name = "Sequence Render";
567 else name = "Render";
569 steve = WM_jobs_get(CTX_wm_manager(C), CTX_wm_window(C), scene, name, jobflag);
570 WM_jobs_customdata(steve, rj, render_freejob);
571 WM_jobs_timer(steve, 0.2, NC_SCENE | ND_RENDER_RESULT, 0);
572 WM_jobs_callbacks(steve, render_startjob, NULL, NULL, render_endjob);
574 /* get a render result image, and make sure it is empty */
575 ima = BKE_image_verify_viewer(IMA_TYPE_R_RESULT, "Render Result");
576 BKE_image_signal(ima, NULL, IMA_SIGNAL_FREE);
577 BKE_image_backup_render(rj->scene, ima);
580 /* setup new render */
581 re = RE_NewRender(scene->id.name);
582 RE_test_break_cb(re, rj, render_breakjob);
583 RE_draw_lock_cb(re, rj, render_drawlock);
584 RE_display_draw_cb(re, rj, image_rect_update);
585 RE_stats_draw_cb(re, rj, image_renderinfo_cb);
586 RE_progress_cb(re, rj, render_progress_update);
591 WM_jobs_start(CTX_wm_manager(C), steve);
594 WM_event_add_notifier(C, NC_SCENE | ND_RENDER_RESULT, scene);
596 /* we set G.rendering here already instead of only in the job, this ensure
597 * main loop or other scene updates are disabled in time, since they may
598 * have started before the job thread */
601 /* add modal handler for ESC */
602 WM_event_add_modal_handler(C, op);
604 return OPERATOR_RUNNING_MODAL;
607 /* contextual render, using current scene, view3d? */
608 void RENDER_OT_render(wmOperatorType *ot)
612 ot->description = "Render active scene";
613 ot->idname = "RENDER_OT_render";
616 ot->invoke = screen_render_invoke;
617 ot->modal = screen_render_modal;
618 ot->exec = screen_render_exec;
620 /*ot->poll = ED_operator_screenactive;*/ /* this isn't needed, causes failer in background mode */
622 RNA_def_boolean(ot->srna, "animation", 0, "Animation", "Render files from the animation range of this scene");
623 RNA_def_boolean(ot->srna, "write_still", 0, "Write Image", "Save rendered the image to the output path (used only when animation is disabled)");
624 RNA_def_string(ot->srna, "layer", "", RE_MAXNAME, "Render Layer", "Single render layer to re-render (used only when animation is disabled)");
625 RNA_def_string(ot->srna, "scene", "", MAX_ID_NAME - 2, "Scene", "Scene to render, current scene if not specified");