Merge branch 'master' into blender2.8
[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 #include "ED_uvedit.h"
83
84 #include "object_intern.h"
85
86 static Image *bake_object_image_get(Object *ob, int mat_nr)
87 {
88         Image *image = NULL;
89         ED_object_get_active_image(ob, mat_nr + 1, &image, NULL, NULL, NULL);
90         return image;
91 }
92
93 static Image **bake_object_image_get_array(Object *ob)
94 {
95         Image **image_array = MEM_mallocN(sizeof(Material *) * ob->totcol, __func__);
96         for (int i = 0; i < ob->totcol; i++) {
97                 image_array[i] = bake_object_image_get(ob, i);
98         }
99         return image_array;
100 }
101
102 /* ****************** multires BAKING ********************** */
103
104 /* holder of per-object data needed for bake job
105  * needed to make job totally thread-safe */
106 typedef struct MultiresBakerJobData {
107         struct MultiresBakerJobData *next, *prev;
108         /* material aligned image array (for per-face bake image) */
109         struct {
110                 Image **array;
111                 int     len;
112         } ob_image;
113         DerivedMesh *lores_dm, *hires_dm;
114         bool simple;
115         int lvl, tot_lvl;
116         ListBase images;
117 } MultiresBakerJobData;
118
119 /* data passing to multires-baker job */
120 typedef struct {
121         ListBase data;
122         bool bake_clear;      /* Clear the images before baking */
123         int bake_filter;      /* Bake-filter, aka margin */
124         short mode;           /* mode of baking (displacement, normals, AO) */
125         bool use_lores_mesh;  /* Use low-resolution mesh when baking displacement maps */
126         int number_of_rays;   /* Number of rays to be cast when doing AO baking */
127         float bias;           /* Bias between object and start ray point when doing AO baking */
128         int threads;             /* Number of threads to be used for baking */
129         float user_scale;          /* User scale used to scale displacement when baking derivative map. */
130 } MultiresBakeJob;
131
132 static bool multiresbake_check(bContext *C, wmOperator *op)
133 {
134         Scene *scene = CTX_data_scene(C);
135         Object *ob;
136         Mesh *me;
137         MultiresModifierData *mmd;
138         bool ok = true;
139         int a;
140
141         CTX_DATA_BEGIN (C, Base *, base, selected_editable_bases)
142         {
143                 ob = base->object;
144
145                 if (ob->type != OB_MESH) {
146                         BKE_report(op->reports, RPT_ERROR, "Baking of multires data only works with an active mesh object");
147
148                         ok = false;
149                         break;
150                 }
151
152                 me = (Mesh *)ob->data;
153                 mmd = get_multires_modifier(scene, ob, 0);
154
155                 /* Multi-resolution should be and be last in the stack */
156                 if (ok && mmd) {
157                         ModifierData *md;
158
159                         ok = mmd->totlvl > 0;
160
161                         for (md = (ModifierData *)mmd->modifier.next; md && ok; md = md->next) {
162                                 if (modifier_isEnabled(scene, md, eModifierMode_Realtime)) {
163                                         ok = false;
164                                 }
165                         }
166                 }
167                 else {
168                         ok = false;
169                 }
170
171                 if (!ok) {
172                         BKE_report(op->reports, RPT_ERROR, "Multires data baking requires multi-resolution object");
173
174                         break;
175                 }
176
177                 if (!me->mloopuv) {
178                         BKE_report(op->reports, RPT_ERROR, "Mesh should be unwrapped before multires data baking");
179
180                         ok = false;
181                 }
182                 else {
183                         a = me->totpoly;
184                         while (ok && a--) {
185                                 Image *ima = bake_object_image_get(ob, me->mpoly[a].mat_nr);
186
187                                 if (!ima) {
188                                         BKE_report(op->reports, RPT_ERROR, "You should have active texture to use multires baker");
189
190                                         ok = false;
191                                 }
192                                 else {
193                                         ImBuf *ibuf = BKE_image_acquire_ibuf(ima, NULL, NULL);
194
195                                         if (!ibuf) {
196                                                 BKE_report(op->reports, RPT_ERROR, "Baking should happen to image with image buffer");
197
198                                                 ok = false;
199                                         }
200                                         else {
201                                                 if (ibuf->rect == NULL && ibuf->rect_float == NULL)
202                                                         ok = false;
203
204                                                 if (ibuf->rect_float && !(ibuf->channels == 0 || ibuf->channels == 4))
205                                                         ok = false;
206
207                                                 if (!ok)
208                                                         BKE_report(op->reports, RPT_ERROR, "Baking to unsupported image type");
209                                         }
210
211                                         BKE_image_release_ibuf(ima, ibuf, NULL);
212                                 }
213                         }
214                 }
215
216                 if (!ok)
217                         break;
218         }
219         CTX_DATA_END;
220
221         return ok;
222 }
223
224 static DerivedMesh *multiresbake_create_loresdm(Scene *scene, Object *ob, int *lvl)
225 {
226         DerivedMesh *dm;
227         MultiresModifierData *mmd = get_multires_modifier(scene, ob, 0);
228         Mesh *me = (Mesh *)ob->data;
229         MultiresModifierData tmp_mmd = *mmd;
230         DerivedMesh *cddm = CDDM_from_mesh(me);
231
232         if (mmd->lvl > 0) {
233                 *lvl = mmd->lvl;
234         }
235         else {
236                 *lvl = 1;
237                 tmp_mmd.simple = true;
238         }
239
240         DM_set_only_copy(cddm, CD_MASK_BAREMESH);
241
242         tmp_mmd.lvl = *lvl;
243         tmp_mmd.sculptlvl = *lvl;
244         dm = multires_make_derived_from_derived(cddm, &tmp_mmd, ob, 0);
245         cddm->release(cddm);
246
247         return dm;
248 }
249
250 static DerivedMesh *multiresbake_create_hiresdm(Scene *scene, Object *ob, int *lvl, bool *simple)
251 {
252         Mesh *me = (Mesh *)ob->data;
253         MultiresModifierData *mmd = get_multires_modifier(scene, ob, 0);
254         MultiresModifierData tmp_mmd = *mmd;
255         DerivedMesh *cddm = CDDM_from_mesh(me);
256         DerivedMesh *dm;
257
258         DM_set_only_copy(cddm, CD_MASK_BAREMESH);
259
260         /* TODO: DM_set_only_copy wouldn't set mask for loop and poly data,
261          *       but we really need BAREMESH only to save lots of memory
262          */
263         CustomData_set_only_copy(&cddm->loopData, CD_MASK_BAREMESH);
264         CustomData_set_only_copy(&cddm->polyData, CD_MASK_BAREMESH);
265
266         *lvl = mmd->totlvl;
267         *simple = mmd->simple != 0;
268
269         tmp_mmd.lvl = mmd->totlvl;
270         tmp_mmd.sculptlvl = mmd->totlvl;
271         dm = multires_make_derived_from_derived(cddm, &tmp_mmd, ob, 0);
272         cddm->release(cddm);
273
274         return dm;
275 }
276
277 typedef enum ClearFlag {
278         CLEAR_TANGENT_NORMAL = 1,
279         CLEAR_DISPLACEMENT = 2
280 } ClearFlag;
281
282
283 static void clear_single_image(Image *image, ClearFlag flag)
284 {
285         const float vec_alpha[4] = {0.0f, 0.0f, 0.0f, 0.0f};
286         const float vec_solid[4] = {0.0f, 0.0f, 0.0f, 1.0f};
287         const float nor_alpha[4] = {0.5f, 0.5f, 1.0f, 0.0f};
288         const float nor_solid[4] = {0.5f, 0.5f, 1.0f, 1.0f};
289         const float disp_alpha[4] = {0.5f, 0.5f, 0.5f, 0.0f};
290         const float disp_solid[4] = {0.5f, 0.5f, 0.5f, 1.0f};
291
292         if ((image->id.tag & LIB_TAG_DOIT) == 0) {
293                 ImBuf *ibuf = BKE_image_acquire_ibuf(image, NULL, NULL);
294
295                 if (flag == CLEAR_TANGENT_NORMAL)
296                         IMB_rectfill(ibuf, (ibuf->planes == R_IMF_PLANES_RGBA) ? nor_alpha : nor_solid);
297                 else if (flag == CLEAR_DISPLACEMENT)
298                         IMB_rectfill(ibuf, (ibuf->planes == R_IMF_PLANES_RGBA) ? disp_alpha : disp_solid);
299                 else
300                         IMB_rectfill(ibuf, (ibuf->planes == R_IMF_PLANES_RGBA) ? vec_alpha : vec_solid);
301
302                 image->id.tag |= LIB_TAG_DOIT;
303
304                 BKE_image_release_ibuf(image, ibuf, NULL);
305         }
306 }
307
308 static void clear_images_poly(Image **ob_image_array, int ob_image_array_len, ClearFlag flag)
309 {
310         for (int i = 0; i < ob_image_array_len; i++) {
311                 Image *image = ob_image_array[i];
312                 if (image) {
313                         image->id.tag &= ~LIB_TAG_DOIT;
314                 }
315         }
316
317         for (int i = 0; i < ob_image_array_len; i++) {
318                 Image *image = ob_image_array[i];
319                 if (image) {
320                         clear_single_image(image, flag);
321                 }
322         }
323
324         for (int i = 0; i < ob_image_array_len; i++) {
325                 Image *image = ob_image_array[i];
326                 if (image) {
327                         image->id.tag &= ~LIB_TAG_DOIT;
328                 }
329         }
330 }
331
332 static int multiresbake_image_exec_locked(bContext *C, wmOperator *op)
333 {
334         Object *ob;
335         Scene *scene = CTX_data_scene(C);
336         int objects_baked = 0;
337
338         if (!multiresbake_check(C, op))
339                 return OPERATOR_CANCELLED;
340
341         if (scene->r.bake_flag & R_BAKE_CLEAR) {  /* clear images */
342                 CTX_DATA_BEGIN (C, Base *, base, selected_editable_bases)
343                 {
344                         ClearFlag clear_flag = 0;
345
346                         ob = base->object;
347                         // me = (Mesh *)ob->data;
348
349                         if (scene->r.bake_mode == RE_BAKE_NORMALS) {
350                                 clear_flag = CLEAR_TANGENT_NORMAL;
351                         }
352                         else if (scene->r.bake_mode == RE_BAKE_DISPLACEMENT) {
353                                 clear_flag = CLEAR_DISPLACEMENT;
354                         }
355
356                         {
357                                 Image **ob_image_array = bake_object_image_get_array(ob);
358                                 clear_images_poly(ob_image_array, ob->totcol, clear_flag);
359                                 MEM_freeN(ob_image_array);
360                         }
361                 }
362                 CTX_DATA_END;
363         }
364
365         CTX_DATA_BEGIN (C, Base *, base, selected_editable_bases)
366         {
367                 MultiresBakeRender bkr = {NULL};
368
369                 ob = base->object;
370
371                 multires_force_update(ob);
372
373                 /* copy data stored in job descriptor */
374                 bkr.bake_filter = scene->r.bake_filter;
375                 bkr.mode = scene->r.bake_mode;
376                 bkr.use_lores_mesh = scene->r.bake_flag & R_BAKE_LORES_MESH;
377                 bkr.bias = scene->r.bake_biasdist;
378                 bkr.number_of_rays = scene->r.bake_samples;
379                 bkr.threads = BKE_scene_num_threads(scene);
380                 bkr.user_scale = (scene->r.bake_flag & R_BAKE_USERSCALE) ? scene->r.bake_user_scale : -1.0f;
381                 //bkr.reports= op->reports;
382
383                 /* create low-resolution DM (to bake to) and hi-resolution DM (to bake from) */
384                 bkr.ob_image.array = bake_object_image_get_array(ob);
385                 bkr.ob_image.len = ob->totcol;
386
387                 bkr.hires_dm = multiresbake_create_hiresdm(scene, ob, &bkr.tot_lvl, &bkr.simple);
388                 bkr.lores_dm = multiresbake_create_loresdm(scene, ob, &bkr.lvl);
389
390                 RE_multires_bake_images(&bkr);
391
392                 MEM_freeN(bkr.ob_image.array);
393
394                 BLI_freelistN(&bkr.image);
395
396                 bkr.lores_dm->release(bkr.lores_dm);
397                 bkr.hires_dm->release(bkr.hires_dm);
398
399                 objects_baked++;
400         }
401         CTX_DATA_END;
402
403         if (!objects_baked)
404                 BKE_report(op->reports, RPT_ERROR, "No objects found to bake from");
405
406         return OPERATOR_FINISHED;
407 }
408
409 /* Multiresbake adopted for job-system executing */
410 static void init_multiresbake_job(bContext *C, MultiresBakeJob *bkj)
411 {
412         Scene *scene = CTX_data_scene(C);
413         Object *ob;
414
415         /* backup scene settings, so their changing in UI would take no effect on baker */
416         bkj->bake_filter = scene->r.bake_filter;
417         bkj->mode = scene->r.bake_mode;
418         bkj->use_lores_mesh = scene->r.bake_flag & R_BAKE_LORES_MESH;
419         bkj->bake_clear = scene->r.bake_flag & R_BAKE_CLEAR;
420         bkj->bias = scene->r.bake_biasdist;
421         bkj->number_of_rays = scene->r.bake_samples;
422         bkj->threads = BKE_scene_num_threads(scene);
423         bkj->user_scale = (scene->r.bake_flag & R_BAKE_USERSCALE) ? scene->r.bake_user_scale : -1.0f;
424         //bkj->reports = op->reports;
425
426         CTX_DATA_BEGIN (C, Base *, base, selected_editable_bases)
427         {
428                 MultiresBakerJobData *data;
429                 int lvl;
430
431                 ob = base->object;
432
433                 multires_force_update(ob);
434
435                 data = MEM_callocN(sizeof(MultiresBakerJobData), "multiresBaker derivedMesh_data");
436
437                 data->ob_image.array = bake_object_image_get_array(ob);
438                 data->ob_image.len = ob->totcol;
439
440                 /* create low-resolution DM (to bake to) and hi-resolution DM (to bake from) */
441                 data->hires_dm = multiresbake_create_hiresdm(scene, ob, &data->tot_lvl, &data->simple);
442                 data->lores_dm = multiresbake_create_loresdm(scene, ob, &lvl);
443                 data->lvl = lvl;
444
445                 BLI_addtail(&bkj->data, data);
446         }
447         CTX_DATA_END;
448 }
449
450 static void multiresbake_startjob(void *bkv, short *stop, short *do_update, float *progress)
451 {
452         MultiresBakerJobData *data;
453         MultiresBakeJob *bkj = bkv;
454         int baked_objects = 0, tot_obj;
455
456         tot_obj = BLI_listbase_count(&bkj->data);
457
458         if (bkj->bake_clear) {  /* clear images */
459                 for (data = bkj->data.first; data; data = data->next) {
460                         ClearFlag clear_flag = 0;
461
462                         if (bkj->mode == RE_BAKE_NORMALS) {
463                                 clear_flag = CLEAR_TANGENT_NORMAL;
464                         }
465                         else if (bkj->mode == RE_BAKE_DISPLACEMENT) {
466                                 clear_flag = CLEAR_DISPLACEMENT;
467                         }
468
469                         clear_images_poly(data->ob_image.array, data->ob_image.len, clear_flag);
470                 }
471         }
472
473         for (data = bkj->data.first; data; data = data->next) {
474                 MultiresBakeRender bkr = {NULL};
475
476                 /* copy data stored in job descriptor */
477                 bkr.bake_filter = bkj->bake_filter;
478                 bkr.mode = bkj->mode;
479                 bkr.use_lores_mesh = bkj->use_lores_mesh;
480                 bkr.user_scale = bkj->user_scale;
481                 //bkr.reports = bkj->reports;
482                 bkr.ob_image.array = data->ob_image.array;
483                 bkr.ob_image.len   = data->ob_image.len;
484
485                 /* create low-resolution DM (to bake to) and hi-resolution DM (to bake from) */
486                 bkr.lores_dm = data->lores_dm;
487                 bkr.hires_dm = data->hires_dm;
488                 bkr.tot_lvl = data->tot_lvl;
489                 bkr.lvl = data->lvl;
490                 bkr.simple = data->simple;
491
492                 /* needed for proper progress bar */
493                 bkr.tot_obj = tot_obj;
494                 bkr.baked_objects = baked_objects;
495
496                 bkr.stop = stop;
497                 bkr.do_update = do_update;
498                 bkr.progress = progress;
499
500                 bkr.bias = bkj->bias;
501                 bkr.number_of_rays = bkj->number_of_rays;
502                 bkr.threads = bkj->threads;
503
504                 RE_multires_bake_images(&bkr);
505
506                 data->images = bkr.image;
507
508                 baked_objects++;
509         }
510 }
511
512 static void multiresbake_freejob(void *bkv)
513 {
514         MultiresBakeJob *bkj = bkv;
515         MultiresBakerJobData *data, *next;
516         LinkData *link;
517
518         data = bkj->data.first;
519         while (data) {
520                 next = data->next;
521                 data->lores_dm->release(data->lores_dm);
522                 data->hires_dm->release(data->hires_dm);
523
524                 /* delete here, since this delete will be called from main thread */
525                 for (link = data->images.first; link; link = link->next) {
526                         Image *ima = (Image *)link->data;
527                         GPU_free_image(ima);
528                 }
529
530                 MEM_freeN(data->ob_image.array);
531
532                 BLI_freelistN(&data->images);
533
534                 MEM_freeN(data);
535                 data = next;
536         }
537
538         MEM_freeN(bkj);
539 }
540
541 static int multiresbake_image_exec(bContext *C, wmOperator *op)
542 {
543         Scene *scene = CTX_data_scene(C);
544         MultiresBakeJob *bkr;
545         wmJob *wm_job;
546
547         if (!multiresbake_check(C, op))
548                 return OPERATOR_CANCELLED;
549
550         bkr = MEM_callocN(sizeof(MultiresBakeJob), "MultiresBakeJob data");
551         init_multiresbake_job(C, bkr);
552
553         if (!bkr->data.first) {
554                 BKE_report(op->reports, RPT_ERROR, "No objects found to bake from");
555                 return OPERATOR_CANCELLED;
556         }
557
558         /* setup job */
559         wm_job = WM_jobs_get(CTX_wm_manager(C), CTX_wm_window(C), scene, "Multires Bake",
560                              WM_JOB_EXCL_RENDER | WM_JOB_PRIORITY | WM_JOB_PROGRESS, WM_JOB_TYPE_OBJECT_BAKE_TEXTURE);
561         WM_jobs_customdata_set(wm_job, bkr, multiresbake_freejob);
562         WM_jobs_timer(wm_job, 0.5, NC_IMAGE, 0); /* TODO - only draw bake image, can we enforce this */
563         WM_jobs_callbacks(wm_job, multiresbake_startjob, NULL, NULL, NULL);
564
565         G.is_break = false;
566
567         WM_jobs_start(CTX_wm_manager(C), wm_job);
568         WM_cursor_wait(0);
569
570         /* add modal handler for ESC */
571         WM_event_add_modal_handler(C, op);
572
573         return OPERATOR_RUNNING_MODAL;
574 }
575
576 /* ****************** render BAKING ********************** */
577
578 /* catch esc */
579 static int objects_bake_render_modal(bContext *C, wmOperator *UNUSED(op), const wmEvent *event)
580 {
581         /* no running blender, remove handler and pass through */
582         if (0 == WM_jobs_test(CTX_wm_manager(C), CTX_data_scene(C), WM_JOB_TYPE_OBJECT_BAKE_TEXTURE))
583                 return OPERATOR_FINISHED | OPERATOR_PASS_THROUGH;
584
585         /* running render */
586         switch (event->type) {
587                 case ESCKEY:
588                         return OPERATOR_RUNNING_MODAL;
589         }
590         return OPERATOR_PASS_THROUGH;
591 }
592
593 static bool is_multires_bake(Scene *scene)
594 {
595         if (ELEM(scene->r.bake_mode, RE_BAKE_NORMALS, RE_BAKE_DISPLACEMENT, RE_BAKE_AO))
596                 return scene->r.bake_flag & R_BAKE_MULTIRES;
597
598         return 0;
599 }
600
601 static int objects_bake_render_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(_event))
602 {
603         Scene *scene = CTX_data_scene(C);
604         int result = OPERATOR_CANCELLED;
605
606         result = multiresbake_image_exec(C, op);
607
608         WM_event_add_notifier(C, NC_SCENE | ND_RENDER_RESULT, scene);
609
610         return result;
611 }
612
613
614 static int bake_image_exec(bContext *C, wmOperator *op)
615 {
616         Scene *scene = CTX_data_scene(C);
617         int result = OPERATOR_CANCELLED;
618
619         if (!is_multires_bake(scene)) {
620                 BLI_assert(0);
621                 return result;
622         }
623
624         result = multiresbake_image_exec_locked(C, op);
625
626         WM_event_add_notifier(C, NC_SCENE | ND_RENDER_RESULT, scene);
627
628         return result;
629 }
630
631 void OBJECT_OT_bake_image(wmOperatorType *ot)
632 {
633         /* identifiers */
634         ot->name = "Bake";
635         ot->description = "Bake image textures of selected objects";
636         ot->idname = "OBJECT_OT_bake_image";
637
638         /* api callbacks */
639         ot->exec = bake_image_exec;
640         ot->invoke = objects_bake_render_invoke;
641         ot->modal = objects_bake_render_modal;
642         ot->poll = ED_operator_object_active;
643 }