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