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