Pass EvaluationContext argument everywhere
[blender.git] / source / blender / blenkernel / intern / paint.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) 2009 by Nicholas Bishop
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/blenkernel/intern/paint.c
29  *  \ingroup bke
30  */
31
32 #include <stdlib.h>
33 #include <string.h>
34
35 #include "MEM_guardedalloc.h"
36
37 #include "DNA_object_types.h"
38 #include "DNA_mesh_types.h"
39 #include "DNA_meshdata_types.h"
40 #include "DNA_modifier_types.h"
41 #include "DNA_scene_types.h"
42 #include "DNA_brush_types.h"
43 #include "DNA_space_types.h"
44
45 #include "BLI_bitmap.h"
46 #include "BLI_utildefines.h"
47 #include "BLI_math_vector.h"
48 #include "BLI_listbase.h"
49
50 #include "BKE_brush.h"
51 #include "BKE_colortools.h"
52 #include "BKE_main.h"
53 #include "BKE_context.h"
54 #include "BKE_crazyspace.h"
55 #include "BKE_global.h"
56 #include "BKE_image.h"
57 #include "BKE_key.h"
58 #include "BKE_library.h"
59 #include "BKE_mesh.h"
60 #include "BKE_modifier.h"
61 #include "BKE_object.h"
62 #include "BKE_paint.h"
63 #include "BKE_pbvh.h"
64 #include "BKE_subsurf.h"
65
66 #include "DEG_depsgraph.h"
67
68 #include "bmesh.h"
69
70 const char PAINT_CURSOR_SCULPT[3] = {255, 100, 100};
71 const char PAINT_CURSOR_VERTEX_PAINT[3] = {255, 255, 255};
72 const char PAINT_CURSOR_WEIGHT_PAINT[3] = {200, 200, 255};
73 const char PAINT_CURSOR_TEXTURE_PAINT[3] = {255, 255, 255};
74
75 static OverlayControlFlags overlay_flags = 0;
76
77 void BKE_paint_invalidate_overlay_tex(Scene *scene, SceneLayer *sl, const Tex *tex)
78 {
79         Paint *p = BKE_paint_get_active(scene, sl);
80         Brush *br = p->brush;
81
82         if (!br)
83                 return;
84
85         if (br->mtex.tex == tex)
86                 overlay_flags |= PAINT_INVALID_OVERLAY_TEXTURE_PRIMARY;
87         if (br->mask_mtex.tex == tex)
88                 overlay_flags |= PAINT_INVALID_OVERLAY_TEXTURE_SECONDARY;
89 }
90
91 void BKE_paint_invalidate_cursor_overlay(Scene *scene, SceneLayer *sl, CurveMapping *curve)
92 {
93         Paint *p = BKE_paint_get_active(scene, sl);
94         Brush *br = p->brush;
95
96         if (br && br->curve == curve)
97                 overlay_flags |= PAINT_INVALID_OVERLAY_CURVE;
98 }
99
100 void BKE_paint_invalidate_overlay_all(void)
101 {
102         overlay_flags |= (PAINT_INVALID_OVERLAY_TEXTURE_SECONDARY |
103                           PAINT_INVALID_OVERLAY_TEXTURE_PRIMARY |
104                           PAINT_INVALID_OVERLAY_CURVE);
105 }
106
107 OverlayControlFlags BKE_paint_get_overlay_flags(void)
108 {
109         return overlay_flags;
110 }
111
112 void BKE_paint_set_overlay_override(OverlayFlags flags)
113 {
114         if (flags & BRUSH_OVERLAY_OVERRIDE_MASK) {
115                 if (flags & BRUSH_OVERLAY_CURSOR_OVERRIDE_ON_STROKE)
116                         overlay_flags |= PAINT_OVERLAY_OVERRIDE_CURSOR;
117                 if (flags & BRUSH_OVERLAY_PRIMARY_OVERRIDE_ON_STROKE)
118                         overlay_flags |= PAINT_OVERLAY_OVERRIDE_PRIMARY;
119                 if (flags & BRUSH_OVERLAY_SECONDARY_OVERRIDE_ON_STROKE)
120                         overlay_flags |= PAINT_OVERLAY_OVERRIDE_SECONDARY;
121         }
122         else {
123                 overlay_flags &= ~(PAINT_OVERRIDE_MASK);
124         }
125 }
126
127 void BKE_paint_reset_overlay_invalid(OverlayControlFlags flag)
128 {
129         overlay_flags &= ~(flag);
130 }
131
132 Paint *BKE_paint_get_active_from_paintmode(Scene *sce, PaintMode mode)
133 {
134         if (sce) {
135                 ToolSettings *ts = sce->toolsettings;
136
137                 switch (mode) {
138                         case ePaintSculpt:
139                                 return &ts->sculpt->paint;
140                         case ePaintVertex:
141                                 return &ts->vpaint->paint;
142                         case ePaintWeight:
143                                 return &ts->wpaint->paint;
144                         case ePaintTexture2D:
145                         case ePaintTextureProjective:
146                                 return &ts->imapaint.paint;
147                         case ePaintSculptUV:
148                                 return &ts->uvsculpt->paint;
149                         case ePaintInvalid:
150                                 return NULL;
151                         default:
152                                 return &ts->imapaint.paint;
153                 }
154         }
155
156         return NULL;
157 }
158
159 Paint *BKE_paint_get_active(Scene *sce, SceneLayer *sl)
160 {
161         if (sce && sl) {
162                 ToolSettings *ts = sce->toolsettings;
163                 
164                 if (sl->basact && sl->basact->object) {
165                         switch (sl->basact->object->mode) {
166                                 case OB_MODE_SCULPT:
167                                         return &ts->sculpt->paint;
168                                 case OB_MODE_VERTEX_PAINT:
169                                         return &ts->vpaint->paint;
170                                 case OB_MODE_WEIGHT_PAINT:
171                                         return &ts->wpaint->paint;
172                                 case OB_MODE_TEXTURE_PAINT:
173                                         return &ts->imapaint.paint;
174                                 case OB_MODE_EDIT:
175                                         if (ts->use_uv_sculpt)
176                                                 return &ts->uvsculpt->paint;
177                                         return &ts->imapaint.paint;
178                         }
179                 }
180
181                 /* default to image paint */
182                 return &ts->imapaint.paint;
183         }
184
185         return NULL;
186 }
187
188 Paint *BKE_paint_get_active_from_context(const bContext *C)
189 {
190         Scene *sce = CTX_data_scene(C);
191         SceneLayer *sl = CTX_data_scene_layer(C);
192         SpaceImage *sima;
193
194         if (sce && sl) {
195                 ToolSettings *ts = sce->toolsettings;
196                 Object *obact = NULL;
197
198                 if (sl->basact && sl->basact->object)
199                         obact = sl->basact->object;
200
201                 if ((sima = CTX_wm_space_image(C)) != NULL) {
202                         if (obact && obact->mode == OB_MODE_EDIT) {
203                                 if (sima->mode == SI_MODE_PAINT)
204                                         return &ts->imapaint.paint;
205                                 else if (ts->use_uv_sculpt)
206                                         return &ts->uvsculpt->paint;
207                         }
208                         else {
209                                 return &ts->imapaint.paint;
210                         }
211                 }
212                 else if (obact) {
213                         switch (obact->mode) {
214                                 case OB_MODE_SCULPT:
215                                         return &ts->sculpt->paint;
216                                 case OB_MODE_VERTEX_PAINT:
217                                         return &ts->vpaint->paint;
218                                 case OB_MODE_WEIGHT_PAINT:
219                                         return &ts->wpaint->paint;
220                                 case OB_MODE_TEXTURE_PAINT:
221                                         return &ts->imapaint.paint;
222                                 case OB_MODE_EDIT:
223                                         if (ts->use_uv_sculpt)
224                                                 return &ts->uvsculpt->paint;
225                                         return &ts->imapaint.paint;
226                                 default:
227                                         return &ts->imapaint.paint;
228                         }
229                 }
230                 else {
231                         /* default to image paint */
232                         return &ts->imapaint.paint;
233                 }
234         }
235
236         return NULL;
237 }
238
239 PaintMode BKE_paintmode_get_active_from_context(const bContext *C)
240 {
241         Scene *sce = CTX_data_scene(C);
242         SceneLayer *sl = CTX_data_scene_layer(C);
243         SpaceImage *sima;
244
245         if (sce && sl) {
246                 ToolSettings *ts = sce->toolsettings;
247                 Object *obact = NULL;
248
249                 if (sl->basact && sl->basact->object)
250                         obact = sl->basact->object;
251
252                 if ((sima = CTX_wm_space_image(C)) != NULL) {
253                         if (obact && obact->mode == OB_MODE_EDIT) {
254                                 if (sima->mode == SI_MODE_PAINT)
255                                         return ePaintTexture2D;
256                                 else if (ts->use_uv_sculpt)
257                                         return ePaintSculptUV;
258                         }
259                         else {
260                                 return ePaintTexture2D;
261                         }
262                 }
263                 else if (obact) {
264                         switch (obact->mode) {
265                                 case OB_MODE_SCULPT:
266                                         return ePaintSculpt;
267                                 case OB_MODE_VERTEX_PAINT:
268                                         return ePaintVertex;
269                                 case OB_MODE_WEIGHT_PAINT:
270                                         return ePaintWeight;
271                                 case OB_MODE_TEXTURE_PAINT:
272                                         return ePaintTextureProjective;
273                                 case OB_MODE_EDIT:
274                                         if (ts->use_uv_sculpt)
275                                                 return ePaintSculptUV;
276                                         return ePaintTexture2D;
277                                 default:
278                                         return ePaintTexture2D;
279                         }
280                 }
281                 else {
282                         /* default to image paint */
283                         return ePaintTexture2D;
284                 }
285         }
286
287         return ePaintInvalid;
288 }
289
290 Brush *BKE_paint_brush(Paint *p)
291 {
292         return p ? p->brush : NULL;
293 }
294
295 void BKE_paint_brush_set(Paint *p, Brush *br)
296 {
297         if (p) {
298                 id_us_min((ID *)p->brush);
299                 id_us_plus((ID *)br);
300                 p->brush = br;
301         }
302 }
303
304 /** Free (or release) any data used by this paint curve (does not free the pcurve itself). */
305 void BKE_paint_curve_free(PaintCurve *pc)
306 {
307         MEM_SAFE_FREE(pc->points);
308         pc->tot_points = 0;
309 }
310
311 PaintCurve *BKE_paint_curve_add(Main *bmain, const char *name)
312 {
313         PaintCurve *pc;
314
315         pc = BKE_libblock_alloc(bmain, ID_PC, name);
316
317         return pc;
318 }
319
320 PaintCurve *BKE_paint_curve_copy(Main *bmain, const PaintCurve *pc)
321 {
322         PaintCurve *pc_new;
323
324         pc_new = BKE_libblock_copy(bmain, &pc->id);
325
326         if (pc->tot_points != 0) {
327                 pc_new->points = MEM_dupallocN(pc->points);
328         }
329
330         BKE_id_copy_ensure_local(bmain, &pc->id, &pc_new->id);
331
332         return pc_new;
333 }
334
335 void BKE_paint_curve_make_local(Main *bmain, PaintCurve *pc, const bool lib_local)
336 {
337         BKE_id_make_local_generic(bmain, &pc->id, true, lib_local);
338 }
339
340 Palette *BKE_paint_palette(Paint *p)
341 {
342         return p ? p->palette : NULL;
343 }
344
345 void BKE_paint_palette_set(Paint *p, Palette *palette)
346 {
347         if (p) {
348                 id_us_min((ID *)p->palette);
349                 id_us_plus((ID *)palette);
350                 p->palette = palette;
351         }
352 }
353
354 void BKE_paint_curve_set(Brush *br, PaintCurve *pc)
355 {
356         if (br) {
357                 id_us_min((ID *)br->paint_curve);
358                 id_us_plus((ID *)pc);
359                 br->paint_curve = pc;
360         }
361 }
362
363 void BKE_paint_curve_clamp_endpoint_add_index(PaintCurve *pc, const int add_index)
364 {
365         pc->add_index = (add_index || pc->tot_points == 1) ? (add_index + 1) : 0;
366 }
367
368 /* remove colour from palette. Must be certain color is inside the palette! */
369 void BKE_palette_color_remove(Palette *palette, PaletteColor *color)
370 {
371         if (BLI_listbase_count_ex(&palette->colors, palette->active_color) == palette->active_color) {
372                 palette->active_color--;
373         }
374
375         BLI_remlink(&palette->colors, color);
376
377         if (palette->active_color < 0 && !BLI_listbase_is_empty(&palette->colors)) {
378                 palette->active_color = 0;
379         }
380
381         MEM_freeN(color);
382 }
383
384 void BKE_palette_clear(Palette *palette)
385 {
386         BLI_freelistN(&palette->colors);
387         palette->active_color = 0;
388 }
389
390 Palette *BKE_palette_add(Main *bmain, const char *name)
391 {
392         Palette *palette;
393
394         palette = BKE_libblock_alloc(bmain, ID_PAL, name);
395
396         /* enable fake user by default */
397         id_fake_user_set(&palette->id);
398
399         return palette;
400 }
401
402 Palette *BKE_palette_copy(Main *bmain, const Palette *palette)
403 {
404         Palette *palette_new;
405
406         palette_new = BKE_libblock_copy(bmain, &palette->id);
407
408         BLI_duplicatelist(&palette_new->colors, &palette->colors);
409
410         BKE_id_copy_ensure_local(bmain, &palette->id, &palette_new->id);
411
412         return palette_new;
413 }
414
415 void BKE_palette_make_local(Main *bmain, Palette *palette, const bool lib_local)
416 {
417         BKE_id_make_local_generic(bmain, &palette->id, true, lib_local);
418 }
419
420 /** Free (or release) any data used by this palette (does not free the palette itself). */
421 void BKE_palette_free(Palette *palette)
422 {
423         BLI_freelistN(&palette->colors);
424 }
425
426 PaletteColor *BKE_palette_color_add(Palette *palette)
427 {
428         PaletteColor *color = MEM_callocN(sizeof(*color), "Pallete Color");
429         BLI_addtail(&palette->colors, color);
430         return color;
431 }
432
433
434 bool BKE_palette_is_empty(const struct Palette *palette)
435 {
436         return BLI_listbase_is_empty(&palette->colors);
437 }
438
439
440 /* are we in vertex paint or weight pain face select mode? */
441 bool BKE_paint_select_face_test(Object *ob)
442 {
443         return ( (ob != NULL) &&
444                  (ob->type == OB_MESH) &&
445                  (ob->data != NULL) &&
446                  (((Mesh *)ob->data)->editflag & ME_EDIT_PAINT_FACE_SEL) &&
447                  (ob->mode & (OB_MODE_VERTEX_PAINT | OB_MODE_WEIGHT_PAINT | OB_MODE_TEXTURE_PAINT))
448                  );
449 }
450
451 /* are we in weight paint vertex select mode? */
452 bool BKE_paint_select_vert_test(Object *ob)
453 {
454         return ( (ob != NULL) &&
455                  (ob->type == OB_MESH) &&
456                  (ob->data != NULL) &&
457                  (((Mesh *)ob->data)->editflag & ME_EDIT_PAINT_VERT_SEL) &&
458                  (ob->mode & OB_MODE_WEIGHT_PAINT)
459                  );
460 }
461
462 /**
463  * used to check if selection is possible
464  * (when we don't care if its face or vert)
465  */
466 bool BKE_paint_select_elem_test(Object *ob)
467 {
468         return (BKE_paint_select_vert_test(ob) ||
469                 BKE_paint_select_face_test(ob));
470 }
471
472 void BKE_paint_cavity_curve_preset(Paint *p, int preset)
473 {
474         CurveMap *cm = NULL;
475
476         if (!p->cavity_curve)
477                 p->cavity_curve = curvemapping_add(1, 0, 0, 1, 1);
478
479         cm = p->cavity_curve->cm;
480         cm->flag &= ~CUMA_EXTEND_EXTRAPOLATE;
481
482         p->cavity_curve->preset = preset;
483         curvemap_reset(cm, &p->cavity_curve->clipr, p->cavity_curve->preset, CURVEMAP_SLOPE_POSITIVE);
484         curvemapping_changed(p->cavity_curve, false);
485 }
486
487 short BKE_paint_object_mode_from_paint_mode(PaintMode mode)
488 {
489         switch (mode) {
490                 case ePaintSculpt:
491                         return OB_MODE_SCULPT;
492                 case ePaintVertex:
493                         return OB_MODE_VERTEX_PAINT;
494                 case ePaintWeight:
495                         return OB_MODE_WEIGHT_PAINT;
496                 case ePaintTextureProjective:
497                         return OB_MODE_TEXTURE_PAINT;
498                 case ePaintTexture2D:
499                         return OB_MODE_TEXTURE_PAINT;
500                 case ePaintSculptUV:
501                         return OB_MODE_EDIT;
502                 case ePaintInvalid:
503                 default:
504                         return 0;
505         }
506 }
507
508 void BKE_paint_init(Scene *sce, PaintMode mode, const char col[3])
509 {
510         UnifiedPaintSettings *ups = &sce->toolsettings->unified_paint_settings;
511         Brush *brush;
512         Paint *paint = BKE_paint_get_active_from_paintmode(sce, mode);
513
514         /* If there's no brush, create one */
515         brush = BKE_paint_brush(paint);
516         if (brush == NULL) {
517                 short ob_mode = BKE_paint_object_mode_from_paint_mode(mode);
518                 brush = BKE_brush_first_search(G.main, ob_mode);
519
520                 if (!brush)
521                         brush = BKE_brush_add(G.main, "Brush", ob_mode);
522                 BKE_paint_brush_set(paint, brush);
523         }
524
525         memcpy(paint->paint_cursor_col, col, 3);
526         paint->paint_cursor_col[3] = 128;
527         ups->last_stroke_valid = false;
528         zero_v3(ups->average_stroke_accum);
529         ups->average_stroke_counter = 0;
530         if (!paint->cavity_curve)
531                 BKE_paint_cavity_curve_preset(paint, CURVE_PRESET_LINE);
532 }
533
534 void BKE_paint_free(Paint *paint)
535 {
536         curvemapping_free(paint->cavity_curve);
537 }
538
539 /* called when copying scene settings, so even if 'src' and 'tar' are the same
540  * still do a id_us_plus(), rather then if we were copying between 2 existing
541  * scenes where a matching value should decrease the existing user count as
542  * with paint_brush_set() */
543 void BKE_paint_copy(Paint *src, Paint *tar)
544 {
545         tar->brush = src->brush;
546         id_us_plus((ID *)tar->brush);
547         id_us_plus((ID *)tar->palette);
548         tar->cavity_curve = curvemapping_copy(src->cavity_curve);
549 }
550
551 void BKE_paint_stroke_get_average(Scene *scene, Object *ob, float stroke[3])
552 {
553         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
554         if (ups->last_stroke_valid && ups->average_stroke_counter > 0) {
555                 float fac = 1.0f / ups->average_stroke_counter;
556                 mul_v3_v3fl(stroke, ups->average_stroke_accum, fac);
557         }
558         else {
559                 copy_v3_v3(stroke, ob->obmat[3]);
560         }
561 }
562
563 /* returns non-zero if any of the face's vertices
564  * are hidden, zero otherwise */
565 bool paint_is_face_hidden(const MLoopTri *lt, const MVert *mvert, const MLoop *mloop)
566 {
567         return ((mvert[mloop[lt->tri[0]].v].flag & ME_HIDE) ||
568                 (mvert[mloop[lt->tri[1]].v].flag & ME_HIDE) ||
569                 (mvert[mloop[lt->tri[2]].v].flag & ME_HIDE));
570 }
571
572 /* returns non-zero if any of the corners of the grid
573  * face whose inner corner is at (x, y) are hidden,
574  * zero otherwise */
575 bool paint_is_grid_face_hidden(const unsigned int *grid_hidden,
576                               int gridsize, int x, int y)
577 {
578         /* skip face if any of its corners are hidden */
579         return (BLI_BITMAP_TEST(grid_hidden, y * gridsize + x) ||
580                 BLI_BITMAP_TEST(grid_hidden, y * gridsize + x + 1) ||
581                 BLI_BITMAP_TEST(grid_hidden, (y + 1) * gridsize + x + 1) ||
582                 BLI_BITMAP_TEST(grid_hidden, (y + 1) * gridsize + x));
583 }
584
585 /* Return true if all vertices in the face are visible, false otherwise */
586 bool paint_is_bmesh_face_hidden(BMFace *f)
587 {
588         BMLoop *l_iter;
589         BMLoop *l_first;
590
591         l_iter = l_first = BM_FACE_FIRST_LOOP(f);
592         do {
593                 if (BM_elem_flag_test(l_iter->v, BM_ELEM_HIDDEN)) {
594                         return true;
595                 }
596         } while ((l_iter = l_iter->next) != l_first);
597
598         return false;
599 }
600
601 float paint_grid_paint_mask(const GridPaintMask *gpm, unsigned level,
602                             unsigned x, unsigned y)
603 {
604         int factor = BKE_ccg_factor(level, gpm->level);
605         int gridsize = BKE_ccg_gridsize(gpm->level);
606         
607         return gpm->data[(y * factor) * gridsize + (x * factor)];
608 }
609
610 /* threshold to move before updating the brush rotation */
611 #define RAKE_THRESHHOLD 20
612
613 void paint_update_brush_rake_rotation(UnifiedPaintSettings *ups, Brush *brush, float rotation)
614 {
615         if (brush->mtex.brush_angle_mode & MTEX_ANGLE_RAKE)
616                 ups->brush_rotation = rotation;
617         else
618                 ups->brush_rotation = 0.0f;
619
620         if (brush->mask_mtex.brush_angle_mode & MTEX_ANGLE_RAKE)
621                 ups->brush_rotation_sec = rotation;
622         else
623                 ups->brush_rotation_sec = 0.0f;
624 }
625
626 void paint_calculate_rake_rotation(UnifiedPaintSettings *ups, Brush *brush, const float mouse_pos[2])
627 {
628         if ((brush->mtex.brush_angle_mode & MTEX_ANGLE_RAKE) || (brush->mask_mtex.brush_angle_mode & MTEX_ANGLE_RAKE)) {
629                 const float r = RAKE_THRESHHOLD;
630                 float rotation;
631
632                 float dpos[2];
633                 sub_v2_v2v2(dpos, ups->last_rake, mouse_pos);
634
635                 if (len_squared_v2(dpos) >= r * r) {
636                         rotation = atan2f(dpos[0], dpos[1]);
637
638                         copy_v2_v2(ups->last_rake, mouse_pos);
639
640                         ups->last_rake_angle = rotation;
641
642                         paint_update_brush_rake_rotation(ups, brush, rotation);
643                 }
644                 /* make sure we reset here to the last rotation to avoid accumulating
645                  * values in case a random rotation is also added */
646                 else {
647                         paint_update_brush_rake_rotation(ups, brush, ups->last_rake_angle);
648                 }
649         }
650         else {
651                 ups->brush_rotation = ups->brush_rotation_sec = 0.0f;
652         }
653 }
654
655 void BKE_sculptsession_free_deformMats(SculptSession *ss)
656 {
657         MEM_SAFE_FREE(ss->orig_cos);
658         MEM_SAFE_FREE(ss->deform_cos);
659         MEM_SAFE_FREE(ss->deform_imats);
660 }
661
662 /* Write out the sculpt dynamic-topology BMesh to the Mesh */
663 static void sculptsession_bm_to_me_update_data_only(Object *ob, bool reorder)
664 {
665         SculptSession *ss = ob->sculpt;
666
667         if (ss->bm) {
668                 if (ob->data) {
669                         BMIter iter;
670                         BMFace *efa;
671                         BM_ITER_MESH (efa, &iter, ss->bm, BM_FACES_OF_MESH) {
672                                 BM_elem_flag_set(efa, BM_ELEM_SMOOTH, ss->bm_smooth_shading);
673                         }
674                         if (reorder)
675                                 BM_log_mesh_elems_reorder(ss->bm, ss->bm_log);
676                         BM_mesh_bm_to_me(ss->bm, ob->data, (&(struct BMeshToMeshParams){0}));
677                 }
678         }
679 }
680
681 void BKE_sculptsession_bm_to_me(Object *ob, bool reorder)
682 {
683         if (ob && ob->sculpt) {
684                 sculptsession_bm_to_me_update_data_only(ob, reorder);
685
686                 /* ensure the objects DerivedMesh mesh doesn't hold onto arrays now realloc'd in the mesh [#34473] */
687                 DEG_id_tag_update(&ob->id, OB_RECALC_DATA);
688         }
689 }
690
691 void BKE_sculptsession_bm_to_me_for_render(Object *object)
692 {
693         if (object && object->sculpt) {
694                 if (object->sculpt->bm) {
695                         /* Ensure no points to old arrays are stored in DM
696                          *
697                          * Apparently, we could not use DAG_id_tag_update
698                          * here because this will lead to the while object
699                          * surface to disappear, so we'll release DM in place.
700                          */
701                         BKE_object_free_derived_caches(object);
702
703                         if (object->sculpt->pbvh) {
704                                 BKE_pbvh_free(object->sculpt->pbvh);
705                                 object->sculpt->pbvh = NULL;
706                         }
707
708                         sculptsession_bm_to_me_update_data_only(object, false);
709
710                         /* In contrast with sculptsession_bm_to_me no need in
711                          * DAG tag update here - derived mesh was freed and
712                          * old pointers are nowhere stored.
713                          */
714                 }
715         }
716 }
717
718 void BKE_sculptsession_free(Object *ob)
719 {
720         if (ob && ob->sculpt) {
721                 SculptSession *ss = ob->sculpt;
722                 DerivedMesh *dm = ob->derivedFinal;
723
724                 if (ss->bm) {
725                         BKE_sculptsession_bm_to_me(ob, true);
726                         BM_mesh_free(ss->bm);
727                 }
728
729                 if (ss->pbvh)
730                         BKE_pbvh_free(ss->pbvh);
731                 if (ss->bm_log)
732                         BM_log_free(ss->bm_log);
733
734                 if (dm && dm->getPBVH)
735                         dm->getPBVH(NULL, dm);  /* signal to clear */
736
737                 if (ss->texcache)
738                         MEM_freeN(ss->texcache);
739
740                 if (ss->tex_pool)
741                         BKE_image_pool_free(ss->tex_pool);
742
743                 if (ss->layer_co)
744                         MEM_freeN(ss->layer_co);
745
746                 if (ss->orig_cos)
747                         MEM_freeN(ss->orig_cos);
748                 if (ss->deform_cos)
749                         MEM_freeN(ss->deform_cos);
750                 if (ss->deform_imats)
751                         MEM_freeN(ss->deform_imats);
752
753                 MEM_freeN(ss);
754
755                 ob->sculpt = NULL;
756         }
757 }
758
759 /* Sculpt mode handles multires differently from regular meshes, but only if
760  * it's the last modifier on the stack and it is not on the first level */
761 MultiresModifierData *BKE_sculpt_multires_active(Scene *scene, Object *ob)
762 {
763         Mesh *me = (Mesh *)ob->data;
764         ModifierData *md;
765         VirtualModifierData virtualModifierData;
766
767         if (ob->sculpt && ob->sculpt->bm) {
768                 /* can't combine multires and dynamic topology */
769                 return NULL;
770         }
771
772         if (!CustomData_get_layer(&me->ldata, CD_MDISPS)) {
773                 /* multires can't work without displacement layer */
774                 return NULL;
775         }
776
777         for (md = modifiers_getVirtualModifierList(ob, &virtualModifierData); md; md = md->next) {
778                 if (md->type == eModifierType_Multires) {
779                         MultiresModifierData *mmd = (MultiresModifierData *)md;
780
781                         if (!modifier_isEnabled(scene, md, eModifierMode_Realtime))
782                                 continue;
783
784                         if (mmd->sculptlvl > 0) return mmd;
785                         else return NULL;
786                 }
787         }
788
789         return NULL;
790 }
791
792
793 /* Checks if there are any supported deformation modifiers active */
794 static bool sculpt_modifiers_active(Scene *scene, Sculpt *sd, Object *ob)
795 {
796         ModifierData *md;
797         Mesh *me = (Mesh *)ob->data;
798         MultiresModifierData *mmd = BKE_sculpt_multires_active(scene, ob);
799         VirtualModifierData virtualModifierData;
800
801         if (mmd || ob->sculpt->bm)
802                 return false;
803
804         /* non-locked shape keys could be handled in the same way as deformed mesh */
805         if ((ob->shapeflag & OB_SHAPE_LOCK) == 0 && me->key && ob->shapenr)
806                 return true;
807
808         md = modifiers_getVirtualModifierList(ob, &virtualModifierData);
809
810         /* exception for shape keys because we can edit those */
811         for (; md; md = md->next) {
812                 const ModifierTypeInfo *mti = modifierType_getInfo(md->type);
813                 if (!modifier_isEnabled(scene, md, eModifierMode_Realtime)) continue;
814                 if (ELEM(md->type, eModifierType_ShapeKey, eModifierType_Multires)) continue;
815
816                 if (mti->type == eModifierTypeType_OnlyDeform) return true;
817                 else if ((sd->flags & SCULPT_ONLY_DEFORM) == 0) return true;
818         }
819
820         return false;
821 }
822
823 /**
824  * \param need_mask So the DerivedMesh thats returned has mask data
825  */
826 void BKE_sculpt_update_mesh_elements(EvaluationContext *eval_ctx, Scene *scene, Sculpt *sd, Object *ob,
827                                      bool need_pmap, bool need_mask)
828 {
829         DerivedMesh *dm;
830         SculptSession *ss = ob->sculpt;
831         Mesh *me = ob->data;
832         MultiresModifierData *mmd = BKE_sculpt_multires_active(scene, ob);
833
834         ss->modifiers_active = sculpt_modifiers_active(scene, sd, ob);
835         ss->show_diffuse_color = (sd->flags & SCULPT_SHOW_DIFFUSE) != 0;
836
837         if (need_mask) {
838                 if (mmd == NULL) {
839                         if (!CustomData_has_layer(&me->vdata, CD_PAINT_MASK)) {
840                                 BKE_sculpt_mask_layers_ensure(ob, NULL);
841                         }
842                 }
843                 else {
844                         if (!CustomData_has_layer(&me->ldata, CD_GRID_PAINT_MASK)) {
845 #if 1
846                                 BKE_sculpt_mask_layers_ensure(ob, mmd);
847 #else                           /* if we wanted to support adding mask data while multi-res painting, we would need to do this */
848                                 if ((ED_sculpt_mask_layers_ensure(ob, mmd) & ED_SCULPT_MASK_LAYER_CALC_LOOP)) {
849                                         /* remake the derived mesh */
850                                         ob->recalc |= OB_RECALC_DATA;
851                                         BKE_object_handle_update(scene, ob);
852                                 }
853 #endif
854                         }
855                 }
856         }
857
858         /* tessfaces aren't used and will become invalid */
859         BKE_mesh_tessface_clear(me);
860
861         ss->kb = (mmd == NULL) ? BKE_keyblock_from_object(ob) : NULL;
862
863         dm = mesh_get_derived_final(eval_ctx, scene, ob, CD_MASK_BAREMESH);
864
865         if (mmd) {
866                 ss->multires = mmd;
867                 ss->totvert = dm->getNumVerts(dm);
868                 ss->totpoly = dm->getNumPolys(dm);
869                 ss->mvert = NULL;
870                 ss->mpoly = NULL;
871                 ss->mloop = NULL;
872         }
873         else {
874                 ss->totvert = me->totvert;
875                 ss->totpoly = me->totpoly;
876                 ss->mvert = me->mvert;
877                 ss->mpoly = me->mpoly;
878                 ss->mloop = me->mloop;
879                 ss->multires = NULL;
880                 ss->vmask = CustomData_get_layer(&me->vdata, CD_PAINT_MASK);
881         }
882
883         ss->pbvh = dm->getPBVH(ob, dm);
884         ss->pmap = (need_pmap && dm->getPolyMap) ? dm->getPolyMap(ob, dm) : NULL;
885
886         pbvh_show_diffuse_color_set(ss->pbvh, ss->show_diffuse_color);
887
888         if (ss->modifiers_active) {
889                 if (!ss->orig_cos) {
890                         int a;
891
892                         BKE_sculptsession_free_deformMats(ss);
893
894                         ss->orig_cos = (ss->kb) ? BKE_keyblock_convert_to_vertcos(ob, ss->kb) : BKE_mesh_vertexCos_get(me, NULL);
895
896                         BKE_crazyspace_build_sculpt(eval_ctx, scene, ob, &ss->deform_imats, &ss->deform_cos);
897                         BKE_pbvh_apply_vertCos(ss->pbvh, ss->deform_cos);
898
899                         for (a = 0; a < me->totvert; ++a) {
900                                 invert_m3(ss->deform_imats[a]);
901                         }
902                 }
903         }
904         else {
905                 BKE_sculptsession_free_deformMats(ss);
906         }
907
908         if (ss->kb != NULL && ss->deform_cos == NULL) {
909                 ss->deform_cos = BKE_keyblock_convert_to_vertcos(ob, ss->kb);
910         }
911
912         /* if pbvh is deformed, key block is already applied to it */
913         if (ss->kb) {
914                 bool pbvh_deformd = BKE_pbvh_isDeformed(ss->pbvh);
915                 if (!pbvh_deformd || ss->deform_cos == NULL) {
916                         float (*vertCos)[3] = BKE_keyblock_convert_to_vertcos(ob, ss->kb);
917
918                         if (vertCos) {
919                                 if (!pbvh_deformd) {
920                                         /* apply shape keys coordinates to PBVH */
921                                         BKE_pbvh_apply_vertCos(ss->pbvh, vertCos);
922                                 }
923                                 if (ss->deform_cos == NULL) {
924                                         ss->deform_cos = vertCos;
925                                 }
926                                 if (vertCos != ss->deform_cos) {
927                                         MEM_freeN(vertCos);
928                                 }
929                         }
930                 }
931         }
932
933         /* 2.8x - avoid full mesh update! */
934         BKE_mesh_batch_cache_dirty(me, BKE_MESH_BATCH_DIRTY_SCULPT_COORDS);
935 }
936
937 int BKE_sculpt_mask_layers_ensure(Object *ob, MultiresModifierData *mmd)
938 {
939         const float *paint_mask;
940         Mesh *me = ob->data;
941         int ret = 0;
942
943         paint_mask = CustomData_get_layer(&me->vdata, CD_PAINT_MASK);
944
945         /* if multires is active, create a grid paint mask layer if there
946          * isn't one already */
947         if (mmd && !CustomData_has_layer(&me->ldata, CD_GRID_PAINT_MASK)) {
948                 GridPaintMask *gmask;
949                 int level = max_ii(1, mmd->sculptlvl);
950                 int gridsize = BKE_ccg_gridsize(level);
951                 int gridarea = gridsize * gridsize;
952                 int i, j;
953
954                 gmask = CustomData_add_layer(&me->ldata, CD_GRID_PAINT_MASK,
955                                              CD_CALLOC, NULL, me->totloop);
956
957                 for (i = 0; i < me->totloop; i++) {
958                         GridPaintMask *gpm = &gmask[i];
959
960                         gpm->level = level;
961                         gpm->data = MEM_callocN(sizeof(float) * gridarea,
962                                                 "GridPaintMask.data");
963                 }
964
965                 /* if vertices already have mask, copy into multires data */
966                 if (paint_mask) {
967                         for (i = 0; i < me->totpoly; i++) {
968                                 const MPoly *p = &me->mpoly[i];
969                                 float avg = 0;
970
971                                 /* mask center */
972                                 for (j = 0; j < p->totloop; j++) {
973                                         const MLoop *l = &me->mloop[p->loopstart + j];
974                                         avg += paint_mask[l->v];
975                                 }
976                                 avg /= (float)p->totloop;
977
978                                 /* fill in multires mask corner */
979                                 for (j = 0; j < p->totloop; j++) {
980                                         GridPaintMask *gpm = &gmask[p->loopstart + j];
981                                         const MLoop *l = &me->mloop[p->loopstart + j];
982                                         const MLoop *prev = ME_POLY_LOOP_PREV(me->mloop, p, j);
983                                         const MLoop *next = ME_POLY_LOOP_NEXT(me->mloop, p, j);
984
985                                         gpm->data[0] = avg;
986                                         gpm->data[1] = (paint_mask[l->v] +
987                                                         paint_mask[next->v]) * 0.5f;
988                                         gpm->data[2] = (paint_mask[l->v] +
989                                                         paint_mask[prev->v]) * 0.5f;
990                                         gpm->data[3] = paint_mask[l->v];
991                                 }
992                         }
993                 }
994
995                 ret |= SCULPT_MASK_LAYER_CALC_LOOP;
996         }
997
998         /* create vertex paint mask layer if there isn't one already */
999         if (!paint_mask) {
1000                 CustomData_add_layer(&me->vdata, CD_PAINT_MASK,
1001                                      CD_CALLOC, NULL, me->totvert);
1002                 ret |= SCULPT_MASK_LAYER_CALC_VERT;
1003         }
1004
1005         return ret;
1006 }