Progress indicators for threaded jobs
[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_object.h"
52 #include "BKE_utildefines.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
69 /* ****************** render BAKING ********************** */
70
71 /* threaded break test */
72 static int thread_break(void *unused)
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         Scene *scene;
98         struct Object *actob;
99         int tot, ready;
100
101         ReportList *reports;
102
103         short *stop;
104         short *do_update;
105         float *progress;
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_");
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
200         bkr->stop= stop;
201         bkr->do_update= do_update;
202         bkr->progress= progress;
203
204         RE_test_break_cb(bkr->re, NULL, thread_break);
205         G.afbreek= 0;   /* blender_test_break uses this global */
206
207         RE_Database_Baking(bkr->re, scene, scene->lay, scene->r.bake_mode, bkr->actob);
208
209         /* baking itself is threaded, cannot use test_break in threads. we also update optional imagewindow */
210         bkr->tot= RE_bake_shade_all_selected(bkr->re, scene->r.bake_mode, bkr->actob, bkr->do_update, bkr->progress);
211 }
212
213 static void bake_update(void *bkv)
214 {
215         BakeRender *bkr= bkv;
216
217         if(bkr->sa && bkr->sa->spacetype==SPACE_IMAGE) { /* incase the user changed while baking */
218                 SpaceImage *sima= bkr->sa->spacedata.first;
219                 if(sima)
220                         sima->image= RE_bake_shade_get_image();
221         }
222 }
223
224 static void bake_freejob(void *bkv)
225 {
226         BakeRender *bkr= bkv;
227         BLI_end_threads(&bkr->threads);
228         finish_bake_internal(bkr);
229
230         if(bkr->tot==0) BKE_report(bkr->reports, RPT_ERROR, "No Images found to bake to");
231         MEM_freeN(bkr);
232 }
233
234 /* catch esc */
235 static int objects_bake_render_modal(bContext *C, wmOperator *op, wmEvent *event)
236 {
237         /* no running blender, remove handler and pass through */
238         if(0==WM_jobs_test(CTX_wm_manager(C), CTX_data_scene(C)))
239                 return OPERATOR_FINISHED|OPERATOR_PASS_THROUGH;
240
241         /* running render */
242         switch (event->type) {
243                 case ESCKEY:
244                         return OPERATOR_RUNNING_MODAL;
245                         break;
246         }
247         return OPERATOR_PASS_THROUGH;
248 }
249
250 static int objects_bake_render_invoke(bContext *C, wmOperator *op, wmEvent *_event)
251 {
252         Scene *scene= CTX_data_scene(C);
253
254         if(test_bake_internal(C, op->reports)==0) {
255                 return OPERATOR_CANCELLED;
256         }
257         else {
258                 BakeRender *bkr= MEM_callocN(sizeof(BakeRender), "render bake");
259                 wmJob *steve;
260
261                 init_bake_internal(bkr, C);
262                 bkr->reports= op->reports;
263
264                 /* setup job */
265                 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);
266                 WM_jobs_customdata(steve, bkr, bake_freejob);
267                 WM_jobs_timer(steve, 0.2, NC_IMAGE, 0); /* TODO - only draw bake image, can we enforce this */
268                 WM_jobs_callbacks(steve, bake_startjob, NULL, bake_update, NULL);
269
270                 G.afbreek= 0;
271
272                 WM_jobs_start(CTX_wm_manager(C), steve);
273
274                 WM_cursor_wait(0);
275                 WM_event_add_notifier(C, NC_SCENE|ND_RENDER_RESULT, scene);
276
277                 /* add modal handler for ESC */
278                 WM_event_add_modal_handler(C, op);
279         }
280
281         return OPERATOR_RUNNING_MODAL;
282 }
283
284
285 static int bake_image_exec(bContext *C, wmOperator *op)
286 {
287         Scene *scene= CTX_data_scene(C);
288
289
290         if(test_bake_internal(C, op->reports)==0) {
291                 return OPERATOR_CANCELLED;
292         }
293         else {
294                 ListBase threads;
295                 BakeRender bkr;
296
297                 memset(&bkr, 0, sizeof(bkr));
298
299                 init_bake_internal(&bkr, C);
300                 bkr.reports= op->reports;
301
302                 RE_test_break_cb(bkr.re, NULL, thread_break);
303                 G.afbreek= 0;   /* blender_test_break uses this global */
304
305                 RE_Database_Baking(bkr.re, scene, scene->lay, scene->r.bake_mode, (scene->r.bake_flag & R_BAKE_TO_ACTIVE)? OBACT: NULL);
306
307                 /* baking itself is threaded, cannot use test_break in threads  */
308                 BLI_init_threads(&threads, do_bake_render, 1);
309                 bkr.ready= 0;
310                 BLI_insert_thread(&threads, &bkr);
311
312                 while(bkr.ready==0) {
313                         PIL_sleep_ms(50);
314                         if(bkr.ready)
315                                 break;
316
317                         /* used to redraw in 2.4x but this is just for exec in 2.5 */
318                         if (!G.background)
319                                 blender_test_break();
320                 }
321                 BLI_end_threads(&threads);
322
323                 if(bkr.tot==0) BKE_report(op->reports, RPT_ERROR, "No Images found to bake to");
324
325                 finish_bake_internal(&bkr);
326         }
327
328         WM_event_add_notifier(C, NC_SCENE|ND_RENDER_RESULT, scene);
329         return OPERATOR_FINISHED;
330 }
331
332 void OBJECT_OT_bake_image(wmOperatorType *ot)
333 {
334         /* identifiers */
335         ot->name= "Bake";
336         ot->description= "Bake image textures of selected objects";
337         ot->idname= "OBJECT_OT_bake_image";
338
339         /* api callbacks */
340         ot->exec= bake_image_exec;
341         ot->invoke= objects_bake_render_invoke;
342         ot->modal= objects_bake_render_modal;
343 }