Ambient occlusion baker from multi-resolution mesh
[blender.git] / source / blender / editors / object / object_bake.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2004 by Blender Foundation
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): Morten Mikkelsen,
24  *                 Sergey Sharybin
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 /** \file blender/editors/object/object_bake.c
30  *  \ingroup edobj
31  */
32
33 #include <string.h>
34
35 #include "MEM_guardedalloc.h"
36
37 #include "DNA_scene_types.h"
38 #include "DNA_screen_types.h"
39 #include "DNA_space_types.h"
40 #include "DNA_world_types.h"
41 #include "DNA_object_types.h"
42 #include "DNA_mesh_types.h"
43 #include "DNA_meshdata_types.h"
44
45 #include "BLI_blenlib.h"
46 #include "BLI_threads.h"
47 #include "BLI_utildefines.h"
48 #include "BLI_math.h"
49 #include "BLI_math_geom.h"
50
51 #include "BKE_blender.h"
52 #include "BKE_ccg.h"
53 #include "BKE_screen.h"
54 #include "BKE_context.h"
55 #include "BKE_global.h"
56 #include "BKE_image.h"
57 #include "BKE_main.h"
58 #include "BKE_multires.h"
59 #include "BKE_report.h"
60 #include "BKE_cdderivedmesh.h"
61 #include "BKE_modifier.h"
62 #include "BKE_DerivedMesh.h"
63 #include "BKE_subsurf.h"
64
65 #include "RE_pipeline.h"
66 #include "RE_shader_ext.h"
67 #include "RE_multires_bake.h"
68
69 #include "PIL_time.h"
70
71 #include "IMB_imbuf_types.h"
72 #include "IMB_imbuf.h"
73 #include "IMB_colormanagement.h"
74
75 #include "GPU_draw.h" /* GPU_free_image */
76
77 #include "WM_api.h"
78 #include "WM_types.h"
79
80 #include "ED_object.h"
81
82 #include "object_intern.h"
83
84 /* ****************** multires BAKING ********************** */
85
86 /* holder of per-object data needed for bake job
87  * needed to make job totally thread-safe */
88 typedef struct MultiresBakerJobData {
89         struct MultiresBakerJobData *next, *prev;
90         DerivedMesh *lores_dm, *hires_dm;
91         int simple, lvl, tot_lvl;
92 } MultiresBakerJobData;
93
94 /* data passing to multires-baker job */
95 typedef struct {
96         ListBase data;
97         int bake_clear, bake_filter;
98         short mode, use_lores_mesh;
99         int number_of_rays;
100         float bias;
101         int raytrace_structure;
102 } MultiresBakeJob;
103
104 static int multiresbake_check(bContext *C, wmOperator *op)
105 {
106         Scene *scene = CTX_data_scene(C);
107         Object *ob;
108         Mesh *me;
109         MultiresModifierData *mmd;
110         int ok = 1, a;
111
112         CTX_DATA_BEGIN (C, Base *, base, selected_editable_bases)
113         {
114                 ob = base->object;
115
116                 if (ob->type != OB_MESH) {
117                         BKE_report(op->reports, RPT_ERROR, "Baking of multires data only works with an active mesh object");
118
119                         ok = 0;
120                         break;
121                 }
122
123                 me = (Mesh *)ob->data;
124                 mmd = get_multires_modifier(scene, ob, 0);
125
126                 /* Multi-resolution should be and be last in the stack */
127                 if (ok && mmd) {
128                         ModifierData *md;
129
130                         ok = mmd->totlvl > 0;
131
132                         for (md = (ModifierData *)mmd->modifier.next; md && ok; md = md->next) {
133                                 if (modifier_isEnabled(scene, md, eModifierMode_Realtime)) {
134                                         ok = 0;
135                                 }
136                         }
137                 }
138                 else ok = 0;
139
140                 if (!ok) {
141                         BKE_report(op->reports, RPT_ERROR, "Multires data baking requires multi-resolution object");
142
143                         break;
144                 }
145
146                 if (mmd->lvl == 0) {
147                         BKE_report(op->reports, RPT_ERROR, "Multires data baking is not supported for preview subdivision level 0");
148
149                         break;
150                 }
151
152                 if (!me->mtpoly) {
153                         BKE_report(op->reports, RPT_ERROR, "Mesh should be unwrapped before multires data baking");
154
155                         ok = 0;
156                 }
157                 else {
158                         a = me->totpoly;
159                         while (ok && a--) {
160                                 Image *ima = me->mtpoly[a].tpage;
161
162                                 if (!ima) {
163                                         BKE_report(op->reports, RPT_ERROR, "You should have active texture to use multires baker");
164
165                                         ok = 0;
166                                 }
167                                 else {
168                                         ImBuf *ibuf = BKE_image_acquire_ibuf(ima, NULL, NULL);
169
170                                         if (!ibuf) {
171                                                 BKE_report(op->reports, RPT_ERROR, "Baking should happen to image with image buffer");
172
173                                                 ok = 0;
174                                         }
175                                         else {
176                                                 if (ibuf->rect == NULL && ibuf->rect_float == NULL)
177                                                         ok = 0;
178
179                                                 if (ibuf->rect_float && !(ibuf->channels == 0 || ibuf->channels == 4))
180                                                         ok = 0;
181
182                                                 if (!ok)
183                                                         BKE_report(op->reports, RPT_ERROR, "Baking to unsupported image type");
184                                         }
185
186                                         BKE_image_release_ibuf(ima, ibuf, NULL);
187                                 }
188                         }
189                 }
190
191                 if (!ok)
192                         break;
193         }
194         CTX_DATA_END;
195
196         return ok;
197 }
198
199 static DerivedMesh *multiresbake_create_loresdm(Scene *scene, Object *ob, int *lvl)
200 {
201         DerivedMesh *dm;
202         MultiresModifierData *mmd = get_multires_modifier(scene, ob, 0);
203         Mesh *me = (Mesh *)ob->data;
204
205         *lvl = mmd->lvl;
206
207         if (*lvl == 0) {
208                 DerivedMesh *tmp_dm = CDDM_from_mesh(me, ob);
209
210                 DM_set_only_copy(tmp_dm, CD_MASK_BAREMESH | CD_MASK_MTFACE);
211
212                 dm = CDDM_copy(tmp_dm);
213                 tmp_dm->release(tmp_dm);
214         }
215         else {
216                 MultiresModifierData tmp_mmd = *mmd;
217                 DerivedMesh *cddm = CDDM_from_mesh(me, ob);
218
219                 DM_set_only_copy(cddm, CD_MASK_BAREMESH | CD_MASK_MTFACE);
220
221                 tmp_mmd.lvl = *lvl;
222                 tmp_mmd.sculptlvl = *lvl;
223                 dm = multires_make_derived_from_derived(cddm, &tmp_mmd, ob, 0);
224                 cddm->release(cddm);
225         }
226
227         return dm;
228 }
229
230 static DerivedMesh *multiresbake_create_hiresdm(Scene *scene, Object *ob, int *lvl, int *simple)
231 {
232         Mesh *me = (Mesh *)ob->data;
233         MultiresModifierData *mmd = get_multires_modifier(scene, ob, 0);
234         MultiresModifierData tmp_mmd = *mmd;
235         DerivedMesh *cddm = CDDM_from_mesh(me, ob);
236         DerivedMesh *dm;
237
238         DM_set_only_copy(cddm, CD_MASK_BAREMESH);
239
240         /* TODO: DM_set_only_copy wouldn't set mask for loop and poly data,
241          *       but we really need BAREMESH only to save lots of memory
242          */
243         CustomData_set_only_copy(&cddm->loopData, CD_MASK_BAREMESH);
244         CustomData_set_only_copy(&cddm->polyData, CD_MASK_BAREMESH);
245
246         *lvl = mmd->totlvl;
247         *simple = mmd->simple;
248
249         tmp_mmd.lvl = mmd->totlvl;
250         tmp_mmd.sculptlvl = mmd->totlvl;
251         dm = multires_make_derived_from_derived(cddm, &tmp_mmd, ob, 0);
252         cddm->release(cddm);
253
254         return dm;
255 }
256
257 static void clear_images(MTFace *mtface, int totface)
258 {
259         int a;
260         const float vec_alpha[4] = {0.0f, 0.0f, 0.0f, 0.0f};
261         const float vec_solid[4] = {0.0f, 0.0f, 0.0f, 1.0f};
262
263         for (a = 0; a < totface; a++)
264                 mtface[a].tpage->id.flag &= ~LIB_DOIT;
265
266         for (a = 0; a < totface; a++) {
267                 Image *ima = mtface[a].tpage;
268
269                 if ((ima->id.flag & LIB_DOIT) == 0) {
270                         ImBuf *ibuf = BKE_image_acquire_ibuf(ima, NULL, NULL);
271
272                         IMB_rectfill(ibuf, (ibuf->planes == R_IMF_PLANES_RGBA) ? vec_alpha : vec_solid);
273                         ima->id.flag |= LIB_DOIT;
274
275                         BKE_image_release_ibuf(ima, ibuf, NULL);
276                 }
277         }
278
279         for (a = 0; a < totface; a++)
280                 mtface[a].tpage->id.flag &= ~LIB_DOIT;
281 }
282
283 static int multiresbake_image_exec_locked(bContext *C, wmOperator *op)
284 {
285         Object *ob;
286         Scene *scene = CTX_data_scene(C);
287         int objects_baked = 0;
288
289         if (!multiresbake_check(C, op))
290                 return OPERATOR_CANCELLED;
291
292         if (scene->r.bake_flag & R_BAKE_CLEAR) {  /* clear images */
293                 CTX_DATA_BEGIN (C, Base *, base, selected_editable_bases)
294                 {
295                         Mesh *me;
296
297                         ob = base->object;
298                         me = (Mesh *)ob->data;
299
300                         clear_images(me->mtface, me->totface);
301                 }
302                 CTX_DATA_END;
303         }
304
305         CTX_DATA_BEGIN (C, Base *, base, selected_editable_bases)
306         {
307                 MultiresBakeRender bkr = {0};
308
309                 ob = base->object;
310
311                 multires_force_update(ob);
312
313                 /* copy data stored in job descriptor */
314                 bkr.bake_filter = scene->r.bake_filter;
315                 bkr.mode = scene->r.bake_mode;
316                 bkr.use_lores_mesh = scene->r.bake_flag & R_BAKE_LORES_MESH;
317                 bkr.bias = scene->r.bake_biasdist;
318                 bkr.number_of_rays = scene->r.bake_rays_number;
319                 bkr.raytrace_structure = scene->r.raytrace_structure;
320
321                 /* create low-resolution DM (to bake to) and hi-resolution DM (to bake from) */
322                 bkr.hires_dm = multiresbake_create_hiresdm(scene, ob, &bkr.tot_lvl, &bkr.simple);
323                 bkr.lores_dm = multiresbake_create_loresdm(scene, ob, &bkr.lvl);
324
325                 RE_multires_bake_images(&bkr);
326
327                 BLI_freelistN(&bkr.image);
328
329                 bkr.lores_dm->release(bkr.lores_dm);
330                 bkr.hires_dm->release(bkr.hires_dm);
331
332                 objects_baked++;
333         }
334         CTX_DATA_END;
335
336         if (!objects_baked)
337                 BKE_report(op->reports, RPT_ERROR, "No objects found to bake from");
338
339         return OPERATOR_FINISHED;
340 }
341
342 /* Multiresbake adopted for job-system executing */
343 static void init_multiresbake_job(bContext *C, MultiresBakeJob *bkj)
344 {
345         Scene *scene = CTX_data_scene(C);
346         Object *ob;
347
348         /* backup scene settings, so their changing in UI would take no effect on baker */
349         bkj->bake_filter = scene->r.bake_filter;
350         bkj->mode = scene->r.bake_mode;
351         bkj->use_lores_mesh = scene->r.bake_flag & R_BAKE_LORES_MESH;
352         bkj->bake_clear = scene->r.bake_flag & R_BAKE_CLEAR;
353         bkj->bias = scene->r.bake_biasdist;
354         bkj->number_of_rays = scene->r.bake_rays_number;
355         bkj->raytrace_structure = scene->r.raytrace_structure;
356
357         CTX_DATA_BEGIN (C, Base *, base, selected_editable_bases)
358         {
359                 MultiresBakerJobData *data;
360                 int lvl;
361
362                 ob = base->object;
363
364                 multires_force_update(ob);
365
366                 data = MEM_callocN(sizeof(MultiresBakerJobData), "multiresBaker derivedMesh_data");
367
368                 /* create low-resolution DM (to bake to) and hi-resolution DM (to bake from) */
369                 data->hires_dm = multiresbake_create_hiresdm(scene, ob, &data->tot_lvl, &data->simple);
370                 data->lores_dm = multiresbake_create_loresdm(scene, ob, &lvl);
371                 data->lvl = lvl;
372
373                 BLI_addtail(&bkj->data, data);
374         }
375         CTX_DATA_END;
376 }
377
378 static void multiresbake_startjob(void *bkv, short *stop, short *do_update, float *progress)
379 {
380         MultiresBakerJobData *data;
381         MultiresBakeJob *bkj = bkv;
382         int baked_objects = 0, tot_obj;
383
384         tot_obj = BLI_countlist(&bkj->data);
385
386         if (bkj->bake_clear) {  /* clear images */
387                 for (data = bkj->data.first; data; data = data->next) {
388                         DerivedMesh *dm = data->lores_dm;
389                         MTFace *mtface = CustomData_get_layer(&dm->faceData, CD_MTFACE);
390
391                         clear_images(mtface, dm->getNumTessFaces(dm));
392                 }
393         }
394
395         for (data = bkj->data.first; data; data = data->next) {
396                 MultiresBakeRender bkr = {0};
397
398                 /* copy data stored in job descriptor */
399                 bkr.bake_filter = bkj->bake_filter;
400                 bkr.mode = bkj->mode;
401                 bkr.use_lores_mesh = bkj->use_lores_mesh;
402
403                 /* create low-resolution DM (to bake to) and hi-resolution DM (to bake from) */
404                 bkr.lores_dm = data->lores_dm;
405                 bkr.hires_dm = data->hires_dm;
406                 bkr.tot_lvl = data->tot_lvl;
407                 bkr.lvl = data->lvl;
408                 bkr.simple = data->simple;
409
410                 /* needed for proper progress bar */
411                 bkr.tot_obj = tot_obj;
412                 bkr.baked_objects = baked_objects;
413
414                 bkr.stop = stop;
415                 bkr.do_update = do_update;
416                 bkr.progress = progress;
417
418                 bkr.bias = bkj->bias;
419                 bkr.number_of_rays = bkj->number_of_rays;
420                 bkr.raytrace_structure = bkj->raytrace_structure;
421
422                 RE_multires_bake_images(&bkr);
423
424                 BLI_freelistN(&bkr.image);
425
426                 baked_objects++;
427         }
428 }
429
430 static void multiresbake_freejob(void *bkv)
431 {
432         MultiresBakeJob *bkj = bkv;
433         MultiresBakerJobData *data, *next;
434
435         data = bkj->data.first;
436         while (data) {
437                 next = data->next;
438                 data->lores_dm->release(data->lores_dm);
439                 data->hires_dm->release(data->hires_dm);
440                 MEM_freeN(data);
441                 data = next;
442         }
443
444         MEM_freeN(bkj);
445 }
446
447 static int multiresbake_image_exec(bContext *C, wmOperator *op)
448 {
449         Scene *scene = CTX_data_scene(C);
450         MultiresBakeJob *bkr;
451         wmJob *wm_job;
452
453         if (!multiresbake_check(C, op))
454                 return OPERATOR_CANCELLED;
455
456         bkr = MEM_callocN(sizeof(MultiresBakeJob), "MultiresBakeJob data");
457         init_multiresbake_job(C, bkr);
458
459         if (!bkr->data.first) {
460                 BKE_report(op->reports, RPT_ERROR, "No objects found to bake from");
461                 return OPERATOR_CANCELLED;
462         }
463
464         /* setup job */
465         wm_job = WM_jobs_get(CTX_wm_manager(C), CTX_wm_window(C), scene, "Multires Bake",
466                              WM_JOB_EXCL_RENDER | WM_JOB_PRIORITY | WM_JOB_PROGRESS, WM_JOB_TYPE_OBJECT_BAKE_TEXTURE);
467         WM_jobs_customdata_set(wm_job, bkr, multiresbake_freejob);
468         WM_jobs_timer(wm_job, 0.2, NC_IMAGE, 0); /* TODO - only draw bake image, can we enforce this */
469         WM_jobs_callbacks(wm_job, multiresbake_startjob, NULL, NULL, NULL);
470
471         G.is_break = FALSE;
472
473         WM_jobs_start(CTX_wm_manager(C), wm_job);
474         WM_cursor_wait(0);
475
476         /* add modal handler for ESC */
477         WM_event_add_modal_handler(C, op);
478
479         return OPERATOR_RUNNING_MODAL;
480 }
481
482 /* ****************** render BAKING ********************** */
483
484 /* threaded break test */
485 static int thread_break(void *UNUSED(arg))
486 {
487         return G.is_break;
488 }
489
490 typedef struct BakeRender {
491         Render *re;
492         Main *main;
493         Scene *scene;
494         struct Object *actob;
495         int result, ready;
496
497         ReportList *reports;
498
499         short *stop;
500         short *do_update;
501         float *progress;
502         
503         ListBase threads;
504
505         /* backup */
506         short prev_wo_amb_occ;
507         short prev_r_raytrace;
508
509         /* for redrawing */
510         ScrArea *sa;
511 } BakeRender;
512
513 /* use by exec and invoke */
514 static int test_bake_internal(bContext *C, ReportList *reports)
515 {
516         Scene *scene = CTX_data_scene(C);
517
518         if ((scene->r.bake_flag & R_BAKE_TO_ACTIVE) && CTX_data_active_object(C) == NULL) {
519                 BKE_report(reports, RPT_ERROR, "No active object");
520         }
521         else if (scene->r.bake_mode == RE_BAKE_AO && scene->world == NULL) {
522                 BKE_report(reports, RPT_ERROR, "No world set up");
523         }
524         else {
525                 return 1;
526         }
527
528         return 0;
529 }
530
531 static void init_bake_internal(BakeRender *bkr, bContext *C)
532 {
533         Scene *scene = CTX_data_scene(C);
534         bScreen *sc = CTX_wm_screen(C);
535
536         /* get editmode results */
537         ED_object_exit_editmode(C, 0);  /* 0 = does not exit editmode */
538
539         bkr->sa = sc ? BKE_screen_find_big_area(sc, SPACE_IMAGE, 10) : NULL; /* can be NULL */
540         bkr->main = CTX_data_main(C);
541         bkr->scene = scene;
542         bkr->actob = (scene->r.bake_flag & R_BAKE_TO_ACTIVE) ? OBACT : NULL;
543         bkr->re = RE_NewRender("_Bake View_");
544
545         if (scene->r.bake_mode == RE_BAKE_AO) {
546                 /* If raytracing or AO is disabled, switch it on temporarily for baking. */
547                 bkr->prev_wo_amb_occ = (scene->world->mode & WO_AMB_OCC) != 0;
548                 scene->world->mode |= WO_AMB_OCC;
549         }
550         if (scene->r.bake_mode == RE_BAKE_AO || bkr->actob) {
551                 bkr->prev_r_raytrace = (scene->r.mode & R_RAYTRACE) != 0;
552                 scene->r.mode |= R_RAYTRACE;
553         }
554 }
555
556 static void finish_bake_internal(BakeRender *bkr)
557 {
558         Image *ima;
559
560         RE_Database_Free(bkr->re);
561
562         /* restore raytrace and AO */
563         if (bkr->scene->r.bake_mode == RE_BAKE_AO)
564                 if (bkr->prev_wo_amb_occ == 0)
565                         bkr->scene->world->mode &= ~WO_AMB_OCC;
566
567         if (bkr->scene->r.bake_mode == RE_BAKE_AO || bkr->actob)
568                 if (bkr->prev_r_raytrace == 0)
569                         bkr->scene->r.mode &= ~R_RAYTRACE;
570
571         /* force OpenGL reload and mipmap recalc */
572         for (ima = G.main->image.first; ima; ima = ima->id.next) {
573                 ImBuf *ibuf = BKE_image_acquire_ibuf(ima, NULL, NULL);
574
575                 /* some of the images could have been changed during bake,
576                  * so recreate mipmaps regardless bake result status
577                  */
578                 if (ima->ok == IMA_OK_LOADED) {
579                         if (ibuf) {
580                                 if (ibuf->userflags & IB_BITMAPDIRTY) {
581                                         GPU_free_image(ima);
582                                         imb_freemipmapImBuf(ibuf);
583                                 }
584
585                                 /* invalidate display buffers for changed images */
586                                 if (ibuf->userflags & IB_BITMAPDIRTY)
587                                         ibuf->userflags |= IB_DISPLAY_BUFFER_INVALID;
588                         }
589                 }
590
591                 /* freed when baking is done, but if its canceled we need to free here */
592                 if (ibuf) {
593                         if (ibuf->userdata) {
594                                 MEM_freeN(ibuf->userdata);
595                                 ibuf->userdata = NULL;
596                         }
597                 }
598
599                 BKE_image_release_ibuf(ima, ibuf, NULL);
600         }
601 }
602
603 static void *do_bake_render(void *bake_v)
604 {
605         BakeRender *bkr = bake_v;
606
607         bkr->result = RE_bake_shade_all_selected(bkr->re, bkr->scene->r.bake_mode, bkr->actob, NULL, bkr->progress);
608         bkr->ready = 1;
609
610         return NULL;
611 }
612
613 static void bake_startjob(void *bkv, short *stop, short *do_update, float *progress)
614 {
615         BakeRender *bkr = bkv;
616         Scene *scene = bkr->scene;
617         Main *bmain = bkr->main;
618
619         bkr->stop = stop;
620         bkr->do_update = do_update;
621         bkr->progress = progress;
622
623         RE_test_break_cb(bkr->re, NULL, thread_break);
624         G.is_break = FALSE;   /* blender_test_break uses this global */
625
626         RE_Database_Baking(bkr->re, bmain, scene, scene->lay, scene->r.bake_mode, bkr->actob);
627
628         /* baking itself is threaded, cannot use test_break in threads. we also update optional imagewindow */
629         bkr->result = RE_bake_shade_all_selected(bkr->re, scene->r.bake_mode, bkr->actob, bkr->do_update, bkr->progress);
630 }
631
632 static void bake_update(void *bkv)
633 {
634         BakeRender *bkr = bkv;
635
636         if (bkr->sa && bkr->sa->spacetype == SPACE_IMAGE) { /* in case the user changed while baking */
637                 SpaceImage *sima = bkr->sa->spacedata.first;
638                 if (sima)
639                         sima->image = RE_bake_shade_get_image();
640         }
641 }
642
643 static void bake_freejob(void *bkv)
644 {
645         BakeRender *bkr = bkv;
646         finish_bake_internal(bkr);
647
648         if (bkr->result == BAKE_RESULT_NO_OBJECTS)
649                 BKE_report(bkr->reports, RPT_ERROR, "No objects or images found to bake to");
650         else if (bkr->result == BAKE_RESULT_FEEDBACK_LOOP)
651                 BKE_report(bkr->reports, RPT_WARNING, "Feedback loop detected");
652
653         MEM_freeN(bkr);
654         G.is_rendering = FALSE;
655 }
656
657 /* catch esc */
658 static int objects_bake_render_modal(bContext *C, wmOperator *UNUSED(op), wmEvent *event)
659 {
660         /* no running blender, remove handler and pass through */
661         if (0 == WM_jobs_test(CTX_wm_manager(C), CTX_data_scene(C), WM_JOB_TYPE_OBJECT_BAKE_TEXTURE))
662                 return OPERATOR_FINISHED | OPERATOR_PASS_THROUGH;
663
664         /* running render */
665         switch (event->type) {
666                 case ESCKEY:
667                         return OPERATOR_RUNNING_MODAL;
668                         break;
669         }
670         return OPERATOR_PASS_THROUGH;
671 }
672
673 static int is_multires_bake(Scene *scene)
674 {
675         if (ELEM3(scene->r.bake_mode, RE_BAKE_NORMALS, RE_BAKE_DISPLACEMENT, RE_BAKE_AO))
676                 return scene->r.bake_flag & R_BAKE_MULTIRES;
677
678         return 0;
679 }
680
681 static int objects_bake_render_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(_event))
682 {
683         Scene *scene = CTX_data_scene(C);
684         int result = OPERATOR_CANCELLED;
685
686         if (is_multires_bake(scene)) {
687                 result = multiresbake_image_exec(C, op);
688         }
689         else {
690                 /* only one render job at a time */
691                 if (WM_jobs_test(CTX_wm_manager(C), scene, WM_JOB_TYPE_OBJECT_BAKE_TEXTURE))
692                         return OPERATOR_CANCELLED;
693
694                 if (test_bake_internal(C, op->reports) == 0) {
695                         return OPERATOR_CANCELLED;
696                 }
697                 else {
698                         BakeRender *bkr = MEM_callocN(sizeof(BakeRender), "render bake");
699                         wmJob *wm_job;
700
701                         init_bake_internal(bkr, C);
702                         bkr->reports = op->reports;
703
704                         /* setup job */
705                         wm_job = WM_jobs_get(CTX_wm_manager(C), CTX_wm_window(C), scene, "Texture Bake",
706                                              WM_JOB_EXCL_RENDER | WM_JOB_PRIORITY | WM_JOB_PROGRESS, WM_JOB_TYPE_OBJECT_BAKE_TEXTURE);
707                         WM_jobs_customdata_set(wm_job, bkr, bake_freejob);
708                         WM_jobs_timer(wm_job, 0.2, NC_IMAGE, 0); /* TODO - only draw bake image, can we enforce this */
709                         WM_jobs_callbacks(wm_job, bake_startjob, NULL, bake_update, NULL);
710
711                         G.is_break = FALSE;
712                         G.is_rendering = TRUE;
713
714                         WM_jobs_start(CTX_wm_manager(C), wm_job);
715
716                         WM_cursor_wait(0);
717
718                         /* add modal handler for ESC */
719                         WM_event_add_modal_handler(C, op);
720                 }
721
722                 result = OPERATOR_RUNNING_MODAL;
723         }
724
725         WM_event_add_notifier(C, NC_SCENE | ND_RENDER_RESULT, scene);
726
727         return result;
728 }
729
730
731 static int bake_image_exec(bContext *C, wmOperator *op)
732 {
733         Main *bmain = CTX_data_main(C);
734         Scene *scene = CTX_data_scene(C);
735         int result = OPERATOR_CANCELLED;
736
737         if (is_multires_bake(scene)) {
738                 result = multiresbake_image_exec_locked(C, op);
739         }
740         else {
741                 if (test_bake_internal(C, op->reports) == 0) {
742                         return OPERATOR_CANCELLED;
743                 }
744                 else {
745                         ListBase threads;
746                         BakeRender bkr = {NULL};
747
748                         init_bake_internal(&bkr, C);
749                         bkr.reports = op->reports;
750
751                         RE_test_break_cb(bkr.re, NULL, thread_break);
752                         G.is_break = FALSE;   /* blender_test_break uses this global */
753
754                         RE_Database_Baking(bkr.re, bmain, scene, scene->lay, scene->r.bake_mode, (scene->r.bake_flag & R_BAKE_TO_ACTIVE) ? OBACT : NULL);
755
756                         /* baking itself is threaded, cannot use test_break in threads  */
757                         BLI_init_threads(&threads, do_bake_render, 1);
758                         bkr.ready = 0;
759                         BLI_insert_thread(&threads, &bkr);
760
761                         while (bkr.ready == 0) {
762                                 PIL_sleep_ms(50);
763                                 if (bkr.ready)
764                                         break;
765
766                                 /* used to redraw in 2.4x but this is just for exec in 2.5 */
767                                 if (!G.background)
768                                         blender_test_break();
769                         }
770                         BLI_end_threads(&threads);
771
772                         if (bkr.result == BAKE_RESULT_NO_OBJECTS)
773                                 BKE_report(op->reports, RPT_ERROR, "No valid images found to bake to");
774                         else if (bkr.result == BAKE_RESULT_FEEDBACK_LOOP)
775                                 BKE_report(op->reports, RPT_ERROR, "Feedback loop detected");
776
777                         finish_bake_internal(&bkr);
778
779                         result = OPERATOR_FINISHED;
780                 }
781         }
782
783         WM_event_add_notifier(C, NC_SCENE | ND_RENDER_RESULT, scene);
784
785         return result;
786 }
787
788 void OBJECT_OT_bake_image(wmOperatorType *ot)
789 {
790         /* identifiers */
791         ot->name = "Bake";
792         ot->description = "Bake image textures of selected objects";
793         ot->idname = "OBJECT_OT_bake_image";
794
795         /* api callbacks */
796         ot->exec = bake_image_exec;
797         ot->invoke = objects_bake_render_invoke;
798         ot->modal = objects_bake_render_modal;
799 }