Merge branch 'master' into blender2.8
[blender.git] / source / blender / blenkernel / intern / paint.c
index 4ba9cbf..2fded4a 100644 (file)
@@ -41,6 +41,7 @@
 #include "DNA_scene_types.h"
 #include "DNA_brush_types.h"
 #include "DNA_space_types.h"
+#include "DNA_workspace_types.h"
 
 #include "BLI_bitmap.h"
 #include "BLI_utildefines.h"
 #include "BKE_main.h"
 #include "BKE_context.h"
 #include "BKE_crazyspace.h"
-#include "BKE_depsgraph.h"
 #include "BKE_global.h"
 #include "BKE_image.h"
 #include "BKE_key.h"
 #include "BKE_library.h"
 #include "BKE_mesh.h"
+#include "BKE_mesh_runtime.h"
 #include "BKE_modifier.h"
 #include "BKE_object.h"
 #include "BKE_paint.h"
 #include "BKE_pbvh.h"
 #include "BKE_subsurf.h"
 
+#include "DEG_depsgraph.h"
+
 #include "bmesh.h"
 
 const char PAINT_CURSOR_SCULPT[3] = {255, 100, 100};
@@ -74,9 +77,9 @@ const char PAINT_CURSOR_TEXTURE_PAINT[3] = {255, 255, 255};
 
 static eOverlayControlFlags overlay_flags = 0;
 
-void BKE_paint_invalidate_overlay_tex(Scene *scene, const Tex *tex)
+void BKE_paint_invalidate_overlay_tex(Scene *scene, ViewLayer *view_layer, const Tex *tex)
 {
-       Paint *p = BKE_paint_get_active(scene);
+       Paint *p = BKE_paint_get_active(scene, view_layer);
        Brush *br = p->brush;
 
        if (!br)
@@ -88,9 +91,9 @@ void BKE_paint_invalidate_overlay_tex(Scene *scene, const Tex *tex)
                overlay_flags |= PAINT_INVALID_OVERLAY_TEXTURE_SECONDARY;
 }
 
-void BKE_paint_invalidate_cursor_overlay(Scene *scene, CurveMapping *curve)
+void BKE_paint_invalidate_cursor_overlay(Scene *scene, ViewLayer *view_layer, CurveMapping *curve)
 {
-       Paint *p = BKE_paint_get_active(scene);
+       Paint *p = BKE_paint_get_active(scene, view_layer);
        Brush *br = p->brush;
 
        if (br && br->curve == curve)
@@ -156,13 +159,13 @@ Paint *BKE_paint_get_active_from_paintmode(Scene *sce, ePaintMode mode)
        return NULL;
 }
 
-Paint *BKE_paint_get_active(Scene *sce)
+Paint *BKE_paint_get_active(Scene *sce, ViewLayer *view_layer)
 {
-       if (sce) {
+       if (sce && view_layer) {
                ToolSettings *ts = sce->toolsettings;
                
-               if (sce->basact && sce->basact->object) {
-                       switch (sce->basact->object->mode) {
+               if (view_layer->basact && view_layer->basact->object) {
+                       switch (view_layer->basact->object->mode) {
                                case OB_MODE_SCULPT:
                                        return &ts->sculpt->paint;
                                case OB_MODE_VERTEX_PAINT:
@@ -175,6 +178,8 @@ Paint *BKE_paint_get_active(Scene *sce)
                                        if (ts->use_uv_sculpt)
                                                return &ts->uvsculpt->paint;
                                        return &ts->imapaint.paint;
+                               default:
+                                       break;
                        }
                }
 
@@ -188,14 +193,15 @@ Paint *BKE_paint_get_active(Scene *sce)
 Paint *BKE_paint_get_active_from_context(const bContext *C)
 {
        Scene *sce = CTX_data_scene(C);
+       ViewLayer *view_layer = CTX_data_view_layer(C);
        SpaceImage *sima;
 
-       if (sce) {
+       if (sce && view_layer) {
                ToolSettings *ts = sce->toolsettings;
                Object *obact = NULL;
 
-               if (sce->basact && sce->basact->object)
-                       obact = sce->basact->object;
+               if (view_layer->basact && view_layer->basact->object)
+                       obact = view_layer->basact->object;
 
                if ((sima = CTX_wm_space_image(C)) != NULL) {
                        if (obact && obact->mode == OB_MODE_EDIT) {
@@ -209,7 +215,7 @@ Paint *BKE_paint_get_active_from_context(const bContext *C)
                        }
                }
                else {
-                       return BKE_paint_get_active(sce);
+                       return BKE_paint_get_active(sce, view_layer);
                }
        }
 
@@ -219,14 +225,15 @@ Paint *BKE_paint_get_active_from_context(const bContext *C)
 ePaintMode BKE_paintmode_get_active_from_context(const bContext *C)
 {
        Scene *sce = CTX_data_scene(C);
+       ViewLayer *view_layer = CTX_data_view_layer(C);
        SpaceImage *sima;
 
-       if (sce) {
+       if (sce && view_layer) {
                ToolSettings *ts = sce->toolsettings;
                Object *obact = NULL;
 
-               if (sce->basact && sce->basact->object)
-                       obact = sce->basact->object;
+               if (view_layer->basact && view_layer->basact->object)
+                       obact = view_layer->basact->object;
 
                if ((sima = CTX_wm_space_image(C)) != NULL) {
                        if (obact && obact->mode == OB_MODE_EDIT) {
@@ -332,8 +339,8 @@ void BKE_paint_palette_set(Paint *p, Palette *palette)
 {
        if (p) {
                id_us_min((ID *)p->palette);
-               id_us_plus((ID *)palette);
                p->palette = palette;
+               id_us_plus((ID *)p->palette);
        }
 }
 
@@ -341,8 +348,8 @@ void BKE_paint_curve_set(Brush *br, PaintCurve *pc)
 {
        if (br) {
                id_us_min((ID *)br->paint_curve);
-               id_us_plus((ID *)pc);
                br->paint_curve = pc;
+               id_us_plus((ID *)br->paint_curve);
        }
 }
 
@@ -375,9 +382,7 @@ void BKE_palette_clear(Palette *palette)
 
 Palette *BKE_palette_add(Main *bmain, const char *name)
 {
-       Palette *palette;
-
-       palette = BKE_libblock_alloc(bmain, ID_PAL, name, 0);
+       Palette *palette = BKE_id_new(bmain, ID_PAL, name);
 
        /* enable fake user by default */
        id_fake_user_set(&palette->id);
@@ -418,7 +423,7 @@ void BKE_palette_free(Palette *palette)
 
 PaletteColor *BKE_palette_color_add(Palette *palette)
 {
-       PaletteColor *color = MEM_callocN(sizeof(*color), "Pallete Color");
+       PaletteColor *color = MEM_callocN(sizeof(*color), "Palette Color");
        BLI_addtail(&palette->colors, color);
        return color;
 }
@@ -714,7 +719,7 @@ void BKE_sculptsession_bm_to_me(Object *ob, bool reorder)
                sculptsession_bm_to_me_update_data_only(ob, reorder);
 
                /* ensure the objects DerivedMesh mesh doesn't hold onto arrays now realloc'd in the mesh [#34473] */
-               DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
+               DEG_id_tag_update(&ob->id, OB_RECALC_DATA);
        }
 }
 
@@ -724,7 +729,7 @@ void BKE_sculptsession_bm_to_me_for_render(Object *object)
                if (object->sculpt->bm) {
                        /* Ensure no points to old arrays are stored in DM
                         *
-                        * Apparently, we could not use DAG_id_tag_update
+                        * Apparently, we could not use DEG_id_tag_update
                         * here because this will lead to the while object
                         * surface to disappear, so we'll release DM in place.
                         */
@@ -855,9 +860,19 @@ static bool sculpt_modifiers_active(Scene *scene, Sculpt *sd, Object *ob)
 /**
  * \param need_mask So the DerivedMesh thats returned has mask data
  */
-void BKE_sculpt_update_mesh_elements(Scene *scene, Sculpt *sd, Object *ob,
-                                     bool need_pmap, bool need_mask)
+void BKE_sculpt_update_mesh_elements(
+        Depsgraph *depsgraph, Scene *scene, Sculpt *sd, Object *ob,
+        bool need_pmap, bool need_mask)
 {
+       if (depsgraph == NULL) {
+               /* Happens on file load.
+                *
+                * We do nothing in this case, it will be taken care about on depsgraph
+                * evaluation.
+                */
+               return;
+       }
+
        DerivedMesh *dm;
        SculptSession *ss = ob->sculpt;
        Mesh *me = ob->data;
@@ -895,7 +910,7 @@ void BKE_sculpt_update_mesh_elements(Scene *scene, Sculpt *sd, Object *ob,
 
        ss->kb = (mmd == NULL) ? BKE_keyblock_from_object(ob) : NULL;
 
-       dm = mesh_get_derived_final(scene, ob, CD_MASK_BAREMESH);
+       dm = mesh_get_derived_final(depsgraph, scene, ob, CD_MASK_BAREMESH);
 
        /* VWPaint require mesh info for loop lookup, so require sculpt mode here */
        if (mmd && ob->mode & OB_MODE_SCULPT) {
@@ -930,7 +945,7 @@ void BKE_sculpt_update_mesh_elements(Scene *scene, Sculpt *sd, Object *ob,
 
                        ss->orig_cos = (ss->kb) ? BKE_keyblock_convert_to_vertcos(ob, ss->kb) : BKE_mesh_vertexCos_get(me, NULL);
 
-                       BKE_crazyspace_build_sculpt(scene, ob, &ss->deform_imats, &ss->deform_cos);
+                       BKE_crazyspace_build_sculpt(depsgraph, scene, ob, &ss->deform_imats, &ss->deform_cos);
                        BKE_pbvh_apply_vertCos(ss->pbvh, ss->deform_cos);
 
                        for (a = 0; a < me->totvert; ++a) {
@@ -966,6 +981,9 @@ void BKE_sculpt_update_mesh_elements(Scene *scene, Sculpt *sd, Object *ob,
                        }
                }
        }
+
+       /* 2.8x - avoid full mesh update! */
+       BKE_mesh_batch_cache_dirty(me, BKE_MESH_BATCH_DIRTY_SCULPT_COORDS);
 }
 
 int BKE_sculpt_mask_layers_ensure(Object *ob, MultiresModifierData *mmd)