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