Cleanup: BKE_library: remove 'test' param of id_copy.
[blender.git] / source / blender / blenkernel / intern / paint.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software  Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2009 by Nicholas Bishop
17  * All rights reserved.
18  */
19
20 /** \file blender/blenkernel/intern/paint.c
21  *  \ingroup bke
22  */
23
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include "MEM_guardedalloc.h"
28
29 #include "DNA_object_types.h"
30 #include "DNA_mesh_types.h"
31 #include "DNA_meshdata_types.h"
32 #include "DNA_modifier_types.h"
33 #include "DNA_scene_types.h"
34 #include "DNA_brush_types.h"
35 #include "DNA_space_types.h"
36 #include "DNA_gpencil_types.h"
37 #include "DNA_workspace_types.h"
38
39 #include "BLI_bitmap.h"
40 #include "BLI_utildefines.h"
41 #include "BLI_math_vector.h"
42 #include "BLI_listbase.h"
43
44 #include "BLT_translation.h"
45
46 #include "BKE_animsys.h"
47 #include "BKE_brush.h"
48 #include "BKE_ccg.h"
49 #include "BKE_colortools.h"
50 #include "BKE_deform.h"
51 #include "BKE_main.h"
52 #include "BKE_context.h"
53 #include "BKE_crazyspace.h"
54 #include "BKE_gpencil.h"
55 #include "BKE_image.h"
56 #include "BKE_key.h"
57 #include "BKE_library.h"
58 #include "BKE_mesh.h"
59 #include "BKE_mesh_mapping.h"
60 #include "BKE_mesh_runtime.h"
61 #include "BKE_modifier.h"
62 #include "BKE_object.h"
63 #include "BKE_paint.h"
64 #include "BKE_pbvh.h"
65 #include "BKE_subdiv_ccg.h"
66 #include "BKE_subsurf.h"
67
68 #include "DEG_depsgraph.h"
69 #include "DEG_depsgraph_query.h"
70
71 #include "RNA_enum_types.h"
72
73 #include "bmesh.h"
74
75 const char PAINT_CURSOR_SCULPT[3] = {255, 100, 100};
76 const char PAINT_CURSOR_VERTEX_PAINT[3] = {255, 255, 255};
77 const char PAINT_CURSOR_WEIGHT_PAINT[3] = {200, 200, 255};
78 const char PAINT_CURSOR_TEXTURE_PAINT[3] = {255, 255, 255};
79
80 static eOverlayControlFlags overlay_flags = 0;
81
82 void BKE_paint_invalidate_overlay_tex(Scene *scene, ViewLayer *view_layer, const Tex *tex)
83 {
84         Paint *p = BKE_paint_get_active(scene, view_layer);
85         Brush *br = p->brush;
86
87         if (!br)
88                 return;
89
90         if (br->mtex.tex == tex)
91                 overlay_flags |= PAINT_OVERLAY_INVALID_TEXTURE_PRIMARY;
92         if (br->mask_mtex.tex == tex)
93                 overlay_flags |= PAINT_OVERLAY_INVALID_TEXTURE_SECONDARY;
94 }
95
96 void BKE_paint_invalidate_cursor_overlay(Scene *scene, ViewLayer *view_layer, CurveMapping *curve)
97 {
98         Paint *p = BKE_paint_get_active(scene, view_layer);
99         Brush *br = p->brush;
100
101         if (br && br->curve == curve)
102                 overlay_flags |= PAINT_OVERLAY_INVALID_CURVE;
103 }
104
105 void BKE_paint_invalidate_overlay_all(void)
106 {
107         overlay_flags |= (PAINT_OVERLAY_INVALID_TEXTURE_SECONDARY |
108                           PAINT_OVERLAY_INVALID_TEXTURE_PRIMARY |
109                           PAINT_OVERLAY_INVALID_CURVE);
110 }
111
112 eOverlayControlFlags BKE_paint_get_overlay_flags(void)
113 {
114         return overlay_flags;
115 }
116
117 void BKE_paint_set_overlay_override(eOverlayFlags flags)
118 {
119         if (flags & BRUSH_OVERLAY_OVERRIDE_MASK) {
120                 if (flags & BRUSH_OVERLAY_CURSOR_OVERRIDE_ON_STROKE)
121                         overlay_flags |= PAINT_OVERLAY_OVERRIDE_CURSOR;
122                 if (flags & BRUSH_OVERLAY_PRIMARY_OVERRIDE_ON_STROKE)
123                         overlay_flags |= PAINT_OVERLAY_OVERRIDE_PRIMARY;
124                 if (flags & BRUSH_OVERLAY_SECONDARY_OVERRIDE_ON_STROKE)
125                         overlay_flags |= PAINT_OVERLAY_OVERRIDE_SECONDARY;
126         }
127         else {
128                 overlay_flags &= ~(PAINT_OVERRIDE_MASK);
129         }
130 }
131
132 void BKE_paint_reset_overlay_invalid(eOverlayControlFlags flag)
133 {
134         overlay_flags &= ~(flag);
135 }
136
137 Paint *BKE_paint_get_active_from_paintmode(Scene *sce, ePaintMode mode)
138 {
139         if (sce) {
140                 ToolSettings *ts = sce->toolsettings;
141
142                 switch (mode) {
143                         case PAINT_MODE_SCULPT:
144                                 return &ts->sculpt->paint;
145                         case PAINT_MODE_VERTEX:
146                                 return &ts->vpaint->paint;
147                         case PAINT_MODE_WEIGHT:
148                                 return &ts->wpaint->paint;
149                         case PAINT_MODE_TEXTURE_2D:
150                         case PAINT_MODE_TEXTURE_3D:
151                                 return &ts->imapaint.paint;
152                         case PAINT_MODE_SCULPT_UV:
153                                 return &ts->uvsculpt->paint;
154                         case PAINT_MODE_GPENCIL:
155                                 return &ts->gp_paint->paint;
156                         case PAINT_MODE_INVALID:
157                                 return NULL;
158                         default:
159                                 return &ts->imapaint.paint;
160                 }
161         }
162
163         return NULL;
164 }
165
166 const EnumPropertyItem *BKE_paint_get_tool_enum_from_paintmode(ePaintMode mode)
167 {
168         switch (mode) {
169                 case PAINT_MODE_SCULPT:
170                         return rna_enum_brush_sculpt_tool_items;
171                 case PAINT_MODE_VERTEX:
172                         return rna_enum_brush_vertex_tool_items;
173                 case PAINT_MODE_WEIGHT:
174                         return rna_enum_brush_weight_tool_items;
175                 case PAINT_MODE_TEXTURE_2D:
176                 case PAINT_MODE_TEXTURE_3D:
177                         return rna_enum_brush_image_tool_items;
178                 case PAINT_MODE_SCULPT_UV:
179                         return NULL;
180                 case PAINT_MODE_GPENCIL:
181                         return rna_enum_brush_gpencil_types_items;
182                 case PAINT_MODE_INVALID:
183                         break;
184         }
185         return NULL;
186 }
187
188 const char *BKE_paint_get_tool_prop_id_from_paintmode(ePaintMode mode)
189 {
190         switch (mode) {
191                 case PAINT_MODE_SCULPT: return "sculpt_tool";
192                 case PAINT_MODE_VERTEX: return "vertex_tool";
193                 case PAINT_MODE_WEIGHT: return "weight_tool";
194                 case PAINT_MODE_TEXTURE_2D:
195                 case PAINT_MODE_TEXTURE_3D: return "image_tool";
196                 case PAINT_MODE_GPENCIL: return "gpencil_tool";
197                 default:
198                         /* invalid paint mode */
199                         return NULL;
200         }
201 }
202
203 Paint *BKE_paint_get_active(Scene *sce, ViewLayer *view_layer)
204 {
205         if (sce && view_layer) {
206                 ToolSettings *ts = sce->toolsettings;
207
208                 if (view_layer->basact && view_layer->basact->object) {
209                         switch (view_layer->basact->object->mode) {
210                                 case OB_MODE_SCULPT:
211                                         return &ts->sculpt->paint;
212                                 case OB_MODE_VERTEX_PAINT:
213                                         return &ts->vpaint->paint;
214                                 case OB_MODE_WEIGHT_PAINT:
215                                         return &ts->wpaint->paint;
216                                 case OB_MODE_TEXTURE_PAINT:
217                                         return &ts->imapaint.paint;
218                                 case OB_MODE_PAINT_GPENCIL:
219                                         return &ts->gp_paint->paint;
220                                 case OB_MODE_EDIT:
221                                         if (ts->use_uv_sculpt)
222                                                 return &ts->uvsculpt->paint;
223                                         return &ts->imapaint.paint;
224                                 default:
225                                         break;
226                         }
227                 }
228
229                 /* default to image paint */
230                 return &ts->imapaint.paint;
231         }
232
233         return NULL;
234 }
235
236 Paint *BKE_paint_get_active_from_context(const bContext *C)
237 {
238         Scene *sce = CTX_data_scene(C);
239         ViewLayer *view_layer = CTX_data_view_layer(C);
240         SpaceImage *sima;
241
242         if (sce && view_layer) {
243                 ToolSettings *ts = sce->toolsettings;
244                 Object *obact = NULL;
245
246                 if (view_layer->basact && view_layer->basact->object)
247                         obact = view_layer->basact->object;
248
249                 if ((sima = CTX_wm_space_image(C)) != NULL) {
250                         if (obact && obact->mode == OB_MODE_EDIT) {
251                                 if (sima->mode == SI_MODE_PAINT)
252                                         return &ts->imapaint.paint;
253                                 else if (ts->use_uv_sculpt)
254                                         return &ts->uvsculpt->paint;
255                         }
256                         else {
257                                 return &ts->imapaint.paint;
258                         }
259                 }
260                 else {
261                         return BKE_paint_get_active(sce, view_layer);
262                 }
263         }
264
265         return NULL;
266 }
267
268 ePaintMode BKE_paintmode_get_active_from_context(const bContext *C)
269 {
270         Scene *sce = CTX_data_scene(C);
271         ViewLayer *view_layer = CTX_data_view_layer(C);
272         SpaceImage *sima;
273
274         if (sce && view_layer) {
275                 ToolSettings *ts = sce->toolsettings;
276                 Object *obact = NULL;
277
278                 if (view_layer->basact && view_layer->basact->object)
279                         obact = view_layer->basact->object;
280
281                 if ((sima = CTX_wm_space_image(C)) != NULL) {
282                         if (obact && obact->mode == OB_MODE_EDIT) {
283                                 if (sima->mode == SI_MODE_PAINT)
284                                         return PAINT_MODE_TEXTURE_2D;
285                                 else if (ts->use_uv_sculpt)
286                                         return PAINT_MODE_SCULPT_UV;
287                         }
288                         else {
289                                 return PAINT_MODE_TEXTURE_2D;
290                         }
291                 }
292                 else if (obact) {
293                         switch (obact->mode) {
294                                 case OB_MODE_SCULPT:
295                                         return PAINT_MODE_SCULPT;
296                                 case OB_MODE_VERTEX_PAINT:
297                                         return PAINT_MODE_VERTEX;
298                                 case OB_MODE_WEIGHT_PAINT:
299                                         return PAINT_MODE_WEIGHT;
300                                 case OB_MODE_TEXTURE_PAINT:
301                                         return PAINT_MODE_TEXTURE_3D;
302                                 case OB_MODE_EDIT:
303                                         if (ts->use_uv_sculpt)
304                                                 return PAINT_MODE_SCULPT_UV;
305                                         return PAINT_MODE_TEXTURE_2D;
306                                 default:
307                                         return PAINT_MODE_TEXTURE_2D;
308                         }
309                 }
310                 else {
311                         /* default to image paint */
312                         return PAINT_MODE_TEXTURE_2D;
313                 }
314         }
315
316         return PAINT_MODE_INVALID;
317 }
318
319 ePaintMode BKE_paintmode_get_from_tool(const struct bToolRef *tref)
320 {
321         if (tref->space_type == SPACE_VIEW3D) {
322                 switch (tref->mode) {
323                         case CTX_MODE_SCULPT:
324                                 return PAINT_MODE_SCULPT;
325                         case CTX_MODE_PAINT_VERTEX:
326                                 return PAINT_MODE_VERTEX;
327                         case CTX_MODE_PAINT_WEIGHT:
328                                 return PAINT_MODE_WEIGHT;
329                         case CTX_MODE_PAINT_GPENCIL:
330                                 return PAINT_MODE_GPENCIL;
331                         case CTX_MODE_PAINT_TEXTURE:
332                                 return PAINT_MODE_TEXTURE_3D;
333                 }
334         }
335         else if (tref->space_type == SPACE_IMAGE) {
336                 switch (tref->mode) {
337                         case SI_MODE_PAINT:
338                                 return PAINT_MODE_TEXTURE_2D;
339                 }
340         }
341
342         return PAINT_MODE_INVALID;
343 }
344
345 Brush *BKE_paint_brush(Paint *p)
346 {
347         return p ? p->brush : NULL;
348 }
349
350 void BKE_paint_brush_set(Paint *p, Brush *br)
351 {
352         if (p) {
353                 id_us_min((ID *)p->brush);
354                 id_us_plus((ID *)br);
355                 p->brush = br;
356
357                 BKE_paint_toolslots_brush_update(p);
358         }
359 }
360
361 void BKE_paint_runtime_init(const ToolSettings *ts, Paint *paint)
362 {
363         if (paint == &ts->imapaint.paint) {
364                 paint->runtime.tool_offset = offsetof(Brush, imagepaint_tool);
365                 paint->runtime.ob_mode = OB_MODE_TEXTURE_PAINT;
366         }
367         else if (paint == &ts->sculpt->paint) {
368                 paint->runtime.tool_offset = offsetof(Brush, sculpt_tool);
369                 paint->runtime.ob_mode = OB_MODE_SCULPT;
370         }
371         else if (paint == &ts->vpaint->paint) {
372                 paint->runtime.tool_offset = offsetof(Brush, vertexpaint_tool);
373                 paint->runtime.ob_mode = OB_MODE_VERTEX_PAINT;
374         }
375         else if (paint == &ts->wpaint->paint) {
376                 paint->runtime.tool_offset = offsetof(Brush, weightpaint_tool);
377                 paint->runtime.ob_mode = OB_MODE_WEIGHT_PAINT;
378         }
379         else if (paint == &ts->gp_paint->paint) {
380                 paint->runtime.tool_offset = offsetof(Brush, gpencil_tool);
381                 paint->runtime.ob_mode = OB_MODE_PAINT_GPENCIL;
382         }
383         else if (paint == &ts->uvsculpt->paint) {
384                 /* We don't use these yet. */
385                 paint->runtime.tool_offset = 0;
386                 paint->runtime.ob_mode = 0;
387         }
388         else {
389                 BLI_assert(0);
390         }
391 }
392
393 uint BKE_paint_get_brush_tool_offset_from_paintmode(const ePaintMode mode)
394 {
395         switch (mode) {
396                 case PAINT_MODE_TEXTURE_2D:
397                 case PAINT_MODE_TEXTURE_3D:
398                         return offsetof(Brush, imagepaint_tool);
399                 case PAINT_MODE_SCULPT:
400                         return offsetof(Brush, sculpt_tool);
401                 case PAINT_MODE_VERTEX:
402                         return offsetof(Brush, vertexpaint_tool);
403                 case PAINT_MODE_WEIGHT:
404                         return offsetof(Brush, weightpaint_tool);
405                 case PAINT_MODE_GPENCIL:
406                         return offsetof(Brush, gpencil_tool);
407                 case PAINT_MODE_SCULPT_UV:
408                 case PAINT_MODE_INVALID:
409                         break; /* We don't use these yet. */
410         }
411         return 0;
412 }
413
414 /** Free (or release) any data used by this paint curve (does not free the pcurve itself). */
415 void BKE_paint_curve_free(PaintCurve *pc)
416 {
417         MEM_SAFE_FREE(pc->points);
418         pc->tot_points = 0;
419 }
420
421 PaintCurve *BKE_paint_curve_add(Main *bmain, const char *name)
422 {
423         PaintCurve *pc;
424
425         pc = BKE_libblock_alloc(bmain, ID_PC, name, 0);
426
427         return pc;
428 }
429
430 /**
431  * Only copy internal data of PaintCurve ID from source to already allocated/initialized destination.
432  * You probably nerver want to use that directly, use id_copy or BKE_id_copy_ex for typical needs.
433  *
434  * WARNING! This function will not handle ID user count!
435  *
436  * \param flag: Copying options (see BKE_library.h's LIB_ID_COPY_... flags for more).
437  */
438 void BKE_paint_curve_copy_data(Main *UNUSED(bmain), PaintCurve *pc_dst, const PaintCurve *pc_src, const int UNUSED(flag))
439 {
440         if (pc_src->tot_points != 0) {
441                 pc_dst->points = MEM_dupallocN(pc_src->points);
442         }
443 }
444
445 PaintCurve *BKE_paint_curve_copy(Main *bmain, const PaintCurve *pc)
446 {
447         PaintCurve *pc_copy;
448         BKE_id_copy_ex(bmain, &pc->id, (ID **)&pc_copy, 0);
449         return pc_copy;
450 }
451
452 void BKE_paint_curve_make_local(Main *bmain, PaintCurve *pc, const bool lib_local)
453 {
454         BKE_id_make_local_generic(bmain, &pc->id, true, lib_local);
455 }
456
457 Palette *BKE_paint_palette(Paint *p)
458 {
459         return p ? p->palette : NULL;
460 }
461
462 void BKE_paint_palette_set(Paint *p, Palette *palette)
463 {
464         if (p) {
465                 id_us_min((ID *)p->palette);
466                 p->palette = palette;
467                 id_us_plus((ID *)p->palette);
468         }
469 }
470
471 void BKE_paint_curve_set(Brush *br, PaintCurve *pc)
472 {
473         if (br) {
474                 id_us_min((ID *)br->paint_curve);
475                 br->paint_curve = pc;
476                 id_us_plus((ID *)br->paint_curve);
477         }
478 }
479
480 void BKE_paint_curve_clamp_endpoint_add_index(PaintCurve *pc, const int add_index)
481 {
482         pc->add_index = (add_index || pc->tot_points == 1) ? (add_index + 1) : 0;
483 }
484
485 /* remove colour from palette. Must be certain color is inside the palette! */
486 void BKE_palette_color_remove(Palette *palette, PaletteColor *color)
487 {
488         if (BLI_listbase_count_at_most(&palette->colors, palette->active_color) == palette->active_color) {
489                 palette->active_color--;
490         }
491
492         BLI_remlink(&palette->colors, color);
493
494         if (palette->active_color < 0 && !BLI_listbase_is_empty(&palette->colors)) {
495                 palette->active_color = 0;
496         }
497
498         MEM_freeN(color);
499 }
500
501 void BKE_palette_clear(Palette *palette)
502 {
503         BLI_freelistN(&palette->colors);
504         palette->active_color = 0;
505 }
506
507 Palette *BKE_palette_add(Main *bmain, const char *name)
508 {
509         Palette *palette = BKE_id_new(bmain, ID_PAL, name);
510         return palette;
511 }
512
513 /**
514  * Only copy internal data of Palette ID from source to already allocated/initialized destination.
515  * You probably nerver want to use that directly, use id_copy or BKE_id_copy_ex for typical needs.
516  *
517  * WARNING! This function will not handle ID user count!
518  *
519  * \param flag: Copying options (see BKE_library.h's LIB_ID_COPY_... flags for more).
520  */
521 void BKE_palette_copy_data(Main *UNUSED(bmain), Palette *palette_dst, const Palette *palette_src, const int UNUSED(flag))
522 {
523         BLI_duplicatelist(&palette_dst->colors, &palette_src->colors);
524 }
525
526 Palette *BKE_palette_copy(Main *bmain, const Palette *palette)
527 {
528         Palette *palette_copy;
529         BKE_id_copy_ex(bmain, &palette->id, (ID **)&palette_copy, 0);
530         return palette_copy;
531 }
532
533 void BKE_palette_make_local(Main *bmain, Palette *palette, const bool lib_local)
534 {
535         BKE_id_make_local_generic(bmain, &palette->id, true, lib_local);
536 }
537
538 void BKE_palette_init(Palette *palette)
539 {
540         /* Enable fake user by default. */
541         id_fake_user_set(&palette->id);
542 }
543
544 /** Free (or release) any data used by this palette (does not free the palette itself). */
545 void BKE_palette_free(Palette *palette)
546 {
547         BLI_freelistN(&palette->colors);
548 }
549
550 PaletteColor *BKE_palette_color_add(Palette *palette)
551 {
552         PaletteColor *color = MEM_callocN(sizeof(*color), "Palette Color");
553         BLI_addtail(&palette->colors, color);
554         return color;
555 }
556
557 bool BKE_palette_is_empty(const struct Palette *palette)
558 {
559         return BLI_listbase_is_empty(&palette->colors);
560 }
561
562 /* are we in vertex paint or weight paint face select mode? */
563 bool BKE_paint_select_face_test(Object *ob)
564 {
565         return ( (ob != NULL) &&
566                  (ob->type == OB_MESH) &&
567                  (ob->data != NULL) &&
568                  (((Mesh *)ob->data)->editflag & ME_EDIT_PAINT_FACE_SEL) &&
569                  (ob->mode & (OB_MODE_VERTEX_PAINT | OB_MODE_WEIGHT_PAINT | OB_MODE_TEXTURE_PAINT))
570                  );
571 }
572
573 /* are we in weight paint vertex select mode? */
574 bool BKE_paint_select_vert_test(Object *ob)
575 {
576         return ( (ob != NULL) &&
577                  (ob->type == OB_MESH) &&
578                  (ob->data != NULL) &&
579                  (((Mesh *)ob->data)->editflag & ME_EDIT_PAINT_VERT_SEL) &&
580                  (ob->mode & OB_MODE_WEIGHT_PAINT || ob->mode & OB_MODE_VERTEX_PAINT)
581                  );
582 }
583
584 /**
585  * used to check if selection is possible
586  * (when we don't care if its face or vert)
587  */
588 bool BKE_paint_select_elem_test(Object *ob)
589 {
590         return (BKE_paint_select_vert_test(ob) ||
591                 BKE_paint_select_face_test(ob));
592 }
593
594 void BKE_paint_cavity_curve_preset(Paint *p, int preset)
595 {
596         CurveMap *cm = NULL;
597
598         if (!p->cavity_curve)
599                 p->cavity_curve = curvemapping_add(1, 0, 0, 1, 1);
600
601         cm = p->cavity_curve->cm;
602         cm->flag &= ~CUMA_EXTEND_EXTRAPOLATE;
603
604         p->cavity_curve->preset = preset;
605         curvemap_reset(cm, &p->cavity_curve->clipr, p->cavity_curve->preset, CURVEMAP_SLOPE_POSITIVE);
606         curvemapping_changed(p->cavity_curve, false);
607 }
608
609 eObjectMode BKE_paint_object_mode_from_paintmode(ePaintMode mode)
610 {
611         switch (mode) {
612                 case PAINT_MODE_SCULPT:
613                         return OB_MODE_SCULPT;
614                 case PAINT_MODE_VERTEX:
615                         return OB_MODE_VERTEX_PAINT;
616                 case PAINT_MODE_WEIGHT:
617                         return OB_MODE_WEIGHT_PAINT;
618                 case PAINT_MODE_TEXTURE_2D:
619                 case PAINT_MODE_TEXTURE_3D:
620                         return OB_MODE_TEXTURE_PAINT;
621                 case PAINT_MODE_SCULPT_UV:
622                         return OB_MODE_EDIT;
623                 case PAINT_MODE_INVALID:
624                 default:
625                         return 0;
626         }
627 }
628
629 /**
630  * Call when entering each respective paint mode.
631  */
632 bool BKE_paint_ensure(const ToolSettings *ts, struct Paint **r_paint)
633 {
634         Paint *paint = NULL;
635         if (*r_paint) {
636                 /* Note: 'ts->imapaint' is ignored, it's not allocated. */
637                 BLI_assert(
638                         ELEM(*r_paint,
639                              &ts->gp_paint->paint,
640                              &ts->sculpt->paint,
641                              &ts->vpaint->paint,
642                              &ts->wpaint->paint,
643                              &ts->uvsculpt->paint));
644
645 #ifdef DEBUG
646                 struct Paint paint_test = **r_paint;
647                 BKE_paint_runtime_init(ts, *r_paint);
648                 /* Swap so debug doesn't hide errors when release fails. */
649                 SWAP(Paint, **r_paint, paint_test);
650                 BLI_assert(paint_test.runtime.ob_mode == (*r_paint)->runtime.ob_mode);
651                 BLI_assert(paint_test.runtime.tool_offset == (*r_paint)->runtime.tool_offset);
652 #endif
653                 return true;
654         }
655
656         if (((VPaint **)r_paint == &ts->vpaint) ||
657             ((VPaint **)r_paint == &ts->wpaint))
658         {
659                 VPaint *data = MEM_callocN(sizeof(*data), __func__);
660                 paint = &data->paint;
661         }
662         else if ((Sculpt **)r_paint == &ts->sculpt) {
663                 Sculpt *data = MEM_callocN(sizeof(*data), __func__);
664                 paint = &data->paint;
665
666                 /* Turn on X plane mirror symmetry by default */
667                 paint->symmetry_flags |= PAINT_SYMM_X;
668
669                 /* Make sure at least dyntopo subdivision is enabled */
670                 data->flags |= SCULPT_DYNTOPO_SUBDIVIDE | SCULPT_DYNTOPO_COLLAPSE;
671         }
672         else if ((GpPaint **)r_paint == &ts->gp_paint) {
673                 GpPaint *data = MEM_callocN(sizeof(*data), __func__);
674                 paint = &data->paint;
675         }
676         else if ((UvSculpt **)r_paint == &ts->uvsculpt) {
677                 UvSculpt *data = MEM_callocN(sizeof(*data), __func__);
678                 paint = &data->paint;
679         }
680
681         paint->flags |= PAINT_SHOW_BRUSH;
682
683         *r_paint = paint;
684
685         BKE_paint_runtime_init(ts, paint);
686
687         return false;
688 }
689
690 void BKE_paint_init(Main *bmain, Scene *sce, ePaintMode mode, const char col[3])
691 {
692         UnifiedPaintSettings *ups = &sce->toolsettings->unified_paint_settings;
693         Paint *paint = BKE_paint_get_active_from_paintmode(sce, mode);
694
695         /* If there's no brush, create one */
696         if (PAINT_MODE_HAS_BRUSH(mode)) {
697                 Brush *brush = BKE_paint_brush(paint);
698                 if (brush == NULL) {
699                         eObjectMode ob_mode = BKE_paint_object_mode_from_paintmode(mode);
700                         brush = BKE_brush_first_search(bmain, ob_mode);
701                         if (!brush) {
702                                 brush = BKE_brush_add(bmain, "Brush", ob_mode);
703                                 id_us_min(&brush->id);  /* fake user only */
704                         }
705                         BKE_paint_brush_set(paint, brush);
706                 }
707         }
708
709         memcpy(paint->paint_cursor_col, col, 3);
710         paint->paint_cursor_col[3] = 128;
711         ups->last_stroke_valid = false;
712         zero_v3(ups->average_stroke_accum);
713         ups->average_stroke_counter = 0;
714         if (!paint->cavity_curve)
715                 BKE_paint_cavity_curve_preset(paint, CURVE_PRESET_LINE);
716 }
717
718 void BKE_paint_free(Paint *paint)
719 {
720         curvemapping_free(paint->cavity_curve);
721         MEM_SAFE_FREE(paint->tool_slots);
722 }
723
724 /* called when copying scene settings, so even if 'src' and 'tar' are the same
725  * still do a id_us_plus(), rather then if we were copying between 2 existing
726  * scenes where a matching value should decrease the existing user count as
727  * with paint_brush_set() */
728 void BKE_paint_copy(Paint *src, Paint *tar, const int flag)
729 {
730         tar->brush = src->brush;
731         tar->cavity_curve = curvemapping_copy(src->cavity_curve);
732         tar->tool_slots = MEM_dupallocN(src->tool_slots);
733
734         if ((flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0) {
735                 id_us_plus((ID *)tar->brush);
736                 id_us_plus((ID *)tar->palette);
737                 if (src->tool_slots != NULL) {
738                         for (int i = 0; i < tar->tool_slots_len; i++) {
739                                 id_us_plus((ID *)tar->tool_slots[i].brush);
740                         }
741                 }
742         }
743 }
744
745 void BKE_paint_stroke_get_average(Scene *scene, Object *ob, float stroke[3])
746 {
747         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
748         if (ups->last_stroke_valid && ups->average_stroke_counter > 0) {
749                 float fac = 1.0f / ups->average_stroke_counter;
750                 mul_v3_v3fl(stroke, ups->average_stroke_accum, fac);
751         }
752         else {
753                 copy_v3_v3(stroke, ob->obmat[3]);
754         }
755 }
756
757 /* returns non-zero if any of the face's vertices
758  * are hidden, zero otherwise */
759 bool paint_is_face_hidden(const MLoopTri *lt, const MVert *mvert, const MLoop *mloop)
760 {
761         return ((mvert[mloop[lt->tri[0]].v].flag & ME_HIDE) ||
762                 (mvert[mloop[lt->tri[1]].v].flag & ME_HIDE) ||
763                 (mvert[mloop[lt->tri[2]].v].flag & ME_HIDE));
764 }
765
766 /* returns non-zero if any of the corners of the grid
767  * face whose inner corner is at (x, y) are hidden,
768  * zero otherwise */
769 bool paint_is_grid_face_hidden(const unsigned int *grid_hidden,
770                               int gridsize, int x, int y)
771 {
772         /* skip face if any of its corners are hidden */
773         return (BLI_BITMAP_TEST(grid_hidden, y * gridsize + x) ||
774                 BLI_BITMAP_TEST(grid_hidden, y * gridsize + x + 1) ||
775                 BLI_BITMAP_TEST(grid_hidden, (y + 1) * gridsize + x + 1) ||
776                 BLI_BITMAP_TEST(grid_hidden, (y + 1) * gridsize + x));
777 }
778
779 /* Return true if all vertices in the face are visible, false otherwise */
780 bool paint_is_bmesh_face_hidden(BMFace *f)
781 {
782         BMLoop *l_iter;
783         BMLoop *l_first;
784
785         l_iter = l_first = BM_FACE_FIRST_LOOP(f);
786         do {
787                 if (BM_elem_flag_test(l_iter->v, BM_ELEM_HIDDEN)) {
788                         return true;
789                 }
790         } while ((l_iter = l_iter->next) != l_first);
791
792         return false;
793 }
794
795 float paint_grid_paint_mask(const GridPaintMask *gpm, unsigned level,
796                             unsigned x, unsigned y)
797 {
798         int factor = BKE_ccg_factor(level, gpm->level);
799         int gridsize = BKE_ccg_gridsize(gpm->level);
800
801         return gpm->data[(y * factor) * gridsize + (x * factor)];
802 }
803
804 /* threshold to move before updating the brush rotation */
805 #define RAKE_THRESHHOLD 20
806
807 void paint_update_brush_rake_rotation(UnifiedPaintSettings *ups, Brush *brush, float rotation)
808 {
809         if (brush->mtex.brush_angle_mode & MTEX_ANGLE_RAKE)
810                 ups->brush_rotation = rotation;
811         else
812                 ups->brush_rotation = 0.0f;
813
814         if (brush->mask_mtex.brush_angle_mode & MTEX_ANGLE_RAKE)
815                 ups->brush_rotation_sec = rotation;
816         else
817                 ups->brush_rotation_sec = 0.0f;
818 }
819
820 bool paint_calculate_rake_rotation(UnifiedPaintSettings *ups, Brush *brush, const float mouse_pos[2])
821 {
822         bool ok = false;
823         if ((brush->mtex.brush_angle_mode & MTEX_ANGLE_RAKE) || (brush->mask_mtex.brush_angle_mode & MTEX_ANGLE_RAKE)) {
824                 const float r = RAKE_THRESHHOLD;
825                 float rotation;
826
827                 float dpos[2];
828                 sub_v2_v2v2(dpos, ups->last_rake, mouse_pos);
829
830                 if (len_squared_v2(dpos) >= r * r) {
831                         rotation = atan2f(dpos[0], dpos[1]);
832
833                         copy_v2_v2(ups->last_rake, mouse_pos);
834
835                         ups->last_rake_angle = rotation;
836
837                         paint_update_brush_rake_rotation(ups, brush, rotation);
838                         ok = true;
839                 }
840                 /* make sure we reset here to the last rotation to avoid accumulating
841                  * values in case a random rotation is also added */
842                 else {
843                         paint_update_brush_rake_rotation(ups, brush, ups->last_rake_angle);
844                         ok = false;
845                 }
846         }
847         else {
848                 ups->brush_rotation = ups->brush_rotation_sec = 0.0f;
849                 ok = true;
850         }
851         return ok;
852 }
853
854 void BKE_sculptsession_free_deformMats(SculptSession *ss)
855 {
856         MEM_SAFE_FREE(ss->orig_cos);
857         MEM_SAFE_FREE(ss->deform_cos);
858         MEM_SAFE_FREE(ss->deform_imats);
859 }
860
861 void BKE_sculptsession_free_vwpaint_data(struct SculptSession *ss)
862 {
863         struct SculptVertexPaintGeomMap *gmap = NULL;
864         if (ss->mode_type == OB_MODE_VERTEX_PAINT) {
865                 gmap = &ss->mode.vpaint.gmap;
866
867                 MEM_SAFE_FREE(ss->mode.vpaint.previous_color);
868         }
869         else if (ss->mode_type == OB_MODE_WEIGHT_PAINT) {
870                 gmap = &ss->mode.wpaint.gmap;
871
872                 MEM_SAFE_FREE(ss->mode.wpaint.alpha_weight);
873                 if (ss->mode.wpaint.dvert_prev) {
874                         BKE_defvert_array_free_elems(ss->mode.wpaint.dvert_prev, ss->totvert);
875                         MEM_freeN(ss->mode.wpaint.dvert_prev);
876                         ss->mode.wpaint.dvert_prev = NULL;
877                 }
878         }
879         else {
880                 return;
881         }
882         MEM_SAFE_FREE(gmap->vert_to_loop);
883         MEM_SAFE_FREE(gmap->vert_map_mem);
884         MEM_SAFE_FREE(gmap->vert_to_poly);
885         MEM_SAFE_FREE(gmap->poly_map_mem);
886 }
887
888 /* Write out the sculpt dynamic-topology BMesh to the Mesh */
889 static void sculptsession_bm_to_me_update_data_only(Object *ob, bool reorder)
890 {
891         SculptSession *ss = ob->sculpt;
892
893         if (ss->bm) {
894                 if (ob->data) {
895                         BMIter iter;
896                         BMFace *efa;
897                         BM_ITER_MESH (efa, &iter, ss->bm, BM_FACES_OF_MESH) {
898                                 BM_elem_flag_set(efa, BM_ELEM_SMOOTH, ss->bm_smooth_shading);
899                         }
900                         if (reorder)
901                                 BM_log_mesh_elems_reorder(ss->bm, ss->bm_log);
902                         BM_mesh_bm_to_me(NULL, ss->bm, ob->data, (&(struct BMeshToMeshParams){.calc_object_remap = false,}));
903                 }
904         }
905 }
906
907 void BKE_sculptsession_bm_to_me(Object *ob, bool reorder)
908 {
909         if (ob && ob->sculpt) {
910                 sculptsession_bm_to_me_update_data_only(ob, reorder);
911
912                 /* ensure the objects evaluated mesh doesn't hold onto arrays now realloc'd in the mesh [#34473] */
913                 DEG_id_tag_update(&ob->id, ID_RECALC_GEOMETRY);
914         }
915 }
916
917 void BKE_sculptsession_bm_to_me_for_render(Object *object)
918 {
919         if (object && object->sculpt) {
920                 if (object->sculpt->bm) {
921                         /* Ensure no points to old arrays are stored in DM
922                          *
923                          * Apparently, we could not use DEG_id_tag_update
924                          * here because this will lead to the while object
925                          * surface to disappear, so we'll release DM in place.
926                          */
927                         BKE_object_free_derived_caches(object);
928
929                         if (object->sculpt->pbvh) {
930                                 BKE_pbvh_free(object->sculpt->pbvh);
931                                 object->sculpt->pbvh = NULL;
932                         }
933
934                         sculptsession_bm_to_me_update_data_only(object, false);
935
936                         /* In contrast with sculptsession_bm_to_me no need in
937                          * DAG tag update here - derived mesh was freed and
938                          * old pointers are nowhere stored.
939                          */
940                 }
941         }
942 }
943
944 void BKE_sculptsession_free(Object *ob)
945 {
946         if (ob && ob->sculpt) {
947                 SculptSession *ss = ob->sculpt;
948
949                 if (ss->bm) {
950                         BKE_sculptsession_bm_to_me(ob, true);
951                         BM_mesh_free(ss->bm);
952                 }
953
954                 if (ss->pbvh)
955                         BKE_pbvh_free(ss->pbvh);
956                 MEM_SAFE_FREE(ss->pmap);
957                 MEM_SAFE_FREE(ss->pmap_mem);
958                 if (ss->bm_log)
959                         BM_log_free(ss->bm_log);
960
961                 if (ss->texcache)
962                         MEM_freeN(ss->texcache);
963
964                 if (ss->tex_pool)
965                         BKE_image_pool_free(ss->tex_pool);
966
967                 if (ss->layer_co)
968                         MEM_freeN(ss->layer_co);
969
970                 if (ss->orig_cos)
971                         MEM_freeN(ss->orig_cos);
972                 if (ss->deform_cos)
973                         MEM_freeN(ss->deform_cos);
974                 if (ss->deform_imats)
975                         MEM_freeN(ss->deform_imats);
976
977                 BKE_sculptsession_free_vwpaint_data(ob->sculpt);
978
979                 MEM_freeN(ss);
980
981                 ob->sculpt = NULL;
982         }
983 }
984
985 /* Sculpt mode handles multires differently from regular meshes, but only if
986  * it's the last modifier on the stack and it is not on the first level */
987 MultiresModifierData *BKE_sculpt_multires_active(Scene *scene, Object *ob)
988 {
989         Mesh *me = (Mesh *)ob->data;
990         ModifierData *md;
991         VirtualModifierData virtualModifierData;
992
993         if (ob->sculpt && ob->sculpt->bm) {
994                 /* can't combine multires and dynamic topology */
995                 return NULL;
996         }
997
998         if (!CustomData_get_layer(&me->ldata, CD_MDISPS)) {
999                 /* multires can't work without displacement layer */
1000                 return NULL;
1001         }
1002
1003         for (md = modifiers_getVirtualModifierList(ob, &virtualModifierData); md; md = md->next) {
1004                 if (md->type == eModifierType_Multires) {
1005                         MultiresModifierData *mmd = (MultiresModifierData *)md;
1006
1007                         if (!modifier_isEnabled(scene, md, eModifierMode_Realtime))
1008                                 continue;
1009
1010                         if (mmd->sculptlvl > 0) return mmd;
1011                         else return NULL;
1012                 }
1013         }
1014
1015         return NULL;
1016 }
1017
1018
1019 /* Checks if there are any supported deformation modifiers active */
1020 static bool sculpt_modifiers_active(Scene *scene, Sculpt *sd, Object *ob)
1021 {
1022         ModifierData *md;
1023         Mesh *me = (Mesh *)ob->data;
1024         MultiresModifierData *mmd = BKE_sculpt_multires_active(scene, ob);
1025         VirtualModifierData virtualModifierData;
1026
1027         if (mmd || ob->sculpt->bm)
1028                 return false;
1029
1030         /* non-locked shape keys could be handled in the same way as deformed mesh */
1031         if ((ob->shapeflag & OB_SHAPE_LOCK) == 0 && me->key && ob->shapenr)
1032                 return true;
1033
1034         md = modifiers_getVirtualModifierList(ob, &virtualModifierData);
1035
1036         /* exception for shape keys because we can edit those */
1037         for (; md; md = md->next) {
1038                 const ModifierTypeInfo *mti = modifierType_getInfo(md->type);
1039                 if (!modifier_isEnabled(scene, md, eModifierMode_Realtime)) continue;
1040                 if (ELEM(md->type, eModifierType_ShapeKey, eModifierType_Multires)) continue;
1041
1042                 if (mti->type == eModifierTypeType_OnlyDeform) return true;
1043                 else if ((sd->flags & SCULPT_ONLY_DEFORM) == 0) return true;
1044         }
1045
1046         return false;
1047 }
1048
1049 /**
1050  * \param need_mask: So that the evaluated mesh that is returned has mask data.
1051  */
1052 void BKE_sculpt_update_mesh_elements(
1053         Depsgraph *depsgraph, Scene *scene, Sculpt *sd, Object *ob,
1054         bool need_pmap, bool need_mask)
1055 {
1056         /* TODO(sergey): Make sure ob points to an original object. This is what it
1057          * is supposed to be pointing to. The issue is, currently draw code takes
1058          * care of PBVH creation, even though this is something up to dependency
1059          * graph.
1060          * Probably, we need to being back logic which was checking for sculpt mode
1061          * and (re)create PBVH if needed in that case, similar to how DerivedMesh
1062          * was handling this.
1063          */
1064         ob = DEG_get_original_object(ob);
1065         Object *ob_eval = DEG_get_evaluated_object(depsgraph, ob);
1066
1067         SculptSession *ss = ob->sculpt;
1068         Mesh *me = BKE_object_get_original_mesh(ob);
1069         MultiresModifierData *mmd = BKE_sculpt_multires_active(scene, ob);
1070
1071         ss->modifiers_active = sculpt_modifiers_active(scene, sd, ob);
1072         ss->show_diffuse_color = (sd->flags & SCULPT_SHOW_DIFFUSE) != 0;
1073         ss->show_mask = (sd->flags & SCULPT_HIDE_MASK) == 0;
1074
1075         ss->building_vp_handle = false;
1076
1077         if (need_mask) {
1078                 if (mmd == NULL) {
1079                         if (!CustomData_has_layer(&me->vdata, CD_PAINT_MASK)) {
1080                                 BKE_sculpt_mask_layers_ensure(ob, NULL);
1081                         }
1082                 }
1083                 else {
1084                         if (!CustomData_has_layer(&me->ldata, CD_GRID_PAINT_MASK)) {
1085 #if 1
1086                                 BKE_sculpt_mask_layers_ensure(ob, mmd);
1087 #else                   /* if we wanted to support adding mask data while multi-res painting, we would need to do this */
1088                                 if ((ED_sculpt_mask_layers_ensure(ob, mmd) & ED_SCULPT_MASK_LAYER_CALC_LOOP)) {
1089                                         /* remake the derived mesh */
1090                                         ob->recalc |= ID_RECALC_GEOMETRY;
1091                                         BKE_object_handle_update(scene, ob);
1092                                 }
1093 #endif
1094                         }
1095                 }
1096         }
1097
1098         /* tessfaces aren't used and will become invalid */
1099         BKE_mesh_tessface_clear(me);
1100
1101         ss->kb = (mmd == NULL) ? BKE_keyblock_from_object(ob) : NULL;
1102
1103         Mesh *me_eval = mesh_get_eval_final(depsgraph, scene, ob_eval, CD_MASK_BAREMESH);
1104
1105         /* VWPaint require mesh info for loop lookup, so require sculpt mode here */
1106         if (mmd && ob->mode & OB_MODE_SCULPT) {
1107                 ss->multires = mmd;
1108                 ss->totvert = me_eval->totvert;
1109                 ss->totpoly = me_eval->totpoly;
1110                 ss->mvert = NULL;
1111                 ss->mpoly = NULL;
1112                 ss->mloop = NULL;
1113         }
1114         else {
1115                 ss->totvert = me->totvert;
1116                 ss->totpoly = me->totpoly;
1117                 ss->mvert = me->mvert;
1118                 ss->mpoly = me->mpoly;
1119                 ss->mloop = me->mloop;
1120                 ss->multires = NULL;
1121                 ss->vmask = CustomData_get_layer(&me->vdata, CD_PAINT_MASK);
1122         }
1123
1124         ss->subdiv_ccg = me_eval->runtime.subdiv_ccg;
1125
1126         PBVH *pbvh = BKE_sculpt_object_pbvh_ensure(depsgraph, ob);
1127         BLI_assert(pbvh == ss->pbvh);
1128         UNUSED_VARS_NDEBUG(pbvh);
1129         MEM_SAFE_FREE(ss->pmap);
1130         MEM_SAFE_FREE(ss->pmap_mem);
1131         if (need_pmap && ob->type == OB_MESH) {
1132                 BKE_mesh_vert_poly_map_create(
1133                             &ss->pmap, &ss->pmap_mem,
1134                             me->mpoly, me->mloop,
1135                             me->totvert, me->totpoly, me->totloop);
1136         }
1137
1138         pbvh_show_diffuse_color_set(ss->pbvh, ss->show_diffuse_color);
1139         pbvh_show_mask_set(ss->pbvh, ss->show_mask);
1140
1141         if (ss->modifiers_active) {
1142                 if (!ss->orig_cos) {
1143                         Object *object_orig = DEG_get_original_object(ob);
1144                         int a;
1145
1146                         BKE_sculptsession_free_deformMats(ss);
1147
1148                         ss->orig_cos = (ss->kb) ? BKE_keyblock_convert_to_vertcos(ob, ss->kb) : BKE_mesh_vertexCos_get(me, NULL);
1149
1150                         BKE_crazyspace_build_sculpt(depsgraph, scene, object_orig, &ss->deform_imats, &ss->deform_cos);
1151                         BKE_pbvh_apply_vertCos(ss->pbvh, ss->deform_cos, me->totvert);
1152
1153                         for (a = 0; a < me->totvert; ++a) {
1154                                 invert_m3(ss->deform_imats[a]);
1155                         }
1156                 }
1157         }
1158         else {
1159                 BKE_sculptsession_free_deformMats(ss);
1160         }
1161
1162         if (ss->kb != NULL && ss->deform_cos == NULL) {
1163                 ss->deform_cos = BKE_keyblock_convert_to_vertcos(ob, ss->kb);
1164         }
1165
1166         /* if pbvh is deformed, key block is already applied to it */
1167         if (ss->kb) {
1168                 bool pbvh_deformed = BKE_pbvh_isDeformed(ss->pbvh);
1169                 if (!pbvh_deformed || ss->deform_cos == NULL) {
1170                         float (*vertCos)[3] = BKE_keyblock_convert_to_vertcos(ob, ss->kb);
1171
1172                         if (vertCos) {
1173                                 if (!pbvh_deformed) {
1174                                         /* apply shape keys coordinates to PBVH */
1175                                         BKE_pbvh_apply_vertCos(ss->pbvh, vertCos, me->totvert);
1176                                 }
1177                                 if (ss->deform_cos == NULL) {
1178                                         ss->deform_cos = vertCos;
1179                                 }
1180                                 if (vertCos != ss->deform_cos) {
1181                                         MEM_freeN(vertCos);
1182                                 }
1183                         }
1184                 }
1185         }
1186
1187         /* 2.8x - avoid full mesh update! */
1188         BKE_mesh_batch_cache_dirty_tag(me, BKE_MESH_BATCH_DIRTY_SCULPT_COORDS);
1189 }
1190
1191 int BKE_sculpt_mask_layers_ensure(Object *ob, MultiresModifierData *mmd)
1192 {
1193         const float *paint_mask;
1194         Mesh *me = ob->data;
1195         int ret = 0;
1196
1197         paint_mask = CustomData_get_layer(&me->vdata, CD_PAINT_MASK);
1198
1199         /* if multires is active, create a grid paint mask layer if there
1200          * isn't one already */
1201         if (mmd && !CustomData_has_layer(&me->ldata, CD_GRID_PAINT_MASK)) {
1202                 GridPaintMask *gmask;
1203                 int level = max_ii(1, mmd->sculptlvl);
1204                 int gridsize = BKE_ccg_gridsize(level);
1205                 int gridarea = gridsize * gridsize;
1206                 int i, j;
1207
1208                 gmask = CustomData_add_layer(&me->ldata, CD_GRID_PAINT_MASK,
1209                                              CD_CALLOC, NULL, me->totloop);
1210
1211                 for (i = 0; i < me->totloop; i++) {
1212                         GridPaintMask *gpm = &gmask[i];
1213
1214                         gpm->level = level;
1215                         gpm->data = MEM_callocN(sizeof(float) * gridarea,
1216                                                 "GridPaintMask.data");
1217                 }
1218
1219                 /* if vertices already have mask, copy into multires data */
1220                 if (paint_mask) {
1221                         for (i = 0; i < me->totpoly; i++) {
1222                                 const MPoly *p = &me->mpoly[i];
1223                                 float avg = 0;
1224
1225                                 /* mask center */
1226                                 for (j = 0; j < p->totloop; j++) {
1227                                         const MLoop *l = &me->mloop[p->loopstart + j];
1228                                         avg += paint_mask[l->v];
1229                                 }
1230                                 avg /= (float)p->totloop;
1231
1232                                 /* fill in multires mask corner */
1233                                 for (j = 0; j < p->totloop; j++) {
1234                                         GridPaintMask *gpm = &gmask[p->loopstart + j];
1235                                         const MLoop *l = &me->mloop[p->loopstart + j];
1236                                         const MLoop *prev = ME_POLY_LOOP_PREV(me->mloop, p, j);
1237                                         const MLoop *next = ME_POLY_LOOP_NEXT(me->mloop, p, j);
1238
1239                                         gpm->data[0] = avg;
1240                                         gpm->data[1] = (paint_mask[l->v] +
1241                                                         paint_mask[next->v]) * 0.5f;
1242                                         gpm->data[2] = (paint_mask[l->v] +
1243                                                         paint_mask[prev->v]) * 0.5f;
1244                                         gpm->data[3] = paint_mask[l->v];
1245                                 }
1246                         }
1247                 }
1248
1249                 ret |= SCULPT_MASK_LAYER_CALC_LOOP;
1250         }
1251
1252         /* create vertex paint mask layer if there isn't one already */
1253         if (!paint_mask) {
1254                 CustomData_add_layer(&me->vdata, CD_PAINT_MASK,
1255                                      CD_CALLOC, NULL, me->totvert);
1256                 ret |= SCULPT_MASK_LAYER_CALC_VERT;
1257         }
1258
1259         return ret;
1260 }
1261
1262 void BKE_sculpt_toolsettings_data_ensure(struct Scene *scene)
1263 {
1264         BKE_paint_ensure(scene->toolsettings, (Paint **)&scene->toolsettings->sculpt);
1265
1266         Sculpt *sd = scene->toolsettings->sculpt;
1267         if (!sd->detail_size) {
1268                 sd->detail_size = 12;
1269         }
1270         if (!sd->detail_percent) {
1271                 sd->detail_percent = 25;
1272         }
1273         if (sd->constant_detail == 0.0f) {
1274                 sd->constant_detail = 3.0f;
1275         }
1276
1277         /* Set sane default tiling offsets */
1278         if (!sd->paint.tile_offset[0]) {
1279                 sd->paint.tile_offset[0] = 1.0f;
1280         }
1281         if (!sd->paint.tile_offset[1]) {
1282                 sd->paint.tile_offset[1] = 1.0f;
1283         }
1284         if (!sd->paint.tile_offset[2]) {
1285                 sd->paint.tile_offset[2] = 1.0f;
1286         }
1287 }
1288
1289 static bool check_sculpt_object_deformed(Object *object, const bool for_construction)
1290 {
1291         bool deformed = false;
1292
1293         /* Active modifiers means extra deformation, which can't be handled correct
1294          * on birth of PBVH and sculpt "layer" levels, so use PBVH only for internal brush
1295          * stuff and show final evaluated mesh so user would see actual object shape.
1296          */
1297         deformed |= object->sculpt->modifiers_active;
1298
1299         if (for_construction) {
1300                 deformed |= object->sculpt->kb != NULL;
1301         }
1302         else {
1303                 /* As in case with modifiers, we can't synchronize deformation made against
1304                  * PBVH and non-locked keyblock, so also use PBVH only for brushes and
1305                  * final DM to give final result to user.
1306                  */
1307                 deformed |= object->sculpt->kb && (object->shapeflag & OB_SHAPE_LOCK) == 0;
1308         }
1309
1310         return deformed;
1311 }
1312
1313 static PBVH *build_pbvh_for_dynamic_topology(Object *ob)
1314 {
1315         PBVH *pbvh = BKE_pbvh_new();
1316         BKE_pbvh_build_bmesh(pbvh, ob->sculpt->bm,
1317                              ob->sculpt->bm_smooth_shading,
1318                              ob->sculpt->bm_log, ob->sculpt->cd_vert_node_offset,
1319                              ob->sculpt->cd_face_node_offset);
1320         pbvh_show_diffuse_color_set(pbvh, ob->sculpt->show_diffuse_color);
1321         pbvh_show_mask_set(pbvh, ob->sculpt->show_mask);
1322         return pbvh;
1323 }
1324
1325 static PBVH *build_pbvh_from_regular_mesh(Object *ob, Mesh *me_eval_deform)
1326 {
1327         Mesh *me = BKE_object_get_original_mesh(ob);
1328         const int looptris_num = poly_to_tri_count(me->totpoly, me->totloop);
1329         PBVH *pbvh = BKE_pbvh_new();
1330
1331         MLoopTri *looptri = MEM_malloc_arrayN(
1332                 looptris_num, sizeof(*looptri), __func__);
1333
1334         BKE_mesh_recalc_looptri(
1335                 me->mloop, me->mpoly,
1336                 me->mvert,
1337                 me->totloop, me->totpoly,
1338                 looptri);
1339
1340         BKE_pbvh_build_mesh(
1341                 pbvh,
1342                 me->mpoly, me->mloop,
1343                 me->mvert, me->totvert, &me->vdata,
1344                 looptri, looptris_num);
1345
1346         pbvh_show_diffuse_color_set(pbvh, ob->sculpt->show_diffuse_color);
1347         pbvh_show_mask_set(pbvh, ob->sculpt->show_mask);
1348
1349         const bool is_deformed = check_sculpt_object_deformed(ob, true);
1350         if (is_deformed && me_eval_deform != NULL) {
1351                 int totvert;
1352                 float (*v_cos)[3] = BKE_mesh_vertexCos_get(me_eval_deform, &totvert);
1353                 BKE_pbvh_apply_vertCos(pbvh, v_cos, totvert);
1354                 MEM_freeN(v_cos);
1355         }
1356
1357         return pbvh;
1358 }
1359
1360 static PBVH *build_pbvh_from_ccg(Object *ob, SubdivCCG *subdiv_ccg)
1361 {
1362         CCGKey key;
1363         BKE_subdiv_ccg_key_top_level(&key, subdiv_ccg);
1364         PBVH *pbvh = BKE_pbvh_new();
1365         BKE_pbvh_build_grids(
1366                 pbvh,
1367                 subdiv_ccg->grids, subdiv_ccg->num_grids,
1368                 &key,
1369                 (void **)subdiv_ccg->grid_faces,
1370                 subdiv_ccg->grid_flag_mats,
1371                 subdiv_ccg->grid_hidden);
1372         pbvh_show_diffuse_color_set(pbvh, ob->sculpt->show_diffuse_color);
1373         pbvh_show_mask_set(pbvh, ob->sculpt->show_mask);
1374         return pbvh;
1375 }
1376
1377 PBVH *BKE_sculpt_object_pbvh_ensure(Depsgraph *depsgraph, Object *ob)
1378 {
1379         if (ob == NULL || ob->sculpt == NULL) {
1380                 return NULL;
1381         }
1382         PBVH *pbvh = ob->sculpt->pbvh;
1383         if (pbvh != NULL) {
1384                 /* Nothing to do, PBVH is already up to date. */
1385                 return pbvh;
1386         }
1387
1388         if (ob->sculpt->bm != NULL) {
1389                 /* Sculpting on a BMesh (dynamic-topology) gets a special PBVH. */
1390                 pbvh = build_pbvh_for_dynamic_topology(ob);
1391         }
1392         else {
1393                 Object *object_eval = DEG_get_evaluated_object(depsgraph, ob);
1394                 Mesh *mesh_eval = object_eval->data;
1395                 if (mesh_eval->runtime.subdiv_ccg != NULL) {
1396                         pbvh = build_pbvh_from_ccg(ob, mesh_eval->runtime.subdiv_ccg);
1397                 }
1398                 else if (ob->type == OB_MESH) {
1399                         Mesh *me_eval_deform = mesh_get_eval_deform(
1400                                 depsgraph, DEG_get_evaluated_scene(depsgraph), object_eval, CD_MASK_BAREMESH);
1401                         pbvh = build_pbvh_from_regular_mesh(ob, me_eval_deform);
1402                 }
1403         }
1404
1405         ob->sculpt->pbvh = pbvh;
1406         return pbvh;
1407 }