Rename any instance of scene layer or render layer in code with view layer
[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
49 #include "BKE_blender.h"
50 #include "BKE_screen.h"
51 #include "BKE_context.h"
52 #include "BKE_global.h"
53 #include "BKE_image.h"
54 #include "BKE_main.h"
55 #include "BKE_material.h"
56 #include "BKE_multires.h"
57 #include "BKE_report.h"
58 #include "BKE_cdderivedmesh.h"
59 #include "BKE_modifier.h"
60 #include "BKE_DerivedMesh.h"
61 #include "BKE_mesh.h"
62 #include "BKE_scene.h"
63
64 #include "DEG_depsgraph.h"
65
66 #include "RE_pipeline.h"
67 #include "RE_shader_ext.h"
68 #include "RE_multires_bake.h"
69
70 #include "PIL_time.h"
71
72 #include "IMB_imbuf_types.h"
73 #include "IMB_imbuf.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 #include "ED_screen.h"
82
83 #include "object_intern.h"
84
85 /* ****************** multires BAKING ********************** */
86
87 /* holder of per-object data needed for bake job
88  * needed to make job totally thread-safe */
89 typedef struct MultiresBakerJobData {
90         struct MultiresBakerJobData *next, *prev;
91         /* material aligned image array (for per-face bake image) */
92         struct {
93                 Image **array;
94                 int     len;
95         } ob_image;
96         DerivedMesh *lores_dm, *hires_dm;
97         bool simple;
98         int lvl, tot_lvl;
99         ListBase images;
100 } MultiresBakerJobData;
101
102 /* data passing to multires-baker job */
103 typedef struct {
104         ListBase data;
105         bool bake_clear;      /* Clear the images before baking */
106         int bake_filter;      /* Bake-filter, aka margin */
107         short mode;           /* mode of baking (displacement, normals, AO) */
108         bool use_lores_mesh;  /* Use low-resolution mesh when baking displacement maps */
109         int number_of_rays;   /* Number of rays to be cast when doing AO baking */
110         float bias;           /* Bias between object and start ray point when doing AO baking */
111         int raytrace_structure;  /* Optimization structure to be used for AO baking */
112         int octree_resolution;   /* Reslution of octotree when using octotree optimization structure */
113         int threads;             /* Number of threads to be used for baking */
114         float user_scale;          /* User scale used to scale displacement when baking derivative map. */
115 } MultiresBakeJob;
116
117 static bool multiresbake_check(bContext *C, wmOperator *op)
118 {
119         Scene *scene = CTX_data_scene(C);
120         Object *ob;
121         Mesh *me;
122         MultiresModifierData *mmd;
123         bool ok = true;
124         int a;
125
126         CTX_DATA_BEGIN (C, Base *, base, selected_editable_bases)
127         {
128                 ob = base->object;
129
130                 if (ob->type != OB_MESH) {
131                         BKE_report(op->reports, RPT_ERROR, "Baking of multires data only works with an active mesh object");
132
133                         ok = false;
134                         break;
135                 }
136
137                 me = (Mesh *)ob->data;
138                 mmd = get_multires_modifier(scene, ob, 0);
139
140                 /* Multi-resolution should be and be last in the stack */
141                 if (ok && mmd) {
142                         ModifierData *md;
143
144                         ok = mmd->totlvl > 0;
145
146                         for (md = (ModifierData *)mmd->modifier.next; md && ok; md = md->next) {
147                                 if (modifier_isEnabled(scene, md, eModifierMode_Realtime)) {
148                                         ok = false;
149                                 }
150                         }
151                 }
152                 else {
153                         ok = false;
154                 }
155
156                 if (!ok) {
157                         BKE_report(op->reports, RPT_ERROR, "Multires data baking requires multi-resolution object");
158
159                         break;
160                 }
161
162                 if (!me->mloopuv) {
163                         BKE_report(op->reports, RPT_ERROR, "Mesh should be unwrapped before multires data baking");
164
165                         ok = false;
166                 }
167                 else {
168                         a = me->totpoly;
169                         while (ok && a--) {
170                                 Image *ima = BKE_object_material_edit_image_get(ob, me->mpoly[a].mat_nr);
171
172                                 if (!ima) {
173                                         BKE_report(op->reports, RPT_ERROR, "You should have active texture to use multires baker");
174
175                                         ok = false;
176                                 }
177                                 else {
178                                         ImBuf *ibuf = BKE_image_acquire_ibuf(ima, NULL, NULL);
179
180                                         if (!ibuf) {
181                                                 BKE_report(op->reports, RPT_ERROR, "Baking should happen to image with image buffer");
182
183                                                 ok = false;
184                                         }
185                                         else {
186                                                 if (ibuf->rect == NULL && ibuf->rect_float == NULL)
187                                                         ok = false;
188
189                                                 if (ibuf->rect_float && !(ibuf->channels == 0 || ibuf->channels == 4))
190                                                         ok = false;
191
192                                                 if (!ok)
193                                                         BKE_report(op->reports, RPT_ERROR, "Baking to unsupported image type");
194                                         }
195
196                                         BKE_image_release_ibuf(ima, ibuf, NULL);
197                                 }
198                         }
199                 }
200
201                 if (!ok)
202                         break;
203         }
204         CTX_DATA_END;
205
206         return ok;
207 }
208
209 static DerivedMesh *multiresbake_create_loresdm(Scene *scene, Object *ob, int *lvl)
210 {
211         DerivedMesh *dm;
212         MultiresModifierData *mmd = get_multires_modifier(scene, ob, 0);
213         Mesh *me = (Mesh *)ob->data;
214         MultiresModifierData tmp_mmd = *mmd;
215         DerivedMesh *cddm = CDDM_from_mesh(me);
216
217         if (mmd->lvl > 0) {
218                 *lvl = mmd->lvl;
219         }
220         else {
221                 *lvl = 1;
222                 tmp_mmd.simple = true;
223         }
224
225         DM_set_only_copy(cddm, CD_MASK_BAREMESH);
226
227         tmp_mmd.lvl = *lvl;
228         tmp_mmd.sculptlvl = *lvl;
229         dm = multires_make_derived_from_derived(cddm, &tmp_mmd, ob, 0);
230         cddm->release(cddm);
231
232         return dm;
233 }
234
235 static DerivedMesh *multiresbake_create_hiresdm(Scene *scene, Object *ob, int *lvl, bool *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);
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 != 0;
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_TANGENT_NORMAL = 1,
264         CLEAR_DISPLACEMENT = 2
265 } ClearFlag;
266
267
268 static void clear_single_image(Image *image, ClearFlag flag)
269 {
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         const float disp_alpha[4] = {0.5f, 0.5f, 0.5f, 0.0f};
275         const float disp_solid[4] = {0.5f, 0.5f, 0.5f, 1.0f};
276
277         if ((image->id.tag & LIB_TAG_DOIT) == 0) {
278                 ImBuf *ibuf = BKE_image_acquire_ibuf(image, NULL, NULL);
279
280                 if (flag == CLEAR_TANGENT_NORMAL)
281                         IMB_rectfill(ibuf, (ibuf->planes == R_IMF_PLANES_RGBA) ? nor_alpha : nor_solid);
282                 else if (flag == CLEAR_DISPLACEMENT)
283                         IMB_rectfill(ibuf, (ibuf->planes == R_IMF_PLANES_RGBA) ? disp_alpha : disp_solid);
284                 else
285                         IMB_rectfill(ibuf, (ibuf->planes == R_IMF_PLANES_RGBA) ? vec_alpha : vec_solid);
286
287                 image->id.tag |= LIB_TAG_DOIT;
288
289                 BKE_image_release_ibuf(image, ibuf, NULL);
290         }
291 }
292
293 static void clear_images_poly(Image **ob_image_array, int ob_image_array_len, ClearFlag flag)
294 {
295         for (int i = 0; i < ob_image_array_len; i++) {
296                 Image *image = ob_image_array[i];
297                 if (image) {
298                         image->id.tag &= ~LIB_TAG_DOIT;
299                 }
300         }
301
302         for (int i = 0; i < ob_image_array_len; i++) {
303                 Image *image = ob_image_array[i];
304                 if (image) {
305                         clear_single_image(image, flag);
306                 }
307         }
308
309         for (int i = 0; i < ob_image_array_len; i++) {
310                 Image *image = ob_image_array[i];
311                 if (image) {
312                         image->id.tag &= ~LIB_TAG_DOIT;
313                 }
314         }
315 }
316
317 static int multiresbake_image_exec_locked(bContext *C, wmOperator *op)
318 {
319         Object *ob;
320         Scene *scene = CTX_data_scene(C);
321         int objects_baked = 0;
322
323         if (!multiresbake_check(C, op))
324                 return OPERATOR_CANCELLED;
325
326         if (scene->r.bake_flag & R_BAKE_CLEAR) {  /* clear images */
327                 CTX_DATA_BEGIN (C, Base *, base, selected_editable_bases)
328                 {
329                         ClearFlag clear_flag = 0;
330
331                         ob = base->object;
332                         // me = (Mesh *)ob->data;
333
334                         if (scene->r.bake_mode == RE_BAKE_NORMALS) {
335                                 clear_flag = CLEAR_TANGENT_NORMAL;
336                         }
337                         else if (ELEM(scene->r.bake_mode, RE_BAKE_DISPLACEMENT, RE_BAKE_DERIVATIVE)) {
338                                 clear_flag = CLEAR_DISPLACEMENT;
339                         }
340
341                         {
342                                 Image **ob_image_array = BKE_object_material_edit_image_get_array(ob);
343                                 clear_images_poly(ob_image_array, ob->totcol, clear_flag);
344                                 MEM_freeN(ob_image_array);
345                         }
346                 }
347                 CTX_DATA_END;
348         }
349
350         CTX_DATA_BEGIN (C, Base *, base, selected_editable_bases)
351         {
352                 MultiresBakeRender bkr = {NULL};
353
354                 ob = base->object;
355
356                 multires_force_update(ob);
357
358                 /* copy data stored in job descriptor */
359                 bkr.bake_filter = scene->r.bake_filter;
360                 bkr.mode = scene->r.bake_mode;
361                 bkr.use_lores_mesh = scene->r.bake_flag & R_BAKE_LORES_MESH;
362                 bkr.bias = scene->r.bake_biasdist;
363                 bkr.number_of_rays = scene->r.bake_samples;
364                 bkr.raytrace_structure = scene->r.raytrace_structure;
365                 bkr.octree_resolution = scene->r.ocres;
366                 bkr.threads = BKE_scene_num_threads(scene);
367                 bkr.user_scale = (scene->r.bake_flag & R_BAKE_USERSCALE) ? scene->r.bake_user_scale : -1.0f;
368                 //bkr.reports= op->reports;
369
370                 /* create low-resolution DM (to bake to) and hi-resolution DM (to bake from) */
371                 bkr.ob_image.array = BKE_object_material_edit_image_get_array(ob);
372                 bkr.ob_image.len = ob->totcol;
373
374                 bkr.hires_dm = multiresbake_create_hiresdm(scene, ob, &bkr.tot_lvl, &bkr.simple);
375                 bkr.lores_dm = multiresbake_create_loresdm(scene, ob, &bkr.lvl);
376
377                 RE_multires_bake_images(&bkr);
378
379                 MEM_freeN(bkr.ob_image.array);
380
381                 BLI_freelistN(&bkr.image);
382
383                 bkr.lores_dm->release(bkr.lores_dm);
384                 bkr.hires_dm->release(bkr.hires_dm);
385
386                 objects_baked++;
387         }
388         CTX_DATA_END;
389
390         if (!objects_baked)
391                 BKE_report(op->reports, RPT_ERROR, "No objects found to bake from");
392
393         return OPERATOR_FINISHED;
394 }
395
396 /* Multiresbake adopted for job-system executing */
397 static void init_multiresbake_job(bContext *C, MultiresBakeJob *bkj)
398 {
399         Scene *scene = CTX_data_scene(C);
400         Object *ob;
401
402         /* backup scene settings, so their changing in UI would take no effect on baker */
403         bkj->bake_filter = scene->r.bake_filter;
404         bkj->mode = scene->r.bake_mode;
405         bkj->use_lores_mesh = scene->r.bake_flag & R_BAKE_LORES_MESH;
406         bkj->bake_clear = scene->r.bake_flag & R_BAKE_CLEAR;
407         bkj->bias = scene->r.bake_biasdist;
408         bkj->number_of_rays = scene->r.bake_samples;
409         bkj->raytrace_structure = scene->r.raytrace_structure;
410         bkj->octree_resolution = scene->r.ocres;
411         bkj->threads = BKE_scene_num_threads(scene);
412         bkj->user_scale = (scene->r.bake_flag & R_BAKE_USERSCALE) ? scene->r.bake_user_scale : -1.0f;
413         //bkj->reports = op->reports;
414
415         CTX_DATA_BEGIN (C, Base *, base, selected_editable_bases)
416         {
417                 MultiresBakerJobData *data;
418                 int lvl;
419
420                 ob = base->object;
421
422                 multires_force_update(ob);
423
424                 data = MEM_callocN(sizeof(MultiresBakerJobData), "multiresBaker derivedMesh_data");
425
426                 data->ob_image.array = BKE_object_material_edit_image_get_array(ob);
427                 data->ob_image.len = ob->totcol;
428
429                 /* create low-resolution DM (to bake to) and hi-resolution DM (to bake from) */
430                 data->hires_dm = multiresbake_create_hiresdm(scene, ob, &data->tot_lvl, &data->simple);
431                 data->lores_dm = multiresbake_create_loresdm(scene, ob, &lvl);
432                 data->lvl = lvl;
433
434                 BLI_addtail(&bkj->data, data);
435         }
436         CTX_DATA_END;
437 }
438
439 static void multiresbake_startjob(void *bkv, short *stop, short *do_update, float *progress)
440 {
441         MultiresBakerJobData *data;
442         MultiresBakeJob *bkj = bkv;
443         int baked_objects = 0, tot_obj;
444
445         tot_obj = BLI_listbase_count(&bkj->data);
446
447         if (bkj->bake_clear) {  /* clear images */
448                 for (data = bkj->data.first; data; data = data->next) {
449                         ClearFlag clear_flag = 0;
450
451                         if (bkj->mode == RE_BAKE_NORMALS) {
452                                 clear_flag = CLEAR_TANGENT_NORMAL;
453                         }
454                         else if (ELEM(bkj->mode, RE_BAKE_DISPLACEMENT, RE_BAKE_DERIVATIVE)) {
455                                 clear_flag = CLEAR_DISPLACEMENT;
456                         }
457
458                         clear_images_poly(data->ob_image.array, data->ob_image.len, clear_flag);
459                 }
460         }
461
462         for (data = bkj->data.first; data; data = data->next) {
463                 MultiresBakeRender bkr = {NULL};
464
465                 /* copy data stored in job descriptor */
466                 bkr.bake_filter = bkj->bake_filter;
467                 bkr.mode = bkj->mode;
468                 bkr.use_lores_mesh = bkj->use_lores_mesh;
469                 bkr.user_scale = bkj->user_scale;
470                 //bkr.reports = bkj->reports;
471                 bkr.ob_image.array = data->ob_image.array;
472                 bkr.ob_image.len   = data->ob_image.len;
473
474                 /* create low-resolution DM (to bake to) and hi-resolution DM (to bake from) */
475                 bkr.lores_dm = data->lores_dm;
476                 bkr.hires_dm = data->hires_dm;
477                 bkr.tot_lvl = data->tot_lvl;
478                 bkr.lvl = data->lvl;
479                 bkr.simple = data->simple;
480
481                 /* needed for proper progress bar */
482                 bkr.tot_obj = tot_obj;
483                 bkr.baked_objects = baked_objects;
484
485                 bkr.stop = stop;
486                 bkr.do_update = do_update;
487                 bkr.progress = progress;
488
489                 bkr.bias = bkj->bias;
490                 bkr.number_of_rays = bkj->number_of_rays;
491                 bkr.raytrace_structure = bkj->raytrace_structure;
492                 bkr.octree_resolution = bkj->octree_resolution;
493                 bkr.threads = bkj->threads;
494
495                 RE_multires_bake_images(&bkr);
496
497                 data->images = bkr.image;
498
499                 baked_objects++;
500         }
501 }
502
503 static void multiresbake_freejob(void *bkv)
504 {
505         MultiresBakeJob *bkj = bkv;
506         MultiresBakerJobData *data, *next;
507         LinkData *link;
508
509         data = bkj->data.first;
510         while (data) {
511                 next = data->next;
512                 data->lores_dm->release(data->lores_dm);
513                 data->hires_dm->release(data->hires_dm);
514
515                 /* delete here, since this delete will be called from main thread */
516                 for (link = data->images.first; link; link = link->next) {
517                         Image *ima = (Image *)link->data;
518                         GPU_free_image(ima);
519                 }
520
521                 MEM_freeN(data->ob_image.array);
522
523                 BLI_freelistN(&data->images);
524
525                 MEM_freeN(data);
526                 data = next;
527         }
528
529         MEM_freeN(bkj);
530 }
531
532 static int multiresbake_image_exec(bContext *C, wmOperator *op)
533 {
534         Scene *scene = CTX_data_scene(C);
535         MultiresBakeJob *bkr;
536         wmJob *wm_job;
537
538         if (!multiresbake_check(C, op))
539                 return OPERATOR_CANCELLED;
540
541         bkr = MEM_callocN(sizeof(MultiresBakeJob), "MultiresBakeJob data");
542         init_multiresbake_job(C, bkr);
543
544         if (!bkr->data.first) {
545                 BKE_report(op->reports, RPT_ERROR, "No objects found to bake from");
546                 return OPERATOR_CANCELLED;
547         }
548
549         /* setup job */
550         wm_job = WM_jobs_get(CTX_wm_manager(C), CTX_wm_window(C), scene, "Multires Bake",
551                              WM_JOB_EXCL_RENDER | WM_JOB_PRIORITY | WM_JOB_PROGRESS, WM_JOB_TYPE_OBJECT_BAKE_TEXTURE);
552         WM_jobs_customdata_set(wm_job, bkr, multiresbake_freejob);
553         WM_jobs_timer(wm_job, 0.5, NC_IMAGE, 0); /* TODO - only draw bake image, can we enforce this */
554         WM_jobs_callbacks(wm_job, multiresbake_startjob, NULL, NULL, NULL);
555
556         G.is_break = false;
557
558         WM_jobs_start(CTX_wm_manager(C), wm_job);
559         WM_cursor_wait(0);
560
561         /* add modal handler for ESC */
562         WM_event_add_modal_handler(C, op);
563
564         return OPERATOR_RUNNING_MODAL;
565 }
566
567 /* ****************** render BAKING ********************** */
568
569 /* threaded break test */
570 static int thread_break(void *UNUSED(arg))
571 {
572         return G.is_break;
573 }
574
575 typedef struct BakeRender {
576         Render *re;
577         Main *main;
578         Scene *scene;
579         struct Object *actob;
580         int result, ready;
581
582         ReportList *reports;
583
584         short *stop;
585         short *do_update;
586         float *progress;
587         
588         ListBase threads;
589
590         /* backup */
591         short prev_wo_amb_occ;
592         short prev_r_raytrace;
593
594         /* for redrawing */
595         ScrArea *sa;
596 } BakeRender;
597
598 /* use by exec and invoke */
599 static int test_bake_internal(bContext *C, ReportList *reports)
600 {
601         Scene *scene = CTX_data_scene(C);
602
603         if ((scene->r.bake_flag & R_BAKE_TO_ACTIVE) && CTX_data_active_object(C) == NULL) {
604                 BKE_report(reports, RPT_ERROR, "No active object");
605         }
606         else if (scene->r.bake_mode == RE_BAKE_AO && scene->world == NULL) {
607                 BKE_report(reports, RPT_ERROR, "No world set up");
608         }
609         else {
610                 return 1;
611         }
612
613         return 0;
614 }
615
616 static void init_bake_internal(BakeRender *bkr, bContext *C)
617 {
618         Scene *scene = CTX_data_scene(C);
619         ViewLayer *sl = CTX_data_view_layer(C);
620         bScreen *sc = CTX_wm_screen(C);
621
622         /* get editmode results */
623         ED_object_editmode_load(CTX_data_edit_object(C));
624
625         bkr->sa = sc ? BKE_screen_find_big_area(sc, SPACE_IMAGE, 10) : NULL; /* can be NULL */
626         bkr->main = CTX_data_main(C);
627         bkr->scene = scene;
628         bkr->actob = (scene->r.bake_flag & R_BAKE_TO_ACTIVE) ? OBACT(sl) : NULL;
629         bkr->re = RE_NewRender("_Bake View_");
630
631         if (scene->r.bake_mode == RE_BAKE_AO) {
632                 /* If raytracing or AO is disabled, switch it on temporarily for baking. */
633                 bkr->prev_wo_amb_occ = (scene->world->mode & WO_AMB_OCC) != 0;
634                 scene->world->mode |= WO_AMB_OCC;
635         }
636         if (scene->r.bake_mode == RE_BAKE_AO || bkr->actob) {
637                 bkr->prev_r_raytrace = (scene->r.mode & R_RAYTRACE) != 0;
638                 scene->r.mode |= R_RAYTRACE;
639         }
640 }
641
642 static void finish_bake_internal(BakeRender *bkr)
643 {
644         Image *ima;
645
646         RE_Database_Free(bkr->re);
647
648         /* restore raytrace and AO */
649         if (bkr->scene->r.bake_mode == RE_BAKE_AO)
650                 if (bkr->prev_wo_amb_occ == 0)
651                         bkr->scene->world->mode &= ~WO_AMB_OCC;
652
653         if (bkr->scene->r.bake_mode == RE_BAKE_AO || bkr->actob)
654                 if (bkr->prev_r_raytrace == 0)
655                         bkr->scene->r.mode &= ~R_RAYTRACE;
656
657         /* force OpenGL reload and mipmap recalc */
658         if ((bkr->scene->r.bake_flag & R_BAKE_VCOL) == 0) {
659                 for (ima = G.main->image.first; ima; ima = ima->id.next) {
660                         ImBuf *ibuf = BKE_image_acquire_ibuf(ima, NULL, NULL);
661
662                         /* some of the images could have been changed during bake,
663                          * so recreate mipmaps regardless bake result status
664                          */
665                         if (ima->ok == IMA_OK_LOADED) {
666                                 if (ibuf) {
667                                         if (ibuf->userflags & IB_BITMAPDIRTY) {
668                                                 GPU_free_image(ima);
669                                                 imb_freemipmapImBuf(ibuf);
670                                         }
671
672                                         /* invalidate display buffers for changed images */
673                                         if (ibuf->userflags & IB_BITMAPDIRTY)
674                                                 ibuf->userflags |= IB_DISPLAY_BUFFER_INVALID;
675                                 }
676                         }
677
678                         /* freed when baking is done, but if its canceled we need to free here */
679                         if (ibuf) {
680                                 if (ibuf->userdata) {
681                                         BakeImBufuserData *userdata = (BakeImBufuserData *) ibuf->userdata;
682                                         if (userdata->mask_buffer)
683                                                 MEM_freeN(userdata->mask_buffer);
684                                         if (userdata->displacement_buffer)
685                                                 MEM_freeN(userdata->displacement_buffer);
686                                         MEM_freeN(userdata);
687                                         ibuf->userdata = NULL;
688                                 }
689                         }
690
691                         BKE_image_release_ibuf(ima, ibuf, NULL);
692                         DEG_id_tag_update(&ima->id, 0);                 
693                 }
694         }
695
696         if (bkr->scene->r.bake_flag & R_BAKE_VCOL) {
697                 /* update all tagged meshes */
698                 Mesh *me;
699                 BLI_assert(BLI_thread_is_main());
700                 for (me = G.main->mesh.first; me; me = me->id.next) {
701                         if (me->id.tag & LIB_TAG_DOIT) {
702                                 DEG_id_tag_update(&me->id, OB_RECALC_DATA);
703                                 BKE_mesh_tessface_clear(me);
704                         }
705                 }
706         }
707
708 }
709
710 static void *do_bake_render(void *bake_v)
711 {
712         BakeRender *bkr = bake_v;
713
714         bkr->result = RE_bake_shade_all_selected(bkr->re, bkr->scene->r.bake_mode, bkr->actob, NULL, bkr->progress);
715         bkr->ready = 1;
716
717         return NULL;
718 }
719
720 static void bake_startjob(void *bkv, short *stop, short *do_update, float *progress)
721 {
722         BakeRender *bkr = bkv;
723         Scene *scene = bkr->scene;
724         Main *bmain = bkr->main;
725
726         bkr->stop = stop;
727         bkr->do_update = do_update;
728         bkr->progress = progress;
729
730         RE_test_break_cb(bkr->re, NULL, thread_break);
731         G.is_break = false;   /* BKE_blender_test_break uses this global */
732
733         RE_Database_Baking(bkr->re, bmain, scene, scene->lay, scene->r.bake_mode, bkr->actob);
734
735         /* baking itself is threaded, cannot use test_break in threads. we also update optional imagewindow */
736         bkr->result = RE_bake_shade_all_selected(bkr->re, scene->r.bake_mode, bkr->actob, bkr->do_update, bkr->progress);
737 }
738
739 static void bake_update(void *bkv)
740 {
741         BakeRender *bkr = bkv;
742
743         if (bkr->sa && bkr->sa->spacetype == SPACE_IMAGE) { /* in case the user changed while baking */
744                 SpaceImage *sima = bkr->sa->spacedata.first;
745                 if (sima)
746                         sima->image = RE_bake_shade_get_image();
747         }
748 }
749
750 static void bake_freejob(void *bkv)
751 {
752         BakeRender *bkr = bkv;
753         finish_bake_internal(bkr);
754
755         if (bkr->result == BAKE_RESULT_NO_OBJECTS)
756                 BKE_report(bkr->reports, RPT_ERROR, "No objects or images found to bake to");
757         else if (bkr->result == BAKE_RESULT_FEEDBACK_LOOP)
758                 BKE_report(bkr->reports, RPT_WARNING, "Circular reference in texture stack");
759
760         MEM_freeN(bkr);
761         G.is_rendering = false;
762 }
763
764 /* catch esc */
765 static int objects_bake_render_modal(bContext *C, wmOperator *UNUSED(op), const wmEvent *event)
766 {
767         /* no running blender, remove handler and pass through */
768         if (0 == WM_jobs_test(CTX_wm_manager(C), CTX_data_scene(C), WM_JOB_TYPE_OBJECT_BAKE_TEXTURE))
769                 return OPERATOR_FINISHED | OPERATOR_PASS_THROUGH;
770
771         /* running render */
772         switch (event->type) {
773                 case ESCKEY:
774                         return OPERATOR_RUNNING_MODAL;
775         }
776         return OPERATOR_PASS_THROUGH;
777 }
778
779 static bool is_multires_bake(Scene *scene)
780 {
781         if (ELEM(scene->r.bake_mode, RE_BAKE_NORMALS, RE_BAKE_DISPLACEMENT, RE_BAKE_DERIVATIVE, RE_BAKE_AO))
782                 return scene->r.bake_flag & R_BAKE_MULTIRES;
783
784         return 0;
785 }
786
787 static int objects_bake_render_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(_event))
788 {
789         Scene *scene = CTX_data_scene(C);
790         int result = OPERATOR_CANCELLED;
791
792         if (is_multires_bake(scene)) {
793                 result = multiresbake_image_exec(C, op);
794         }
795         else {
796                 /* only one render job at a time */
797                 if (WM_jobs_test(CTX_wm_manager(C), scene, WM_JOB_TYPE_OBJECT_BAKE_TEXTURE))
798                         return OPERATOR_CANCELLED;
799
800                 if (test_bake_internal(C, op->reports) == 0) {
801                         return OPERATOR_CANCELLED;
802                 }
803                 else {
804                         BakeRender *bkr = MEM_callocN(sizeof(BakeRender), "render bake");
805                         wmJob *wm_job;
806
807                         init_bake_internal(bkr, C);
808                         bkr->reports = op->reports;
809
810                         /* setup job */
811                         wm_job = WM_jobs_get(CTX_wm_manager(C), CTX_wm_window(C), scene, "Texture Bake",
812                                              WM_JOB_EXCL_RENDER | WM_JOB_PRIORITY | WM_JOB_PROGRESS, WM_JOB_TYPE_OBJECT_BAKE_TEXTURE);
813                         WM_jobs_customdata_set(wm_job, bkr, bake_freejob);
814                         WM_jobs_timer(wm_job, 0.5, NC_IMAGE, 0); /* TODO - only draw bake image, can we enforce this */
815                         WM_jobs_callbacks(wm_job, bake_startjob, NULL, bake_update, NULL);
816
817                         G.is_break = false;
818                         G.is_rendering = true;
819
820                         WM_jobs_start(CTX_wm_manager(C), wm_job);
821
822                         WM_cursor_wait(0);
823
824                         /* add modal handler for ESC */
825                         WM_event_add_modal_handler(C, op);
826                 }
827
828                 result = OPERATOR_RUNNING_MODAL;
829         }
830
831         WM_event_add_notifier(C, NC_SCENE | ND_RENDER_RESULT, scene);
832
833         return result;
834 }
835
836
837 static int bake_image_exec(bContext *C, wmOperator *op)
838 {
839         Main *bmain = CTX_data_main(C);
840         Scene *scene = CTX_data_scene(C);
841         ViewLayer *sl = CTX_data_view_layer(C);
842         int result = OPERATOR_CANCELLED;
843
844         if (is_multires_bake(scene)) {
845                 result = multiresbake_image_exec_locked(C, op);
846         }
847         else {
848                 if (test_bake_internal(C, op->reports) == 0) {
849                         return OPERATOR_CANCELLED;
850                 }
851                 else {
852                         ListBase threads;
853                         BakeRender bkr = {NULL};
854
855                         init_bake_internal(&bkr, C);
856                         bkr.reports = op->reports;
857
858                         RE_test_break_cb(bkr.re, NULL, thread_break);
859                         G.is_break = false;   /* BKE_blender_test_break uses this global */
860
861                         RE_Database_Baking(bkr.re, bmain, scene, scene->lay, scene->r.bake_mode, (scene->r.bake_flag & R_BAKE_TO_ACTIVE) ? OBACT(sl) : NULL);
862
863                         /* baking itself is threaded, cannot use test_break in threads  */
864                         BLI_init_threads(&threads, do_bake_render, 1);
865                         bkr.ready = 0;
866                         BLI_insert_thread(&threads, &bkr);
867
868                         while (bkr.ready == 0) {
869                                 PIL_sleep_ms(50);
870                                 if (bkr.ready)
871                                         break;
872
873                                 /* used to redraw in 2.4x but this is just for exec in 2.5 */
874                                 if (!G.background)
875                                         BKE_blender_test_break();
876                         }
877                         BLI_end_threads(&threads);
878
879                         if (bkr.result == BAKE_RESULT_NO_OBJECTS)
880                                 BKE_report(op->reports, RPT_ERROR, "No valid images found to bake to");
881                         else if (bkr.result == BAKE_RESULT_FEEDBACK_LOOP)
882                                 BKE_report(op->reports, RPT_ERROR, "Circular reference in texture stack");
883
884                         finish_bake_internal(&bkr);
885
886                         result = OPERATOR_FINISHED;
887                 }
888         }
889
890         WM_event_add_notifier(C, NC_SCENE | ND_RENDER_RESULT, scene);
891
892         return result;
893 }
894
895 void OBJECT_OT_bake_image(wmOperatorType *ot)
896 {
897         /* identifiers */
898         ot->name = "Bake";
899         ot->description = "Bake image textures of selected objects";
900         ot->idname = "OBJECT_OT_bake_image";
901
902         /* api callbacks */
903         ot->exec = bake_image_exec;
904         ot->invoke = objects_bake_render_invoke;
905         ot->modal = objects_bake_render_modal;
906         ot->poll = ED_operator_object_active;
907 }