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