Fix #23238: crash rendering multiple scenes from compositor.
[blender.git] / source / blender / editors / object / object_bake.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) 2004 by Blender Foundation
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 /*
31         meshtools.c: no editmode (violated already :), tools operating on meshes
32 */
33
34 #include <string.h>
35
36 #include "MEM_guardedalloc.h"
37
38 #include "DNA_scene_types.h"
39 #include "DNA_screen_types.h"
40 #include "DNA_space_types.h"
41 #include "DNA_world_types.h"
42
43 #include "BLI_blenlib.h"
44 #include "BLI_threads.h"
45
46 #include "BKE_blender.h"
47 #include "BKE_context.h"
48 #include "BKE_global.h"
49 #include "BKE_image.h"
50 #include "BKE_main.h"
51 #include "BKE_report.h"
52
53 #include "RE_pipeline.h"
54 #include "RE_shader_ext.h"
55
56 #include "PIL_time.h"
57
58 #include "IMB_imbuf_types.h"
59 #include "IMB_imbuf.h"
60
61 #include "GPU_draw.h" /* GPU_free_image */
62
63 #include "WM_api.h"
64 #include "WM_types.h"
65
66
67 /* ****************** render BAKING ********************** */
68
69 /* threaded break test */
70 static int thread_break(void *unused)
71 {
72         return G.afbreek;
73 }
74
75 static ScrArea *biggest_image_area(bScreen *screen)
76 {
77         ScrArea *sa, *big= NULL;
78         int size, maxsize= 0;
79
80         for(sa= screen->areabase.first; sa; sa= sa->next) {
81                 if(sa->spacetype==SPACE_IMAGE) {
82                         size= sa->winx*sa->winy;
83                         if(sa->winx > 10 && sa->winy > 10 && size > maxsize) {
84                                 maxsize= size;
85                                 big= sa;
86                         }
87                 }
88         }
89         return big;
90 }
91
92
93 typedef struct BakeRender {
94         Render *re;
95         Main *main;
96         Scene *scene;
97         struct Object *actob;
98         int tot, ready;
99
100         ReportList *reports;
101
102         short *stop;
103         short *do_update;
104         float *progress;
105         
106         ListBase threads;
107
108         /* backup */
109         short prev_wo_amb_occ;
110         short prev_r_raytrace;
111
112         /* for redrawing */
113         ScrArea *sa;
114 } BakeRender;
115
116 /* use by exec and invoke */
117 int test_bake_internal(bContext *C, ReportList *reports)
118 {
119         Scene *scene= CTX_data_scene(C);
120
121         if(scene->r.renderer!=R_INTERN) {
122                 BKE_report(reports, RPT_ERROR, "Bake only supported for Internal Renderer");
123         } else if((scene->r.bake_flag & R_BAKE_TO_ACTIVE) && CTX_data_active_object(C)==NULL) {
124                 BKE_report(reports, RPT_ERROR, "No active object");
125         }
126         else if(scene->r.bake_mode==RE_BAKE_AO && scene->world==NULL) {
127                 BKE_report(reports, RPT_ERROR, "No world set up");
128         }
129         else {
130                 return 1;
131         }
132
133         return 0;
134 }
135
136 static void init_bake_internal(BakeRender *bkr, bContext *C)
137 {
138         Scene *scene= CTX_data_scene(C);
139
140         bkr->sa= biggest_image_area(CTX_wm_screen(C)); /* can be NULL */
141         bkr->main= CTX_data_main(C);
142         bkr->scene= scene;
143         bkr->actob= (scene->r.bake_flag & R_BAKE_TO_ACTIVE) ? OBACT : NULL;
144         bkr->re= RE_NewRender("_Bake View_");
145
146         if(scene->r.bake_mode==RE_BAKE_AO) {
147                 /* If raytracing or AO is disabled, switch it on temporarily for baking. */
148                 bkr->prev_wo_amb_occ = (scene->world->mode & WO_AMB_OCC) != 0;
149                 scene->world->mode |= WO_AMB_OCC;
150         }
151         if(scene->r.bake_mode==RE_BAKE_AO || bkr->actob) {
152                 bkr->prev_r_raytrace = (scene->r.mode & R_RAYTRACE) != 0;
153                 scene->r.mode |= R_RAYTRACE;
154         }
155 }
156
157 static void finish_bake_internal(BakeRender *bkr)
158 {
159         RE_Database_Free(bkr->re);
160
161         /* restore raytrace and AO */
162         if(bkr->scene->r.bake_mode==RE_BAKE_AO)
163                 if(bkr->prev_wo_amb_occ == 0)
164                         bkr->scene->world->mode &= ~WO_AMB_OCC;
165
166         if(bkr->scene->r.bake_mode==RE_BAKE_AO || bkr->actob)
167                 if(bkr->prev_r_raytrace == 0)
168                         bkr->scene->r.mode &= ~R_RAYTRACE;
169
170         if(bkr->tot) {
171                 Image *ima;
172                 /* force OpenGL reload and mipmap recalc */
173                 for(ima= G.main->image.first; ima; ima= ima->id.next) {
174                         if(ima->ok==IMA_OK_LOADED) {
175                                 ImBuf *ibuf= BKE_image_get_ibuf(ima, NULL);
176                                 if(ibuf && (ibuf->userflags & IB_BITMAPDIRTY)) {
177                                         GPU_free_image(ima);
178                                         imb_freemipmapImBuf(ibuf);
179                                 }
180                         }
181                 }
182         }
183 }
184
185 static void *do_bake_render(void *bake_v)
186 {
187         BakeRender *bkr= bake_v;
188
189         bkr->tot= RE_bake_shade_all_selected(bkr->re, bkr->scene->r.bake_mode, bkr->actob, NULL, bkr->progress);
190         bkr->ready= 1;
191
192         return NULL;
193 }
194
195 static void bake_startjob(void *bkv, short *stop, short *do_update, float *progress)
196 {
197         BakeRender *bkr= bkv;
198         Scene *scene= bkr->scene;
199         Main *bmain= bkr->main;
200
201         bkr->stop= stop;
202         bkr->do_update= do_update;
203         bkr->progress= progress;
204
205         RE_test_break_cb(bkr->re, NULL, thread_break);
206         G.afbreek= 0;   /* blender_test_break uses this global */
207
208         RE_Database_Baking(bkr->re, bmain, scene, scene->lay, scene->r.bake_mode, bkr->actob);
209
210         /* baking itself is threaded, cannot use test_break in threads. we also update optional imagewindow */
211         bkr->tot= RE_bake_shade_all_selected(bkr->re, scene->r.bake_mode, bkr->actob, bkr->do_update, bkr->progress);
212 }
213
214 static void bake_update(void *bkv)
215 {
216         BakeRender *bkr= bkv;
217
218         if(bkr->sa && bkr->sa->spacetype==SPACE_IMAGE) { /* incase the user changed while baking */
219                 SpaceImage *sima= bkr->sa->spacedata.first;
220                 if(sima)
221                         sima->image= RE_bake_shade_get_image();
222         }
223 }
224
225 static void bake_freejob(void *bkv)
226 {
227         BakeRender *bkr= bkv;
228         BLI_end_threads(&bkr->threads);
229         finish_bake_internal(bkr);
230
231         if(bkr->tot==0) BKE_report(bkr->reports, RPT_ERROR, "No Images found to bake to");
232         MEM_freeN(bkr);
233         G.rendering = 0;
234 }
235
236 /* catch esc */
237 static int objects_bake_render_modal(bContext *C, wmOperator *op, wmEvent *event)
238 {
239         /* no running blender, remove handler and pass through */
240         if(0==WM_jobs_test(CTX_wm_manager(C), CTX_data_scene(C)))
241                 return OPERATOR_FINISHED|OPERATOR_PASS_THROUGH;
242
243         /* running render */
244         switch (event->type) {
245                 case ESCKEY:
246                         return OPERATOR_RUNNING_MODAL;
247                         break;
248         }
249         return OPERATOR_PASS_THROUGH;
250 }
251
252 static int objects_bake_render_invoke(bContext *C, wmOperator *op, wmEvent *_event)
253 {
254         Scene *scene= CTX_data_scene(C);
255
256         if(test_bake_internal(C, op->reports)==0) {
257                 return OPERATOR_CANCELLED;
258         }
259         else {
260                 BakeRender *bkr= MEM_callocN(sizeof(BakeRender), "render bake");
261                 wmJob *steve;
262
263                 init_bake_internal(bkr, C);
264                 bkr->reports= op->reports;
265
266                 /* setup job */
267                 steve= WM_jobs_get(CTX_wm_manager(C), CTX_wm_window(C), scene, "Texture Bake", WM_JOB_EXCL_RENDER|WM_JOB_PRIORITY|WM_JOB_PROGRESS);
268                 WM_jobs_customdata(steve, bkr, bake_freejob);
269                 WM_jobs_timer(steve, 0.2, NC_IMAGE, 0); /* TODO - only draw bake image, can we enforce this */
270                 WM_jobs_callbacks(steve, bake_startjob, NULL, bake_update, NULL);
271
272                 G.afbreek= 0;
273                 G.rendering = 1;
274
275                 WM_jobs_start(CTX_wm_manager(C), steve);
276
277                 WM_cursor_wait(0);
278                 WM_event_add_notifier(C, NC_SCENE|ND_RENDER_RESULT, scene);
279
280                 /* add modal handler for ESC */
281                 WM_event_add_modal_handler(C, op);
282         }
283
284         return OPERATOR_RUNNING_MODAL;
285 }
286
287
288 static int bake_image_exec(bContext *C, wmOperator *op)
289 {
290         Main *bmain= CTX_data_main(C);
291         Scene *scene= CTX_data_scene(C);
292
293
294         if(test_bake_internal(C, op->reports)==0) {
295                 return OPERATOR_CANCELLED;
296         }
297         else {
298                 ListBase threads;
299                 BakeRender bkr;
300
301                 memset(&bkr, 0, sizeof(bkr));
302
303                 init_bake_internal(&bkr, C);
304                 bkr.reports= op->reports;
305
306                 RE_test_break_cb(bkr.re, NULL, thread_break);
307                 G.afbreek= 0;   /* blender_test_break uses this global */
308
309                 RE_Database_Baking(bkr.re, bmain, scene, scene->lay, scene->r.bake_mode, (scene->r.bake_flag & R_BAKE_TO_ACTIVE)? OBACT: NULL);
310
311                 /* baking itself is threaded, cannot use test_break in threads  */
312                 BLI_init_threads(&threads, do_bake_render, 1);
313                 bkr.ready= 0;
314                 BLI_insert_thread(&threads, &bkr);
315
316                 while(bkr.ready==0) {
317                         PIL_sleep_ms(50);
318                         if(bkr.ready)
319                                 break;
320
321                         /* used to redraw in 2.4x but this is just for exec in 2.5 */
322                         if (!G.background)
323                                 blender_test_break();
324                 }
325                 BLI_end_threads(&threads);
326
327                 if(bkr.tot==0) BKE_report(op->reports, RPT_ERROR, "No Images found to bake to");
328
329                 finish_bake_internal(&bkr);
330         }
331
332         WM_event_add_notifier(C, NC_SCENE|ND_RENDER_RESULT, scene);
333         return OPERATOR_FINISHED;
334 }
335
336 void OBJECT_OT_bake_image(wmOperatorType *ot)
337 {
338         /* identifiers */
339         ot->name= "Bake";
340         ot->description= "Bake image textures of selected objects";
341         ot->idname= "OBJECT_OT_bake_image";
342
343         /* api callbacks */
344         ot->exec= bake_image_exec;
345         ot->invoke= objects_bake_render_invoke;
346         ot->modal= objects_bake_render_modal;
347 }