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