Fix syntax for ID keyword.
[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_image_types.h"
39 #include "DNA_scene_types.h"
40 #include "DNA_screen_types.h"
41 #include "DNA_space_types.h"
42 #include "DNA_world_types.h"
43
44 #include "BLI_blenlib.h"
45 #include "BLI_threads.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_object.h"
53 #include "BKE_utildefines.h"
54 #include "BKE_report.h"
55
56 #include "RE_pipeline.h"
57 #include "RE_shader_ext.h"
58
59 #include "PIL_time.h"
60
61 #include "IMB_imbuf_types.h"
62 #include "IMB_imbuf.h"
63
64 #include "GPU_draw.h" /* GPU_free_image */
65
66 #include "WM_api.h"
67 #include "WM_types.h"
68
69
70 /* ****************** render BAKING ********************** */
71
72 /* threaded break test */
73 static int thread_break(void *unused)
74 {
75         return G.afbreek;
76 }
77
78 static ScrArea *biggest_image_area(bScreen *screen)
79 {
80         ScrArea *sa, *big= NULL;
81         int size, maxsize= 0;
82
83         for(sa= screen->areabase.first; sa; sa= sa->next) {
84                 if(sa->spacetype==SPACE_IMAGE) {
85                         size= sa->winx*sa->winy;
86                         if(sa->winx > 10 && sa->winy > 10 && size > maxsize) {
87                                 maxsize= size;
88                                 big= sa;
89                         }
90                 }
91         }
92         return big;
93 }
94
95
96 typedef struct BakeRender {
97         Render *re;
98         Scene *scene;
99         struct Object *actob;
100         int tot, ready;
101
102         ReportList *reports;
103
104         short *stop;
105         short *do_update;
106
107         ListBase threads;
108
109         /* backup */
110         short prev_wo_amb_occ;
111         short prev_r_raytrace;
112
113         /* for redrawing */
114         ScrArea *sa;
115 } BakeRender;
116
117 /* use by exec and invoke */
118 int test_bake_internal(bContext *C, ReportList *reports)
119 {
120         Scene *scene= CTX_data_scene(C);
121
122         if(scene->r.renderer!=R_INTERN) {
123                 BKE_report(reports, RPT_ERROR, "Bake only supported for Internal Renderer");
124         } else if((scene->r.bake_flag & R_BAKE_TO_ACTIVE) && CTX_data_active_object(C)==NULL) {
125                 BKE_report(reports, RPT_ERROR, "No active object");
126         }
127         else if(scene->r.bake_mode==RE_BAKE_AO && scene->world==NULL) {
128                 BKE_report(reports, RPT_ERROR, "No world set up");
129         }
130         else {
131                 return 1;
132         }
133
134         return 0;
135 }
136
137 static void init_bake_internal(BakeRender *bkr, bContext *C)
138 {
139         Scene *scene= CTX_data_scene(C);
140
141         bkr->sa= biggest_image_area(CTX_wm_screen(C)); /* can be NULL */
142         bkr->scene= scene;
143         bkr->actob= (scene->r.bake_flag & R_BAKE_TO_ACTIVE) ? OBACT : NULL;
144         bkr->re= RE_NewRender("_Bake View_", RE_SLOT_DEFAULT);
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);
190         bkr->ready= 1;
191
192         return NULL;
193 }
194
195 static void bake_startjob(void *bkv, short *stop, short *do_update)
196 {
197         BakeRender *bkr= bkv;
198         Scene *scene= bkr->scene;
199
200         bkr->stop= stop;
201         bkr->do_update= do_update;
202
203         RE_test_break_cb(bkr->re, NULL, thread_break);
204         G.afbreek= 0;   /* blender_test_break uses this global */
205
206         RE_Database_Baking(bkr->re, scene, scene->lay, scene->r.bake_mode, bkr->actob);
207
208         /* baking itself is threaded, cannot use test_break in threads. we also update optional imagewindow */
209         bkr->tot= RE_bake_shade_all_selected(bkr->re, scene->r.bake_mode, bkr->actob, bkr->do_update);
210 }
211
212 static void bake_update(void *bkv)
213 {
214         BakeRender *bkr= bkv;
215
216         if(bkr->sa && bkr->sa->spacetype==SPACE_IMAGE) { /* incase the user changed while baking */
217                 SpaceImage *sima= bkr->sa->spacedata.first;
218                 if(sima)
219                         sima->image= RE_bake_shade_get_image();
220         }
221 }
222
223 static void bake_freejob(void *bkv)
224 {
225         BakeRender *bkr= bkv;
226         BLI_end_threads(&bkr->threads);
227         finish_bake_internal(bkr);
228
229         if(bkr->tot==0) BKE_report(bkr->reports, RPT_ERROR, "No Images found to bake to");
230         MEM_freeN(bkr);
231 }
232
233 /* catch esc */
234 static int objects_bake_render_modal(bContext *C, wmOperator *op, wmEvent *event)
235 {
236         /* no running blender, remove handler and pass through */
237         if(0==WM_jobs_test(CTX_wm_manager(C), CTX_data_scene(C)))
238                 return OPERATOR_FINISHED|OPERATOR_PASS_THROUGH;
239
240         /* running render */
241         switch (event->type) {
242                 case ESCKEY:
243                         return OPERATOR_RUNNING_MODAL;
244                         break;
245         }
246         return OPERATOR_PASS_THROUGH;
247 }
248
249 static int objects_bake_render_invoke(bContext *C, wmOperator *op, wmEvent *_event)
250 {
251         Scene *scene= CTX_data_scene(C);
252
253         if(test_bake_internal(C, op->reports)==0) {
254                 return OPERATOR_CANCELLED;
255         }
256         else {
257                 BakeRender *bkr= MEM_callocN(sizeof(BakeRender), "render bake");
258                 wmJob *steve;
259
260                 init_bake_internal(bkr, C);
261                 bkr->reports= op->reports;
262
263                 /* setup job */
264                 steve= WM_jobs_get(CTX_wm_manager(C), CTX_wm_window(C), scene, WM_JOB_EXCL_RENDER|WM_JOB_PRIORITY);
265                 WM_jobs_customdata(steve, bkr, bake_freejob);
266                 WM_jobs_timer(steve, 0.2, NC_IMAGE, 0); /* TODO - only draw bake image, can we enforce this */
267                 WM_jobs_callbacks(steve, bake_startjob, NULL, bake_update);
268
269                 G.afbreek= 0;
270
271                 WM_jobs_start(CTX_wm_manager(C), steve);
272
273                 WM_cursor_wait(0);
274                 WM_event_add_notifier(C, NC_SCENE|ND_RENDER_RESULT, scene);
275
276                 /* add modal handler for ESC */
277                 WM_event_add_modal_handler(C, op);
278         }
279
280         return OPERATOR_RUNNING_MODAL;
281 }
282
283
284 static int bake_image_exec(bContext *C, wmOperator *op)
285 {
286         Scene *scene= CTX_data_scene(C);
287
288
289         if(test_bake_internal(C, op->reports)==0) {
290                 return OPERATOR_CANCELLED;
291         }
292         else {
293                 ListBase threads;
294                 BakeRender bkr;
295
296                 memset(&bkr, 0, sizeof(bkr));
297
298                 init_bake_internal(&bkr, C);
299                 bkr.reports= op->reports;
300
301                 RE_test_break_cb(bkr.re, NULL, thread_break);
302                 G.afbreek= 0;   /* blender_test_break uses this global */
303
304                 RE_Database_Baking(bkr.re, scene, scene->lay, scene->r.bake_mode, (scene->r.bake_flag & R_BAKE_TO_ACTIVE)? OBACT: NULL);
305
306                 /* baking itself is threaded, cannot use test_break in threads  */
307                 BLI_init_threads(&threads, do_bake_render, 1);
308                 bkr.ready= 0;
309                 BLI_insert_thread(&threads, &bkr);
310
311                 while(bkr.ready==0) {
312                         PIL_sleep_ms(50);
313                         if(bkr.ready)
314                                 break;
315
316                         /* used to redraw in 2.4x but this is just for exec in 2.5 */
317                         if (!G.background)
318                                 blender_test_break();
319                 }
320                 BLI_end_threads(&threads);
321
322                 if(bkr.tot==0) BKE_report(op->reports, RPT_ERROR, "No Images found to bake to");
323
324                 finish_bake_internal(&bkr);
325         }
326
327         WM_event_add_notifier(C, NC_SCENE|ND_RENDER_RESULT, scene);
328         return OPERATOR_FINISHED;
329 }
330
331 void OBJECT_OT_bake_image(wmOperatorType *ot)
332 {
333         /* identifiers */
334         ot->name= "Bake";
335         ot->description= "Bake image textures of selected objects";
336         ot->idname= "OBJECT_OT_bake_image";
337
338         /* api callbacks */
339         ot->exec= bake_image_exec;
340         ot->invoke= objects_bake_render_invoke;
341         ot->modal= objects_bake_render_modal;
342 }