Merge branch 'master' into blender2.8
[blender.git] / source / blender / blenkernel / intern / dynamicpaint.c
index ab5fe1d..0b2c65a 100644 (file)
@@ -43,7 +43,7 @@
 #include "DNA_armature_types.h"
 #include "DNA_constraint_types.h"
 #include "DNA_dynamicpaint_types.h"
-#include "DNA_group_types.h" /*GroupObject*/
+#include "DNA_group_types.h"
 #include "DNA_material_types.h"
 #include "DNA_mesh_types.h"
 #include "DNA_meshdata_types.h"
 #include "BKE_animsys.h"
 #include "BKE_armature.h"
 #include "BKE_bvhutils.h"   /* bvh tree */
+#include "BKE_collection.h"
 #include "BKE_colorband.h"
 #include "BKE_cdderivedmesh.h"
 #include "BKE_constraint.h"
 #include "BKE_customdata.h"
 #include "BKE_deform.h"
-#include "BKE_depsgraph.h"
 #include "BKE_DerivedMesh.h"
 #include "BKE_dynamicpaint.h"
 #include "BKE_effect.h"
@@ -75,6 +75,9 @@
 #include "BKE_pointcache.h"
 #include "BKE_scene.h"
 
+#include "DEG_depsgraph.h"
+#include "DEG_depsgraph_query.h"
+
 /* for image output    */
 #include "IMB_imbuf_types.h"
 #include "IMB_imbuf.h"
@@ -487,36 +490,22 @@ static void scene_setSubframe(Scene *scene, float subframe)
        scene->r.subframe = subframe;
 }
 
-static int surface_getBrushFlags(DynamicPaintSurface *surface, const Scene *scene)
+static int surface_getBrushFlags(DynamicPaintSurface *surface, const ViewLayer *view_layer)
 {
-       Base *base = NULL;
-       GroupObject *go = NULL;
+       Base *base = BKE_collection_or_layer_objects(NULL, NULL, view_layer, surface->brush_group);
        Object *brushObj = NULL;
        ModifierData *md = NULL;
 
        int flags = 0;
 
-       if (surface->brush_group)
-               go = surface->brush_group->gobject.first;
-       else
-               base = scene->base.first;
-
-       while (base || go) {
+       while (base) {
                brushObj = NULL;
 
                /* select object */
-               if (surface->brush_group) {
-                       if (go->ob)
-                               brushObj = go->ob;
-               }
-               else {
-                       brushObj = base->object;
-               }
+               brushObj = base->object;
 
-               if (surface->brush_group)
-                       go = go->next;
-               else
-                       base = base->next;
+               /* next item */
+               base = base->next;
 
                if (!brushObj) {
                        continue;
@@ -538,11 +527,6 @@ static int surface_getBrushFlags(DynamicPaintSurface *surface, const Scene *scen
        return flags;
 }
 
-static int brush_usesMaterial(const DynamicPaintBrushSettings *brush, const Scene *scene)
-{
-       return ((brush->flags & MOD_DPAINT_USE_MATERIAL) && (!BKE_scene_use_new_shading_nodes(scene)));
-}
-
 /* check whether two bounds intersect */
 static bool boundsIntersect(Bounds3D *b1, Bounds3D *b2)
 {
@@ -1103,7 +1087,6 @@ bool dynamicPaint_createType(struct DynamicPaintModifierData *pmd, int type, str
                        brush->flags = MOD_DPAINT_ABS_ALPHA | MOD_DPAINT_RAMP_ALPHA;
                        brush->collision = MOD_DPAINT_COL_VOLUME;
 
-                       brush->mat = NULL;
                        brush->r = 0.15f;
                        brush->g = 0.4f;
                        brush->b = 0.8f;
@@ -1243,7 +1226,6 @@ void dynamicPaint_Modifier_copy(const struct DynamicPaintModifierData *pmd, stru
                t_brush->flags = brush->flags;
                t_brush->collision = brush->collision;
 
-               t_brush->mat = brush->mat;
                t_brush->r = brush->r;
                t_brush->g = brush->g;
                t_brush->b = brush->b;
@@ -2073,7 +2055,8 @@ static void canvas_copyDerivedMesh(DynamicPaintCanvasSettings *canvas, DerivedMe
  *     Updates derived mesh copy and processes dynamic paint step / caches.
  */
 static void dynamicPaint_frameUpdate(
-        Main *bmain, EvaluationContext *eval_ctx, DynamicPaintModifierData *pmd, Scene *scene, Object *ob, DerivedMesh *dm)
+        DynamicPaintModifierData *pmd, struct Depsgraph *depsgraph, Scene *scene,
+        Object *ob, DerivedMesh *dm)
 {
        if (pmd->canvas) {
                DynamicPaintCanvasSettings *canvas = pmd->canvas;
@@ -2136,7 +2119,7 @@ static void dynamicPaint_frameUpdate(
                                else if (can_simulate) {
                                        /* calculate surface frame */
                                        canvas->flags |= MOD_DPAINT_BAKING;
-                                       dynamicPaint_calculateFrame(bmain, eval_ctx, surface, scene, ob, current_frame);
+                                       dynamicPaint_calculateFrame(surface, depsgraph, scene, ob, current_frame);
                                        canvas->flags &= ~MOD_DPAINT_BAKING;
 
                                        /* restore canvas derivedmesh if required */
@@ -2155,14 +2138,15 @@ static void dynamicPaint_frameUpdate(
 }
 
 /* Modifier call. Processes dynamic paint modifier step. */
-DerivedMesh *dynamicPaint_Modifier_do(Main *bmain,
-        EvaluationContext *eval_ctx, DynamicPaintModifierData *pmd, Scene *scene, Object *ob, DerivedMesh *dm)
+DerivedMesh *dynamicPaint_Modifier_do(
+        DynamicPaintModifierData *pmd, struct Depsgraph *depsgraph, Scene *scene,
+        Object *ob, DerivedMesh *dm)
 {
        if (pmd->canvas) {
                DerivedMesh *ret;
 
                /* Update canvas data for a new frame */
-               dynamicPaint_frameUpdate(bmain, eval_ctx, pmd, scene, ob, dm);
+               dynamicPaint_frameUpdate(pmd, depsgraph, scene, ob, dm);
 
                /* Return output mesh */
                ret = dynamicPaint_Modifier_apply(pmd, ob, dm);
@@ -2171,7 +2155,7 @@ DerivedMesh *dynamicPaint_Modifier_do(Main *bmain,
        }
        else {
                /* Update canvas data for a new frame */
-               dynamicPaint_frameUpdate(bmain, eval_ctx, pmd, scene, ob, dm);
+               dynamicPaint_frameUpdate(pmd, depsgraph, scene, ob, dm);
 
                /* Return output mesh */
                return dynamicPaint_Modifier_apply(pmd, ob, dm);
@@ -3341,91 +3325,6 @@ void dynamicPaint_outputSurfaceImage(DynamicPaintSurface *surface, char *filenam
 }
 
 
-/***************************** Material / Texture Sampling ******************************/
-
-/* stores a copy of required materials to allow doing adjustments
- *  without interfering the render/preview */
-typedef struct BrushMaterials {
-       Material *mat;
-       Material **ob_mats;
-       int tot;
-} BrushMaterials;
-
-/* Initialize materials for brush object:
- *  Calculates inverse matrices for linked objects, updates
- *  volume caches etc. */
-static void dynamicPaint_updateBrushMaterials(Object *brushOb, Material *ui_mat, Scene *scene, BrushMaterials *bMats)
-{
-       /* Calculate inverse transformation matrix
-        *  for this object */
-       invert_m4_m4(brushOb->imat, brushOb->obmat);
-       copy_m4_m4(brushOb->imat_ren, brushOb->imat);
-
-       /* Now process every material linked to this brush object */
-       if ((ui_mat == NULL) && brushOb->mat && brushOb->totcol) {
-               int i, tot = (*give_totcolp(brushOb));
-
-               /* allocate material pointer array */
-               if (tot) {
-                       bMats->ob_mats = MEM_callocN(sizeof(Material *) * (tot), "BrushMaterials");
-                       for (i = 0; i < tot; i++) {
-                               bMats->ob_mats[i] = RE_sample_material_init(give_current_material(brushOb, (i + 1)), scene);
-                       }
-               }
-               bMats->tot = tot;
-       }
-       else {
-               bMats->mat = RE_sample_material_init(ui_mat, scene);
-       }
-}
-
-/* free all data allocated by dynamicPaint_updateBrushMaterials() */
-static void dynamicPaint_freeBrushMaterials(BrushMaterials *bMats)
-{
-       /* Now process every material linked to this brush object */
-       if (bMats->ob_mats) {
-               int i;
-               for (i = 0; i < bMats->tot; i++) {
-                       RE_sample_material_free(bMats->ob_mats[i]);
-               }
-               MEM_freeN(bMats->ob_mats);
-       }
-       else if (bMats->mat) {
-               RE_sample_material_free(bMats->mat);
-       }
-}
-
-/*
- *     Get material diffuse color and alpha (including linked textures) in given coordinates
- */
-static void dynamicPaint_doMaterialTex(
-        const BrushMaterials *bMats, float color[3], float *alpha, Object *brushOb,
-        const float volume_co[3], const float surface_co[3],
-        int triIndex, DerivedMesh *orcoDm)
-{
-       Material *mat = bMats->mat;
-
-       const MLoopTri *mlooptri = orcoDm->getLoopTriArray(orcoDm);
-       const MPoly *mpoly = orcoDm->getPolyArray(orcoDm);
-
-       /* If no material defined, use the one assigned to the mesh face */
-       if (mat == NULL) {
-               if (bMats->ob_mats) {
-                       int mat_nr = mpoly[mlooptri[triIndex].poly].mat_nr;
-                       if (mat_nr >= (*give_totcolp(brushOb)))
-                               return;
-                       mat = bMats->ob_mats[mat_nr];
-                       if (mat == NULL)
-                               return;    /* No material assigned */
-               }
-               else {
-                       return;
-               }
-       }
-       RE_sample_material_color(mat, color, alpha, volume_co, surface_co, triIndex, orcoDm, brushOb);
-}
-
-
 /***************************** Ray / Nearest Point Utils ******************************/
 
 
@@ -3746,7 +3645,7 @@ static void dynamic_paint_brush_velocity_compute_cb(
 }
 
 static void dynamicPaint_brushMeshCalculateVelocity(
-        Main *bmain, EvaluationContext *eval_ctx, Scene *scene,
+        Depsgraph *depsgraph, Scene *scene,
         Object *ob, DynamicPaintBrushSettings *brush, Vec3f **brushVel, float timescale)
 {
        float prev_obmat[4][4];
@@ -3769,7 +3668,7 @@ static void dynamicPaint_brushMeshCalculateVelocity(
        scene->r.subframe = prev_sfra;
 
        BKE_object_modifier_update_subframe(
-                   bmain, eval_ctx, scene, ob, true, SUBFRAME_RECURSION, BKE_scene_frame_get(scene), eModifierType_DynamicPaint);
+                   depsgraph, scene, ob, true, SUBFRAME_RECURSION, BKE_scene_frame_get(scene), eModifierType_DynamicPaint);
        dm_p = CDDM_copy(brush->dm);
        numOfVerts_p = dm_p->getNumVerts(dm_p);
        mvert_p = dm_p->getVertArray(dm_p);
@@ -3780,7 +3679,7 @@ static void dynamicPaint_brushMeshCalculateVelocity(
        scene->r.subframe = cur_sfra;
 
        BKE_object_modifier_update_subframe(
-                   bmain, eval_ctx, scene, ob, true, SUBFRAME_RECURSION, BKE_scene_frame_get(scene), eModifierType_DynamicPaint);
+                   depsgraph, scene, ob, true, SUBFRAME_RECURSION, BKE_scene_frame_get(scene), eModifierType_DynamicPaint);
        dm_c = brush->dm;
        numOfVerts_c = dm_c->getNumVerts(dm_c);
        mvert_c = dm_p->getVertArray(dm_c);
@@ -3812,7 +3711,7 @@ static void dynamicPaint_brushMeshCalculateVelocity(
 
 /* calculate velocity for object center point */
 static void dynamicPaint_brushObjectCalculateVelocity(
-        Main *bmain, EvaluationContext *eval_ctx, Scene *scene, Object *ob, Vec3f *brushVel, float timescale)
+        Depsgraph *depsgraph, Scene *scene, Object *ob, Vec3f *brushVel, float timescale)
 {
        float prev_obmat[4][4];
        float cur_loc[3] = {0.0f}, prev_loc[3] = {0.0f};
@@ -3831,14 +3730,14 @@ static void dynamicPaint_brushObjectCalculateVelocity(
        scene->r.cfra = prev_fra;
        scene->r.subframe = prev_sfra;
        BKE_object_modifier_update_subframe(
-                   bmain, eval_ctx, scene, ob, false, SUBFRAME_RECURSION, BKE_scene_frame_get(scene), eModifierType_DynamicPaint);
+                   depsgraph, scene, ob, false, SUBFRAME_RECURSION, BKE_scene_frame_get(scene), eModifierType_DynamicPaint);
        copy_m4_m4(prev_obmat, ob->obmat);
 
        /* current frame dm */
        scene->r.cfra = cur_fra;
        scene->r.subframe = cur_sfra;
        BKE_object_modifier_update_subframe(
-                   bmain, eval_ctx, scene, ob, false, SUBFRAME_RECURSION, BKE_scene_frame_get(scene), eModifierType_DynamicPaint);
+                   depsgraph, scene, ob, false, SUBFRAME_RECURSION, BKE_scene_frame_get(scene), eModifierType_DynamicPaint);
 
        /* calculate speed */
        mul_m4_v3(prev_obmat, prev_loc);
@@ -3852,7 +3751,6 @@ typedef struct DynamicPaintPaintData {
        const DynamicPaintSurface *surface;
        const DynamicPaintBrushSettings *brush;
        Object *brushOb;
-       const BrushMaterials *bMats;
        const Scene *scene;
        const float timescale;
        const int c_index;
@@ -3889,14 +3787,10 @@ static void dynamic_paint_paint_mesh_cell_point_cb_ex(
        VolumeGrid *grid = bData->grid;
 
        const DynamicPaintBrushSettings *brush = data->brush;
-       Object *brushOb = data->brushOb;
-       const BrushMaterials *bMats = data->bMats;
 
-       const Scene *scene = data->scene;
        const float timescale = data->timescale;
        const int c_index = data->c_index;
 
-       DerivedMesh *dm = data->dm;
        const MVert *mvert = data->mvert;
        const MLoop *mloop = data->mloop;
        const MLoopTri *mlooptri = data->mlooptri;
@@ -4156,13 +4050,6 @@ static void dynamic_paint_paint_mesh_cell_point_cb_ex(
                        sampleColor[1] = brush->g;
                        sampleColor[2] = brush->b;
 
-                       /* Get material+textures color on hit point if required */
-                       if (brush_usesMaterial(brush, scene)) {
-                               dynamicPaint_doMaterialTex(bMats, sampleColor, &alpha_factor, brushOb,
-                                                          bData->realCoord[bData->s_pos[index] + ss].v,
-                                                          hitCoord, hitTri, dm);
-                       }
-
                        /* Sample proximity colorband if required       */
                        if ((hit_found == HIT_PROXIMITY) &&
                            (brush->proximity_falloff == MOD_DPAINT_PRFALL_RAMP))
@@ -4210,12 +4097,9 @@ static void dynamic_paint_paint_mesh_cell_point_cb_ex(
        }
 }
 
-static int dynamicPaint_paintMesh(Main *bmain,
-                                  EvaluationContext *eval_ctx,
-                                  DynamicPaintSurface *surface,
+static int dynamicPaint_paintMesh(Depsgraph *depsgraph, DynamicPaintSurface *surface,
                                   DynamicPaintBrushSettings *brush,
                                   Object *brushOb,
-                                  BrushMaterials *bMats,
                                   Scene *scene,
                                   float timescale)
 {
@@ -4228,7 +4112,7 @@ static int dynamicPaint_paintMesh(Main *bmain,
        const MLoop *mloop = NULL;
 
        if (brush->flags & MOD_DPAINT_USES_VELOCITY)
-               dynamicPaint_brushMeshCalculateVelocity(bmain, eval_ctx, scene, brushOb, brush, &brushVelocity, timescale);
+               dynamicPaint_brushMeshCalculateVelocity(depsgraph, scene, brushOb, brush, &brushVelocity, timescale);
 
        if (!brush->dm)
                return 0;
@@ -4293,7 +4177,7 @@ static int dynamicPaint_paintMesh(Main *bmain,
                                        /* loop through cell points and process brush */
                                        DynamicPaintPaintData data = {
                                            .surface = surface,
-                                           .brush = brush, .brushOb = brushOb, .bMats = bMats,
+                                           .brush = brush, .brushOb = brushOb,
                                            .scene = scene, .timescale = timescale, .c_index = c_index,
                                            .dm = dm, .mvert = mvert, .mloop = mloop, .mlooptri = mlooptri,
                                            .brush_radius = brush_radius, .avg_brushNor = avg_brushNor, .brushVelocity = brushVelocity,
@@ -4618,13 +4502,9 @@ static void dynamic_paint_paint_single_point_cb_ex(
        const PaintBakeData *bData = sData->bData;
 
        const DynamicPaintBrushSettings *brush = data->brush;
-       Object *brushOb = data->brushOb;
-       const BrushMaterials *bMats = data->bMats;
 
-       const Scene *scene = data->scene;
        const float timescale = data->timescale;
 
-       const MVert *mvert = data->mvert;
        const float brush_radius = data->brush_radius;
        const Vec3f *brushVelocity = data->brushVelocity;
 
@@ -4653,17 +4533,6 @@ static void dynamic_paint_paint_single_point_cb_ex(
                float depth = 0.0f;
                float velocity_val = 0.0f;
 
-               /* material */
-               if (brush_usesMaterial(brush, scene)) {
-                       float alpha_factor = 1.0f;
-                       float hit_coord[3];
-                       /* use dummy coord of first vertex */
-                       mul_v3_m4v3(hit_coord, brushOb->obmat, mvert[0].co);
-
-                       dynamicPaint_doMaterialTex(bMats, paintColor, &alpha_factor, brushOb,
-                                                  bData->realCoord[bData->s_pos[index]].v, hit_coord, 0, brush->dm);
-               }
-
                /* color ramp */
                if (brush->proximity_falloff == MOD_DPAINT_PRFALL_RAMP &&
                    BKE_colorband_evaluate(brush->paint_ramp, (1.0f - strength), colorband))
@@ -4701,11 +4570,9 @@ static void dynamic_paint_paint_single_point_cb_ex(
                                paintColor[2] = colorband[2];
                        }
                        else {
-                               if (!brush_usesMaterial(brush, scene)) {
-                                       paintColor[0] = brush->r;
-                                       paintColor[1] = brush->g;
-                                       paintColor[2] = brush->b;
-                               }
+                               paintColor[0] = brush->r;
+                               paintColor[1] = brush->g;
+                               paintColor[2] = brush->b;
                        }
                }
                else if (ELEM(surface->type, MOD_DPAINT_SURFACE_T_DISPLACE, MOD_DPAINT_SURFACE_T_WAVE)) {
@@ -4718,15 +4585,15 @@ static void dynamic_paint_paint_single_point_cb_ex(
 }
 
 static int dynamicPaint_paintSinglePoint(
-        Main *bmain, EvaluationContext *eval_ctx, DynamicPaintSurface *surface, float *pointCoord, DynamicPaintBrushSettings *brush,
-        Object *brushOb, BrushMaterials *bMats, Scene *scene, float timescale)
+        Depsgraph *depsgraph, DynamicPaintSurface *surface, float *pointCoord, DynamicPaintBrushSettings *brush,
+        Object *brushOb, Scene *scene, float timescale)
 {
        PaintSurfaceData *sData = surface->data;
        float brush_radius = brush->paint_distance * surface->radius_scale;
        Vec3f brushVel;
 
        if (brush->flags & MOD_DPAINT_USES_VELOCITY)
-               dynamicPaint_brushObjectCalculateVelocity(bmain, eval_ctx, scene, brushOb, &brushVel, timescale);
+               dynamicPaint_brushObjectCalculateVelocity(depsgraph, scene, brushOb, &brushVel, timescale);
 
        const MVert *mvert = brush->dm->getVertArray(brush->dm);
 
@@ -4735,7 +4602,7 @@ static int dynamicPaint_paintSinglePoint(
         */
        DynamicPaintPaintData data = {
            .surface = surface,
-           .brush = brush, .brushOb = brushOb, .bMats = bMats,
+           .brush = brush, .brushOb = brushOb,
            .scene = scene, .timescale = timescale,
            .mvert = mvert,
            .brush_radius = brush_radius, .brushVelocity = &brushVel,
@@ -5048,7 +4915,7 @@ static void dynamic_paint_prepare_effect_cb(
 }
 
 static int dynamicPaint_prepareEffectStep(
-        DynamicPaintSurface *surface, Scene *scene, Object *ob, float **force, float timescale)
+        struct Depsgraph *depsgraph, DynamicPaintSurface *surface, Scene *scene, Object *ob, float **force, float timescale)
 {
        double average_force = 0.0f;
        float shrink_speed = 0.0f, spread_speed = 0.0f;
@@ -5059,7 +4926,7 @@ static int dynamicPaint_prepareEffectStep(
 
        /* Init force data if required */
        if (surface->effect & MOD_DPAINT_EFFECT_DO_DRIP) {
-               ListBase *effectors = pdInitEffectors(scene, ob, NULL, surface->effector_weights, true);
+               ListBase *effectors = pdInitEffectors(depsgraph, scene, ob, NULL, surface->effector_weights, true);
 
                /* allocate memory for force data (dir vector + strength) */
                *force = MEM_mallocN(sData->total_points * 4 * sizeof(float), "PaintEffectForces");
@@ -5891,7 +5758,7 @@ static void dynamic_paint_generate_bake_data_cb(
        }
 }
 
-static int dynamicPaint_generateBakeData(DynamicPaintSurface *surface, const Scene *scene, Object *ob)
+static int dynamicPaint_generateBakeData(DynamicPaintSurface *surface, const ViewLayer *view_layer, Object *ob)
 {
        PaintSurfaceData *sData = surface->data;
        PaintBakeData *bData = sData->bData;
@@ -5899,7 +5766,7 @@ static int dynamicPaint_generateBakeData(DynamicPaintSurface *surface, const Sce
        int index;
        bool new_bdata = false;
        const bool do_velocity_data = ((surface->effect & MOD_DPAINT_EFFECT_DO_DRIP) ||
-                                      (surface_getBrushFlags(surface, scene) & BRUSH_USES_VELOCITY));
+                                      (surface_getBrushFlags(surface, view_layer) & BRUSH_USES_VELOCITY));
        const bool do_accel_data = (surface->effect & MOD_DPAINT_EFFECT_DO_DRIP) != 0;
 
        int canvasNumOfVerts = dm->getNumVerts(dm);
@@ -6017,7 +5884,7 @@ static int dynamicPaint_generateBakeData(DynamicPaintSurface *surface, const Sce
  * Do Dynamic Paint step. Paints scene brush objects of current state/frame to the surface.
  */
 static int dynamicPaint_doStep(
-        Main *bmain, EvaluationContext *eval_ctx, Scene *scene,
+        Depsgraph *depsgraph, Scene *scene,
         Object *ob, DynamicPaintSurface *surface, float timescale, float subframe)
 {
        PaintSurfaceData *sData = surface->data;
@@ -6043,36 +5910,22 @@ static int dynamicPaint_doStep(
         * Loop through surface's target paint objects and do painting
         */
        {
-               Base *base = NULL;
-               GroupObject *go = NULL;
                Object *brushObj = NULL;
                ModifierData *md = NULL;
+               ViewLayer *view_layer = DEG_get_evaluated_view_layer(depsgraph);
+               Base *base = BKE_collection_or_layer_objects(NULL, NULL, view_layer, surface->brush_group);
 
                /* backup current scene frame */
                int scene_frame = scene->r.cfra;
                float scene_subframe = scene->r.subframe;
 
-               /* either from group or from all objects */
-               if (surface->brush_group)
-                       go = surface->brush_group->gobject.first;
-               else
-                       base = scene->base.first;
-
-               while (base || go) {
+               while (base) {
                        brushObj = NULL;
                        /* select object */
-                       if (surface->brush_group) {
-                               if (go->ob)
-                                       brushObj = go->ob;
-                       }
-                       else
-                               brushObj = base->object;
+                       brushObj = base->object;
 
                        /* next item */
-                       if (surface->brush_group)
-                               go = go->next;
-                       else
-                               base = base->next;
+                       base = base->next;
 
                        if (!brushObj) {
                                /* skip item */
@@ -6086,7 +5939,6 @@ static int dynamicPaint_doStep(
                                /* make sure we're dealing with a brush */
                                if (pmd2->brush) {
                                        DynamicPaintBrushSettings *brush = pmd2->brush;
-                                       BrushMaterials bMats = {NULL};
 
                                        /* calculate brush speed vectors if required */
                                        if (surface->type == MOD_DPAINT_SURFACE_T_PAINT && brush->flags & MOD_DPAINT_DO_SMUDGE) {
@@ -6101,44 +5953,35 @@ static int dynamicPaint_doStep(
                                        /* update object data on this subframe */
                                        if (subframe) {
                                                scene_setSubframe(scene, subframe);
-                                               BKE_object_modifier_update_subframe(bmain, eval_ctx, scene, brushObj, true, SUBFRAME_RECURSION,
+                                               BKE_object_modifier_update_subframe(depsgraph, scene, brushObj, true, SUBFRAME_RECURSION,
                                                                                    BKE_scene_frame_get(scene), eModifierType_DynamicPaint);
                                        }
-                                       /* Prepare materials if required        */
-                                       if (brush_usesMaterial(brush, scene))
-                                               dynamicPaint_updateBrushMaterials(brushObj, brush->mat, scene, &bMats);
+
 
                                        /* Apply brush on the surface depending on it's collision type */
-                                       /* Particle brush: */
-                                       if (brush->collision == MOD_DPAINT_COL_PSYS) {
-                                               if (brush->psys && brush->psys->part &&
-                                                   ELEM(brush->psys->part->type, PART_EMITTER, PART_FLUID) &&
-                                                   psys_check_enabled(brushObj, brush->psys, G.is_rendering))
-                                               {
-                                                       /* Paint a particle system */
-                                                       BKE_animsys_evaluate_animdata(scene, &brush->psys->part->id, brush->psys->part->adt,
-                                                                                     BKE_scene_frame_get(scene), ADT_RECALC_ANIM);
-                                                       dynamicPaint_paintParticles(surface, brush->psys, brush, timescale);
-                                               }
+                                       if (brush->psys && brush->psys->part &&
+                                           ELEM(brush->psys->part->type, PART_EMITTER, PART_FLUID) &&
+                                           psys_check_enabled(brushObj, brush->psys, G.is_rendering))
+                                       {
+                                               /* Paint a particle system */
+                                               BKE_animsys_evaluate_animdata(depsgraph, scene, &brush->psys->part->id, brush->psys->part->adt,
+                                                                             BKE_scene_frame_get(scene), ADT_RECALC_ANIM);
+                                               dynamicPaint_paintParticles(surface, brush->psys, brush, timescale);
                                        }
                                        /* Object center distance: */
-                                       else if (brush->collision == MOD_DPAINT_COL_POINT && brushObj != ob) {
-                                               dynamicPaint_paintSinglePoint(
-                                                           bmain, eval_ctx, surface, brushObj->loc, brush, brushObj, &bMats, scene, timescale);
+                                       if (brush->collision == MOD_DPAINT_COL_POINT && brushObj != ob) {
+                                               dynamicPaint_paintSinglePoint(depsgraph, surface, brushObj->loc, brush, brushObj, scene, timescale);
                                        }
                                        /* Mesh volume/proximity: */
                                        else if (brushObj != ob) {
-                                               dynamicPaint_paintMesh(bmain, eval_ctx, surface, brush, brushObj, &bMats, scene, timescale);
+                                               dynamicPaint_paintMesh(depsgraph, surface, brush, brushObj, scene, timescale);
                                        }
 
-                                       /* free temp material data */
-                                       if (brush_usesMaterial(brush, scene))
-                                               dynamicPaint_freeBrushMaterials(&bMats);
                                        /* reset object to it's original state */
                                        if (subframe) {
                                                scene->r.cfra = scene_frame;
                                                scene->r.subframe = scene_subframe;
-                                               BKE_object_modifier_update_subframe(bmain, eval_ctx, scene, brushObj, true, SUBFRAME_RECURSION,
+                                               BKE_object_modifier_update_subframe(depsgraph, scene, brushObj, true, SUBFRAME_RECURSION,
                                                                                    BKE_scene_frame_get(scene), eModifierType_DynamicPaint);
                                        }
 
@@ -6173,7 +6016,7 @@ static int dynamicPaint_doStep(
                                return setError(canvas, N_("Not enough free memory"));
 
                        /* Prepare effects and get number of required steps */
-                       steps = dynamicPaint_prepareEffectStep(surface, scene, ob, &force, timescale);
+                       steps = dynamicPaint_prepareEffectStep(depsgraph, surface, scene, ob, &force, timescale);
                        for (s = 0; s < steps; s++) {
                                dynamicPaint_doEffectStep(surface, force, prevPoint, timescale, (float)steps);
                        }
@@ -6198,7 +6041,8 @@ static int dynamicPaint_doStep(
  * Calculate a single frame and included subframes for surface
  */
 int dynamicPaint_calculateFrame(
-        Main *bmain, EvaluationContext *eval_ctx, DynamicPaintSurface *surface, Scene *scene, Object *cObject, int frame)
+        DynamicPaintSurface *surface, struct Depsgraph *depsgraph,
+        Scene *scene, Object *cObject, int frame)
 {
        float timescale = 1.0f;
 
@@ -6207,7 +6051,8 @@ int dynamicPaint_calculateFrame(
                dynamicPaint_applySurfaceDisplace(surface, surface->canvas->dm);
 
        /* update bake data */
-       dynamicPaint_generateBakeData(surface, scene, cObject);
+       ViewLayer *view_layer = DEG_get_evaluated_view_layer(depsgraph);
+       dynamicPaint_generateBakeData(surface, view_layer, cObject);
 
        /* don't do substeps for first frame */
        if (surface->substeps && (frame != surface->start_frame)) {
@@ -6216,10 +6061,10 @@ int dynamicPaint_calculateFrame(
 
                for (st = 1; st <= surface->substeps; st++) {
                        float subframe = ((float) st) / (surface->substeps + 1);
-                       if (!dynamicPaint_doStep(bmain, eval_ctx, scene, cObject, surface, timescale, subframe))
+                       if (!dynamicPaint_doStep(depsgraph, scene, cObject, surface, timescale, subframe))
                                return 0;
                }
        }
 
-       return dynamicPaint_doStep(bmain, eval_ctx, scene, cObject, surface, timescale, 0.0f);
+       return dynamicPaint_doStep(depsgraph, scene, cObject, surface, timescale, 0.0f);
 }