Merge branch 'master' into blender2.8
authorSergey Sharybin <sergey.vfx@gmail.com>
Fri, 21 Jul 2017 09:16:37 +0000 (11:16 +0200)
committerSergey Sharybin <sergey.vfx@gmail.com>
Fri, 21 Jul 2017 09:18:02 +0000 (11:18 +0200)
16 files changed:
1  2 
intern/cycles/blender/blender_mesh.cpp
intern/cycles/blender/blender_session.cpp
intern/cycles/blender/blender_session.h
source/blender/blenkernel/BKE_particle.h
source/blender/blenkernel/intern/particle_system.c
source/blender/depsgraph/intern/builder/deg_builder_nodes.cc
source/blender/depsgraph/intern/builder/deg_builder_relations.cc
source/blender/depsgraph/intern/depsgraph_type_defines.cc
source/blender/depsgraph/intern/depsgraph_types.h
source/blender/editors/include/UI_interface.h
source/blender/editors/interface/interface_utils.c
source/blender/editors/object/object_add.c
source/blender/editors/sculpt_paint/paint_image_2d.c
source/blender/editors/sculpt_paint/paint_image_proj.c
source/blender/editors/sculpt_paint/paint_utils.c
source/blender/editors/space_image/image_ops.c

index 75933c955eed1653c28e8d87638ad8bd66706d6d,b4cca5f00f4eb904c93cb58de74f7522e75d289b..f86b75f6643080ef4c2e3c734a64fe9fb719c2b5
@@@ -50,8 -50,7 +50,7 @@@ enum 
   * Two triangles has vertex indices in the original Blender-side face.
   * If face is already a quad tri_b will not be initialized.
   */
- inline void face_split_tri_indices(const int num_verts,
-                                    const int face_flag,
+ inline void face_split_tri_indices(const int face_flag,
                                     int tri_a[3],
                                     int tri_b[3])
  {
                tri_a[0] = 0;
                tri_a[1] = 1;
                tri_a[2] = 3;
-               if(num_verts == 4) {
-                       tri_b[0] = 2;
-                       tri_b[1] = 3;
-                       tri_b[2] = 1;
-               }
+               tri_b[0] = 2;
+               tri_b[1] = 3;
+               tri_b[2] = 1;
        }
        else /*if(face_flag & FACE_FLAG_DIVIDE_13)*/ {
                tri_a[0] = 0;
                tri_a[1] = 1;
                tri_a[2] = 2;
-               if(num_verts == 4) {
-                       tri_b[0] = 0;
-                       tri_b[1] = 2;
-                       tri_b[2] = 3;
-               }
+               tri_b[0] = 0;
+               tri_b[1] = 2;
+               tri_b[2] = 3;
        }
  }
  
@@@ -250,7 -247,7 +247,7 @@@ static void mikk_compute_tangents(BL::M
  
        for(int i = 0; i < nverts.size(); i++) {
                int tri_a[3], tri_b[3];
-               face_split_tri_indices(nverts[i], face_flags[i], tri_a, tri_b);
+               face_split_tri_indices(face_flags[i], tri_a, tri_b);
  
                tangent[0] = float4_to_float3(userdata.tangent[i*4 + tri_a[0]]);
                tangent[1] = float4_to_float3(userdata.tangent[i*4 + tri_a[1]]);
@@@ -376,7 -373,7 +373,7 @@@ static void attr_create_vertex_color(Sc
  
                        for(l->data.begin(c); c != l->data.end(); ++c, ++i) {
                                int tri_a[3], tri_b[3];
-                               face_split_tri_indices(nverts[i], face_flags[i], tri_a, tri_b);
+                               face_split_tri_indices(face_flags[i], tri_a, tri_b);
  
                                uchar4 colors[4];
                                colors[0] = color_float_to_byte(color_srgb_to_scene_linear_v3(get_float3(c->color1())));
@@@ -417,7 -414,7 +414,7 @@@ static void attr_create_uv_map(Scene *s
                int i = 0;
  
                for(b_mesh.uv_layers.begin(l); l != b_mesh.uv_layers.end(); ++l, ++i) {
 -                      bool active_render = b_mesh.uv_textures[i].active_render();
 +                      bool active_render = b_mesh.uv_layers[i].active_render();
                        AttributeStandard std = (active_render)? ATTR_STD_UV: ATTR_STD_NONE;
                        ustring name = ustring(l->name().c_str());
  
  
                                for(l->data.begin(t); t != l->data.end(); ++t, ++i) {
                                        int tri_a[3], tri_b[3];
-                                       face_split_tri_indices(nverts[i], face_flags[i], tri_a, tri_b);
+                                       face_split_tri_indices(face_flags[i], tri_a, tri_b);
  
                                        float3 uvs[4];
                                        uvs[0] = get_float3(t->uv1());
@@@ -945,7 -942,6 +942,7 @@@ static void sync_mesh_fluid_motion(BL::
  }
  
  Mesh *BlenderSync::sync_mesh(BL::Object& b_ob,
 +                             BL::Object& b_ob_instance,
                               bool object_updated,
                               bool hide_tris)
  {
  
        /* test if we can instance or if the object is modified */
        BL::ID b_ob_data = b_ob.data();
 -      BL::ID key = (BKE_object_is_modified(b_ob))? b_ob: b_ob_data;
 +      BL::ID key = (BKE_object_is_modified(b_ob))? b_ob_instance: b_ob_data;
        BL::Material material_override = render_layer.material_override;
  
        /* find shader indices */
index d8c3750cf20141eeab19ec4c93955fe26ee55e02,12de3da063fa09f02b53387e2d514cce94b4b8d2..bdc254d199514b6753e2a4a59b7eb2da631d62db
@@@ -52,13 -52,11 +52,13 @@@ int BlenderSession::end_resumable_chun
  BlenderSession::BlenderSession(BL::RenderEngine& b_engine,
                                 BL::UserPreferences& b_userpref,
                                 BL::BlendData& b_data,
 +                               BL::Depsgraph& b_depsgraph,
                                 BL::Scene& b_scene)
  : b_engine(b_engine),
    b_userpref(b_userpref),
    b_data(b_data),
    b_render(b_engine.render()),
 +  b_depsgraph(b_depsgraph),
    b_scene(b_scene),
    b_v3d(PointerRNA_NULL),
    b_rv3d(PointerRNA_NULL),
@@@ -78,7 -76,6 +78,7 @@@
  BlenderSession::BlenderSession(BL::RenderEngine& b_engine,
                                 BL::UserPreferences& b_userpref,
                                 BL::BlendData& b_data,
 +                               BL::Depsgraph& b_depsgraph,
                                 BL::Scene& b_scene,
                                 BL::SpaceView3D& b_v3d,
                                 BL::RegionView3D& b_rv3d,
@@@ -87,7 -84,6 +87,7 @@@
    b_userpref(b_userpref),
    b_data(b_data),
    b_render(b_scene.render()),
 +  b_depsgraph(b_depsgraph),
    b_scene(b_scene),
    b_v3d(b_v3d),
    b_rv3d(b_rv3d),
@@@ -133,9 -129,9 +133,9 @@@ void BlenderSession::create_session(
        scene = new Scene(scene_params, session_params.device);
  
        /* setup callbacks for builtin image support */
-       scene->image_manager->builtin_image_info_cb = function_bind(&BlenderSession::builtin_image_info, this, _1, _2, _3, _4, _5, _6, _7);
-       scene->image_manager->builtin_image_pixels_cb = function_bind(&BlenderSession::builtin_image_pixels, this, _1, _2, _3, _4);
-       scene->image_manager->builtin_image_float_pixels_cb = function_bind(&BlenderSession::builtin_image_float_pixels, this, _1, _2, _3, _4);
+       scene->image_manager->builtin_image_info_cb = function_bind(&BlenderSession::builtin_image_info, this, _1, _2, _3, _4, _5, _6, _7, _8);
+       scene->image_manager->builtin_image_pixels_cb = function_bind(&BlenderSession::builtin_image_pixels, this, _1, _2, _3, _4, _5);
+       scene->image_manager->builtin_image_float_pixels_cb = function_bind(&BlenderSession::builtin_image_float_pixels, this, _1, _2, _3, _4, _5);
  
        /* create session */
        session = new Session(session_params);
        session->set_pause(session_pause);
  
        /* create sync */
 -      sync = new BlenderSync(b_engine, b_data, b_scene, scene, !background, session->progress, is_cpu);
 +      sync = new BlenderSync(b_engine, b_data, b_depsgraph, b_scene, scene, !background, session->progress, is_cpu);
        BL::Object b_camera_override(b_engine.camera_override());
        if(b_v3d) {
                if(session_pause == false) {
@@@ -215,7 -211,7 +215,7 @@@ void BlenderSession::reset_session(BL::
        session->stats.mem_peak = session->stats.mem_used;
  
        /* sync object should be re-created */
 -      sync = new BlenderSync(b_engine, b_data, b_scene, scene, !background, session->progress, is_cpu);
 +      sync = new BlenderSync(b_engine, b_data, b_depsgraph, b_scene, scene, !background, session->progress, is_cpu);
  
        /* for final render we will do full data sync per render layer, only
         * do some basic syncing here, no objects or materials for speed */
@@@ -1017,7 -1013,8 +1017,8 @@@ void BlenderSession::builtin_image_info
                                          int &width,
                                          int &height,
                                          int &depth,
-                                         int &channels)
+                                         int &channels,
+                                         bool& free_cache)
  {
        /* empty image */
        is_float = false;
        height = 1;
        depth = 0;
        channels = 0;
+       free_cache = false;
  
        if(!builtin_data)
                return;
                /* image data */
                BL::Image b_image(b_id);
  
+               free_cache = !b_image.has_data();
                is_float = b_image.is_float();
                width = b_image.size()[0];
                height = b_image.size()[1];
  bool BlenderSession::builtin_image_pixels(const string &builtin_name,
                                            void *builtin_data,
                                            unsigned char *pixels,
-                                           const size_t pixels_size)
+                                           const size_t pixels_size,
+                                           const bool free_cache)
  {
        if(!builtin_data) {
                return false;
  
        if(image_pixels && num_pixels * channels == pixels_size) {
                memcpy(pixels, image_pixels, pixels_size * sizeof(unsigned char));
-               MEM_freeN(image_pixels);
        }
        else {
                if(channels == 1) {
                        }
                }
        }
+       if(image_pixels) {
+               MEM_freeN(image_pixels);
+       }
+       /* Free image buffers to save memory during render. */
+       if(free_cache) {
+               b_image.buffers_free();
+       }
        /* Premultiply, byte images are always straight for Blender. */
        unsigned char *cp = pixels;
        for(size_t i = 0; i < num_pixels; i++, cp += channels) {
  bool BlenderSession::builtin_image_float_pixels(const string &builtin_name,
                                                  void *builtin_data,
                                                  float *pixels,
-                                                 const size_t pixels_size)
+                                                 const size_t pixels_size,
+                                                 const bool free_cache)
  {
        if(!builtin_data) {
                return false;
  
                if(image_pixels && num_pixels * channels == pixels_size) {
                        memcpy(pixels, image_pixels, pixels_size * sizeof(float));
-                       MEM_freeN(image_pixels);
                }
                else {
                        if(channels == 1) {
                        }
                }
  
+               if(image_pixels) {
+                       MEM_freeN(image_pixels);
+               }
+               /* Free image buffers to save memory during render. */
+               if(free_cache) {
+                       b_image.buffers_free();
+               }
                return true;
        }
        else if(b_id.is_a(&RNA_Object)) {
index 254e8a049ffa88e661aaea06b2437a9068d5672f,cbd2303d2825b04e3582308261a7e0e78c0736f2..5edf44d087e2ba6f8a26b6f314fbb35896c59e27
@@@ -36,13 -36,11 +36,13 @@@ public
        BlenderSession(BL::RenderEngine& b_engine,
                       BL::UserPreferences& b_userpref,
                       BL::BlendData& b_data,
 +                     BL::Depsgraph& b_depsgraph,
                       BL::Scene& b_scene);
  
        BlenderSession(BL::RenderEngine& b_engine,
                       BL::UserPreferences& b_userpref,
                       BL::BlendData& b_data,
 +                     BL::Depsgraph& b_depsgraph,
                       BL::Scene& b_scene,
                       BL::SpaceView3D& b_v3d,
                       BL::RegionView3D& b_rv3d,
        BL::UserPreferences b_userpref;
        BL::BlendData b_data;
        BL::RenderSettings b_render;
 +      BL::Depsgraph b_depsgraph;
        BL::Scene b_scene;
        BL::SpaceView3D b_v3d;
        BL::RegionView3D b_rv3d;
@@@ -159,15 -156,18 +159,18 @@@ protected
                                int &width,
                                int &height,
                                int &depth,
-                               int &channels);
+                               int &channels,
+                               bool &free_cache);
        bool builtin_image_pixels(const string &builtin_name,
                                  void *builtin_data,
                                  unsigned char *pixels,
-                                 const size_t pixels_size);
+                                 const size_t pixels_size,
+                                 const bool free_cache);
        bool builtin_image_float_pixels(const string &builtin_name,
                                        void *builtin_data,
                                        float *pixels,
-                                       const size_t pixels_size);
+                                       const size_t pixels_size,
+                                       const bool free_cache);
  
        /* Update tile manager to reflect resumable render settings. */
        void update_resumable_tile_manager(int num_samples);
index f318787d954e8c9ef831fee26cf649f134ed0c8c,470e2e468f0219025d52bf27d395c469fe1b8a4e..22a0290f364a6ffe414767e1149207b21f20d9ff
@@@ -473,21 -473,8 +473,20 @@@ typedef struct ParticleRenderData 
  
  struct EvaluationContext;
  
- void BKE_particle_system_eval(struct EvaluationContext *eval_ctx,
-                               struct Scene *scene,
-                               struct Object *ob,
-                               struct ParticleSystem *psys);
 +void BKE_particle_system_settings_eval(struct EvaluationContext *eval_ctx,
 +                                       struct ParticleSystem *psys);
 +void BKE_particle_system_settings_recalc_clear(struct EvaluationContext *UNUSED(eval_ctx),
 +                                               struct ParticleSettings *particle_settings);
 +
+ void BKE_particle_system_eval_init(struct EvaluationContext *eval_ctx,
+                                    struct Scene *scene,
+                                    struct Object *ob);
  
  #endif
 +
 +/* Draw Cache */
 +enum {
 +      BKE_PARTICLE_BATCH_DIRTY_ALL = 0,
 +};
 +void BKE_particle_batch_cache_dirty(struct ParticleSystem *psys, int mode);
 +void BKE_particle_batch_cache_free(struct ParticleSystem *psys);
index 3a7ca2caaa4a1b3bd7c610e1eb887c081a2d36c7,b61f6792c890537a157d137ea144a3559e9883b7..79440b79d5c3c78c9d9ccb3c77f430f6113b1f97
@@@ -78,7 -78,6 +78,7 @@@
  #include "BKE_particle.h"
  #include "BKE_global.h"
  
 +#include "BKE_collection.h"
  #include "BKE_DerivedMesh.h"
  #include "BKE_object.h"
  #include "BKE_material.h"
@@@ -89,6 -88,7 +89,6 @@@
  #include "BKE_modifier.h"
  #include "BKE_scene.h"
  #include "BKE_bvhutils.h"
 -#include "BKE_depsgraph.h"
  
  #include "PIL_time.h"
  
@@@ -2896,6 -2896,7 +2896,6 @@@ static void psys_update_path_cache(Part
        ParticleSystem *psys = sim->psys;
        ParticleSettings *part = psys->part;
        ParticleEditSettings *pset = &sim->scene->toolsettings->particle;
 -      Base *base;
        int distr=0, alloc=0, skip=0;
  
        if ((psys->part->childtype && psys->totchild != psys_get_tot_child(sim->scene, psys)) || psys->recalc&PSYS_RECALC_RESET)
  
  
        /* particle instance modifier with "path" option need cached paths even if particle system doesn't */
 -      for (base = sim->scene->base.first; base; base= base->next) {
 -              ModifierData *md = modifiers_findByType(base->object, eModifierType_ParticleInstance);
 +      FOREACH_SCENE_OBJECT(sim->scene, ob)
 +      {
 +              ModifierData *md = modifiers_findByType(ob, eModifierType_ParticleInstance);
                if (md) {
                        ParticleInstanceModifierData *pimd = (ParticleInstanceModifierData *)md;
                        if (pimd->flag & eParticleInstanceFlag_Path && pimd->ob == sim->ob && pimd->psys == (psys - (ParticleSystem*)sim->ob->particlesystem.first)) {
                        }
                }
        }
 +      FOREACH_SCENE_OBJECT_END
  
        if (!skip) {
                psys_cache_paths(sim, cfra, use_render_params);
@@@ -4321,8 -4320,6 +4321,8 @@@ void particle_system_update(Scene *scen
        /* save matrix for duplicators, at rendertime the actual dupliobject's matrix is used so don't update! */
        if (psys->renderdata==0)
                invert_m4_m4(psys->imat, ob->obmat);
 +
 +      BKE_particle_batch_cache_dirty(psys, BKE_PARTICLE_BATCH_DIRTY_ALL);
  }
  
  /* ID looper */
@@@ -4353,31 -4350,12 +4353,30 @@@ void BKE_particlesystem_id_loop(Particl
  
  /* **** Depsgraph evaluation **** */
  
 -void BKE_particle_system_eval_init(EvaluationContext *UNUSED(eval_ctx),
 +void BKE_particle_system_settings_eval(struct EvaluationContext *UNUSED(eval_ctx),
 +                                       ParticleSystem *psys)
 +{
 +      if (G.debug & G_DEBUG_DEPSGRAPH) {
 +              printf("%s on %s (%p)\n", __func__, psys->name, psys);
 +      }
 +      psys->recalc |= psys->part->recalc;
 +}
 +
 +void BKE_particle_system_settings_recalc_clear(struct EvaluationContext *UNUSED(eval_ctx),
 +                                               ParticleSettings *particle_settings)
 +{
 +      if (G.debug & G_DEBUG_DEPSGRAPH) {
 +              printf("%s on %s (%p)\n", __func__, particle_settings->id.name, particle_settings);
 +      }
 +      particle_settings->recalc = 0;
 +}
 +
- void BKE_particle_system_eval(struct EvaluationContext *UNUSED(eval_ctx),
-                               Scene *scene,
-                               Object *ob,
-                               ParticleSystem *psys)
++void BKE_particle_system_eval_init(struct EvaluationContext *UNUSED(eval_ctx),
+                                    Scene *scene,
+                                    Object *ob)
  {
        if (G.debug & G_DEBUG_DEPSGRAPH) {
-               printf("%s on %s:%s (%p)\n", __func__, ob->id.name, psys->name, psys);
 -              printf("%s on %s\n", __func__, ob->id.name);
++              printf("%s on %s (%p)\n", __func__, ob->id.name, ob);
        }
        BKE_ptcache_object_reset(scene, ob, PTCACHE_RESET_DEPSGRAPH);
  }
index 5a552414cd321c1aadcc6cff1588d922b2a958f6,a90f8ff02b63c0fd238d92903563612c003b6228..93db8724dcee38e6d4617b782ed3f7e393104812
@@@ -62,7 -62,6 +62,7 @@@ extern "C" 
  #include "DNA_node_types.h"
  #include "DNA_particle_types.h"
  #include "DNA_object_types.h"
 +#include "DNA_lightprobe_types.h"
  #include "DNA_rigidbody_types.h"
  #include "DNA_scene_types.h"
  #include "DNA_texture_types.h"
@@@ -73,6 -72,7 +73,6 @@@
  #include "BKE_animsys.h"
  #include "BKE_constraint.h"
  #include "BKE_curve.h"
 -#include "BKE_depsgraph.h"
  #include "BKE_effect.h"
  #include "BKE_fcurve.h"
  #include "BKE_idcode.h"
  #include "DEG_depsgraph_build.h"
  
  #include "intern/builder/deg_builder.h"
 +#include "intern/eval/deg_eval_copy_on_write.h"
  #include "intern/nodes/deg_node.h"
  #include "intern/nodes/deg_node_component.h"
  #include "intern/nodes/deg_node_operation.h"
@@@ -126,7 -125,7 +126,7 @@@ static void modifier_walk(void *user_da
  {
        BuilderWalkUserData *data = (BuilderWalkUserData *)user_data;
        if (*obpoin) {
 -              data->builder->build_object(data->scene, NULL, *obpoin);
 +              data->builder->build_object(data->scene, *obpoin);
        }
  }
  
@@@ -139,7 -138,7 +139,7 @@@ void constraint_walk(bConstraint * /*co
        if (*idpoin) {
                ID *id = *idpoin;
                if (GS(id->name) == ID_OB) {
 -                      data->builder->build_object(data->scene, NULL, (Object *)id);
 +                      data->builder->build_object(data->scene, (Object *)id);
                }
        }
  }
@@@ -163,23 -162,7 +163,23 @@@ DepsgraphNodeBuilder::~DepsgraphNodeBui
  
  IDDepsNode *DepsgraphNodeBuilder::add_id_node(ID *id)
  {
 -      return m_graph->add_id_node(id, id->name);
 +      IDDepsNode *id_node = m_graph->add_id_node(id);
 +#ifdef WITH_COPY_ON_WRITE
 +      /* Currently all ID nodes are supposed to have copy-on-write logic.
 +       *
 +       * NOTE: Zero number of components indicates that ID node was just created.
 +       */
 +      if (BLI_ghash_size(id_node->components) == 0) {
 +              ComponentDepsNode *comp_cow =
 +                      id_node->add_component(DEG_NODE_TYPE_COPY_ON_WRITE);
 +              OperationDepsNode *op_cow = comp_cow->add_operation(
 +                  function_bind(deg_evaluate_copy_on_write, _1, m_graph, id_node),
 +                  DEG_OPCODE_COPY_ON_WRITE,
 +                  "", -1);
 +              m_graph->operations.push_back(op_cow);
 +      }
 +#endif
 +      return id_node;
  }
  
  TimeSourceDepsNode *DepsgraphNodeBuilder::add_time_source()
@@@ -290,11 -273,6 +290,11 @@@ OperationDepsNode *DepsgraphNodeBuilder
        return find_operation_node(id, comp_type, "", opcode, name, name_tag);
  }
  
 +ID *DepsgraphNodeBuilder::get_cow_id(const ID *id_orig) const
 +{
 +      return m_graph->get_cow_id(id_orig);
 +}
 +
  /* **** Build functions for entity nodes **** */
  
  void DepsgraphNodeBuilder::begin_build(Main *bmain) {
        } FOREACH_NODETREE_END
  }
  
 -void DepsgraphNodeBuilder::build_group(Scene *scene,
 -                                       Base *base,
 -                                       Group *group)
 +void DepsgraphNodeBuilder::build_group(Scene *scene, Group *group)
  {
        ID *group_id = &group->id;
        if (group_id->tag & LIB_TAG_DOIT) {
        group_id->tag |= LIB_TAG_DOIT;
  
        LINKLIST_FOREACH (GroupObject *, go, &group->gobject) {
 -              build_object(scene, base, go->ob);
 +              build_object(scene, go->ob);
        }
  }
  
 -void DepsgraphNodeBuilder::build_object(Scene *scene, Base *base, Object *ob)
 +void DepsgraphNodeBuilder::build_object(Scene *scene, Object *ob)
  {
 -      const bool has_object = (ob->id.tag & LIB_TAG_DOIT);
 -      IDDepsNode *id_node = (has_object)
 -              ? m_graph->find_id_node(&ob->id)
 -              : add_id_node(&ob->id);
 -      /* Update node layers.
 -       * Do it for both new and existing ID nodes. This is so because several
 -       * bases might be sharing same object.
 -       */
 -      if (base != NULL) {
 -              id_node->layers |= base->lay;
 -      }
 -      if (ob == scene->camera) {
 -              /* Camera should always be updated, it used directly by viewport. */
 -              id_node->layers |= (unsigned int)(-1);
 -      }
        /* Skip rest of components if the ID node was already there. */
 -      if (has_object) {
 +      if (ob->id.tag & LIB_TAG_DOIT) {
                return;
        }
        ob->id.tag |= LIB_TAG_DOIT;
 +
 +      /* Create ID node for obejct and begin init. */
 +      IDDepsNode *id_node = add_id_node(&ob->id);
        ob->customdata_mask = 0;
  
        /* Standard components. */
        build_object_transform(scene, ob);
  
        if (ob->parent != NULL) {
 -              build_object(scene, NULL, ob->parent);
 +              build_object(scene, ob->parent);
        }
        if (ob->modifiers.first != NULL) {
                BuilderWalkUserData data;
                                build_camera(ob);
                                break;
  
 +                      case OB_LIGHTPROBE:
 +                              build_lightprobe(ob);
 +                              break;
 +
                        default:
                        {
                                ID *obdata = (ID *)ob->data;
        /* Object that this is a proxy for. */
        if (ob->proxy) {
                ob->proxy->proxy_from = ob;
 -              build_object(scene, base, ob->proxy);
 +              build_object(scene, ob->proxy);
        }
  
        /* Object dupligroup. */
        if (ob->dup_group != NULL) {
 -              build_group(scene, base, ob->dup_group);
 +              build_group(scene, ob->dup_group);
        }
  }
  
  void DepsgraphNodeBuilder::build_object_transform(Scene *scene, Object *ob)
  {
        OperationDepsNode *op_node;
 +      Scene *scene_cow = get_cow_datablock(scene);
 +      Object *ob_cow = get_cow_datablock(ob);
  
        /* local transforms (from transform channels - loc/rot/scale + deltas) */
        op_node = add_operation_node(&ob->id, DEG_NODE_TYPE_TRANSFORM,
 -                                   function_bind(BKE_object_eval_local_transform, _1, scene, ob),
 +                                   function_bind(BKE_object_eval_local_transform,
 +                                                 _1,
 +                                                 scene_cow, ob_cow),
                                     DEG_OPCODE_TRANSFORM_LOCAL);
        op_node->set_as_entry();
  
        /* object parent */
 -      if (ob->parent) {
 +      if (ob->parent != NULL) {
                add_operation_node(&ob->id, DEG_NODE_TYPE_TRANSFORM,
 -                                 function_bind(BKE_object_eval_parent, _1, scene, ob),
 +                                 function_bind(BKE_object_eval_parent,
 +                                               _1,
 +                                               scene_cow, ob_cow),
                                   DEG_OPCODE_TRANSFORM_PARENT);
        }
  
        /* object constraints */
 -      if (ob->constraints.first) {
 +      if (ob->constraints.first != NULL) {
                build_object_constraints(scene, ob);
        }
  
 -      /* Temporary uber-update node, which does everything.
 -       * It is for the being we're porting old dependencies into the new system.
 -       * We'll get rid of this node as soon as all the granular update functions
 -       * are filled in.
 -       *
 -       * TODO(sergey): Get rid of this node.
 -       */
 +      /* Rest of transformation update. */
        add_operation_node(&ob->id, DEG_NODE_TYPE_TRANSFORM,
 -                         function_bind(BKE_object_eval_uber_transform, _1, scene, ob),
 -                         DEG_OPCODE_OBJECT_UBEREVAL);
 +                         function_bind(BKE_object_eval_uber_transform,
 +                                       _1,
 +                                       scene_cow, ob_cow),
 +                         DEG_OPCODE_TRANSFORM_OBJECT_UBEREVAL);
  
        /* object transform is done */
        op_node = add_operation_node(&ob->id, DEG_NODE_TYPE_TRANSFORM,
 -                                   function_bind(BKE_object_eval_done, _1, ob),
 +                                   function_bind(BKE_object_eval_done, _1, ob_cow),
                                     DEG_OPCODE_TRANSFORM_FINAL);
        op_node->set_as_exit();
  }
@@@ -505,9 -491,7 +505,9 @@@ void DepsgraphNodeBuilder::build_object
  {
        /* create node for constraint stack */
        add_operation_node(&ob->id, DEG_NODE_TYPE_TRANSFORM,
 -                         function_bind(BKE_object_eval_constraints, _1, scene, ob),
 +                         function_bind(BKE_object_eval_constraints, _1,
 +                                       get_cow_datablock(scene),
 +                                       get_cow_datablock(ob)),
                           DEG_OPCODE_TRANSFORM_CONSTRAINTS);
  }
  
  void DepsgraphNodeBuilder::build_animdata(ID *id)
  {
        AnimData *adt = BKE_animdata_from_id(id);
 -
 -      if (adt == NULL)
 +      if (adt == NULL) {
                return;
 +      }
  
        /* animation */
        if (adt->action || adt->nla_tracks.first || adt->drivers.first) {
 -              // XXX: Hook up specific update callbacks for special properties which may need it...
 +              (void) add_id_node(id);
 +              ID *id_cow = get_cow_id(id);
 +
 +              // XXX: Hook up specific update callbacks for special properties which
 +              // may need it...
  
 -              /* actions and NLA - as a single unit for now, as it gets complicated to schedule otherwise */
 +              /* actions and NLA - as a single unit for now, as it gets complicated to
 +               * schedule otherwise.
 +               */
                if ((adt->action) || (adt->nla_tracks.first)) {
                        /* create the node */
                        add_operation_node(id, DEG_NODE_TYPE_ANIMATION,
 -                                         function_bind(BKE_animsys_eval_animdata, _1, id),
 +                                         function_bind(BKE_animsys_eval_animdata, _1, id_cow),
                                           DEG_OPCODE_ANIMATION, id->name);
  
 -                      // TODO: for each channel affected, we might also want to add some support for running RNA update callbacks on them
 -                      // (which will be needed for proper handling of drivers later)
 +                      /* TODO: for each channel affected, we might also want to add some
 +                       * support for running RNA update callbacks on them
 +                       * (which will be needed for proper handling of drivers later)
 +                       */
                }
  
                /* drivers */
  OperationDepsNode *DepsgraphNodeBuilder::build_driver(ID *id, FCurve *fcu)
  {
        ChannelDriver *driver = fcu->driver;
 +      ID *id_cow = get_cow_id(id);
  
        /* Create data node for this driver */
        /* TODO(sergey): Avoid creating same operation multiple times,
                                                           fcu->array_index);
  
        if (driver_op == NULL) {
 +              /* TODO(sergey): Shall we use COW of fcu itself here? */
                driver_op = add_operation_node(id,
                                               DEG_NODE_TYPE_PARAMETERS,
 -                                             function_bind(BKE_animsys_eval_driver, _1, id, fcu),
 +                                             function_bind(BKE_animsys_eval_driver, _1, id_cow, fcu),
                                               DEG_OPCODE_DRIVER,
                                               fcu->rna_path ? fcu->rna_path : "",
                                               fcu->array_index);
@@@ -604,10 -578,10 +604,10 @@@ void DepsgraphNodeBuilder::build_world(
        build_animdata(world_id);
  
        /* world itself */
 -      add_component_node(world_id, DEG_NODE_TYPE_PARAMETERS);
 -
 -      add_operation_node(world_id, DEG_NODE_TYPE_PARAMETERS, NULL,
 -                         DEG_OPCODE_PLACEHOLDER, "Parameters Eval");
 +      add_operation_node(world_id,
 +                         DEG_NODE_TYPE_SHADING,
 +                         function_bind(BKE_world_eval, _1, world),
 +                         DEG_OPCODE_WORLD_UPDATE);
  
        /* textures */
        build_texture_stack(world->mtex);
  void DepsgraphNodeBuilder::build_rigidbody(Scene *scene)
  {
        RigidBodyWorld *rbw = scene->rigidbody_world;
 +      Scene *scene_cow = get_cow_datablock(scene);
  
        /**
         * Rigidbody Simulation Nodes
         * ==========================
         *
         * There are 3 nodes related to Rigidbody Simulation:
 -       * 1) "Initialize/Rebuild World" - this is called sparingly, only when the simulation
 -       *    needs to be rebuilt (mainly after file reload, or moving back to start frame)
 -       * 2) "Do Simulation" - perform a simulation step - interleaved between the evaluation
 -       *    steps for clusters of objects (i.e. between those affected and/or not affected by
 -       *    the sim for instance)
 +       * 1) "Initialize/Rebuild World" - this is called sparingly, only when the
 +       *    simulation needs to be rebuilt (mainly after file reload, or moving
 +       *    back to start frame)
 +       * 2) "Do Simulation" - perform a simulation step - interleaved between the
 +       *    evaluation steps for clusters of objects (i.e. between those affected
 +       *    and/or not affected by the sim for instance).
         *
 -       * 3) "Pull Results" - grab the specific transforms applied for a specific object -
 -       *    performed as part of object's transform-stack building
 +       * 3) "Pull Results" - grab the specific transforms applied for a specific
 +       *    object - performed as part of object's transform-stack building.
         */
  
 -      /* create nodes ------------------------------------------------------------------------ */
 -      /* XXX: is this the right component, or do we want to use another one instead? */
 +      /* Create nodes --------------------------------------------------------- */
 +
 +      /* XXX: is this the right component, or do we want to use another one
 +       * instead?
 +       */
  
        /* init/rebuild operation */
 -      /*OperationDepsNode *init_node =*/ add_operation_node(&scene->id, DEG_NODE_TYPE_TRANSFORM,
 -                                                            function_bind(BKE_rigidbody_rebuild_sim, _1, scene),
 -                                                            DEG_OPCODE_RIGIDBODY_REBUILD);
 +      /*OperationDepsNode *init_node =*/ add_operation_node(
 +              &scene->id, DEG_NODE_TYPE_TRANSFORM,
 +              function_bind(BKE_rigidbody_rebuild_sim, _1, scene_cow),
 +              DEG_OPCODE_RIGIDBODY_REBUILD);
  
        /* do-sim operation */
        // XXX: what happens if we need to split into several groups?
 -      OperationDepsNode *sim_node     = add_operation_node(&scene->id, DEG_NODE_TYPE_TRANSFORM,
 -                                                           function_bind(BKE_rigidbody_eval_simulation, _1, scene),
 -                                                           DEG_OPCODE_RIGIDBODY_SIM);
 +      OperationDepsNode *sim_node = add_operation_node(
 +              &scene->id, DEG_NODE_TYPE_TRANSFORM,
 +              function_bind(BKE_rigidbody_eval_simulation, _1, scene_cow),
 +              DEG_OPCODE_RIGIDBODY_SIM);
  
 -      /* XXX: For now, the sim node is the only one that really matters here. If any other
 -       * sims get added later, we may have to remove these hacks...
 +      /* XXX: For now, the sim node is the only one that really matters here.
 +       * If any other sims get added later, we may have to remove these hacks...
         */
        sim_node->owner->entry_operation = sim_node;
        sim_node->owner->exit_operation  = sim_node;
  
 -
        /* objects - simulation participants */
        if (rbw->group) {
                LINKLIST_FOREACH (GroupObject *, go, &rbw->group->gobject) {
                                continue;
  
                        /* 2) create operation for flushing results */
 -                      /* object's transform component - where the rigidbody operation lives */
 +                      /* object's transform component - where the rigidbody operation
 +                       * lives. */
                        add_operation_node(&ob->id, DEG_NODE_TYPE_TRANSFORM,
 -                                         function_bind(BKE_rigidbody_object_sync_transforms, _1, scene, ob),
 -                                         DEG_OPCODE_TRANSFORM_RIGIDBODY);
 +                                         function_bind(BKE_rigidbody_object_sync_transforms,
 +                                                       _1,
 +                                                       scene_cow,
 +                                                       get_cow_datablock(ob)),
 +                                         DEG_OPCODE_RIGIDBODY_TRANSFORM_COPY);
                }
        }
  }
@@@ -707,75 -671,42 +707,78 @@@ void DepsgraphNodeBuilder::build_partic
        ComponentDepsNode *psys_comp =
                add_component_node(&ob->id, DEG_NODE_TYPE_EVAL_PARTICLES);
  
 -                                       scene,
 -                                       ob),
 -                         DEG_OPCODE_PSYS_EVAL_INIT);
 +      /* TODO(sergey): Need to get COW of PSYS. */
 +      Scene *scene_cow = get_cow_datablock(scene);
 +      Object *ob_cow = get_cow_datablock(ob);
 +
+       add_operation_node(psys_comp,
+                          function_bind(BKE_particle_system_eval_init,
+                                        _1,
++                                       scene_cow,
++                                       ob_cow),
++                         DEG_OPCODE_PARTICLE_SYSTEM_EVAL_INIT);
        /* particle systems */
        LINKLIST_FOREACH (ParticleSystem *, psys, &ob->particlesystem) {
                ParticleSettings *part = psys->part;
  
 -              /* particle settings */
 -              // XXX: what if this is used more than once!
 -              build_animdata(&part->id);
 +              /* Build particle settings operations.
 +               *
 +               * NOTE: The call itself ensures settings are only build once.
 +               */
 +              build_particle_settings(part);
  
 -              /* this particle system */
 -              // TODO: for now, this will just be a placeholder "ubereval" node
 +              /* Update on particle settings change. */
 +              add_operation_node(psys_comp,
 +                                 function_bind(BKE_particle_system_settings_eval,
 +                                               _1,
 +                                               psys),
 +                                 DEG_OPCODE_PARTICLE_SETTINGS_EVAL,
 +                                 psys->name);
 +
 +              /* Particle system evaluation. */
                add_operation_node(psys_comp,
-                                  function_bind(BKE_particle_system_eval,
-                                                _1,
-                                                scene_cow,
-                                                ob_cow,
-                                                psys),
+                                  NULL,
 -                                 DEG_OPCODE_PSYS_EVAL,
 +                                 DEG_OPCODE_PARTICLE_SYSTEM_EVAL,
                                   psys->name);
        }
  
 -      /* pointcache */
 -      // TODO...
 +      /* TODO(sergey): Do we need a point cache operations here? */
 +}
 +
 +void DepsgraphNodeBuilder::build_particle_settings(ParticleSettings *part) {
 +      ID *part_id = &part->id;
 +      if (part_id->tag & LIB_TAG_DOIT) {
 +              return;
 +      }
 +      part_id->tag |= LIB_TAG_DOIT;
 +      /* Animation data. */
 +      build_animdata(part_id);
 +      /* Parameters change. */
 +      add_operation_node(part_id,
 +                         DEG_NODE_TYPE_PARAMETERS,
 +                         NULL,
 +                         DEG_OPCODE_PARTICLE_SETTINGS_EVAL);
 +      add_operation_node(part_id,
 +                         DEG_NODE_TYPE_PARAMETERS,
 +                         function_bind(BKE_particle_system_settings_recalc_clear,
 +                                       _1,
 +                                       part),
 +                         DEG_OPCODE_PARTICLE_SETTINGS_RECALC_CLEAR);
  }
  
  void DepsgraphNodeBuilder::build_cloth(Scene *scene, Object *object)
  {
 +      Scene *scene_cow = get_cow_datablock(scene);
 +      Object *object_cow = get_cow_datablock(object);
 +
        ComponentDepsNode *cache_comp = add_component_node(&object->id,
                                                           DEG_NODE_TYPE_CACHE);
        add_operation_node(cache_comp,
                           function_bind(BKE_object_eval_cloth,
                                         _1,
 -                                       scene,
 -                                       object),
 +                                       scene_cow,
 +                                       object_cow),
                           DEG_OPCODE_PLACEHOLDER,
                           "Cloth Modifier");
  }
@@@ -793,9 -724,8 +796,9 @@@ void DepsgraphNodeBuilder::build_shapek
  // XXX: what happens if the datablock is shared!
  void DepsgraphNodeBuilder::build_obdata_geom(Scene *scene, Object *ob)
  {
 -      ID *obdata = (ID *)ob->data;
        OperationDepsNode *op_node;
 +      Scene *scene_cow = get_cow_datablock(scene);
 +      Object *object_cow = get_cow_datablock(ob);
  
        /* TODO(sergey): This way using this object's properties as driver target
         * works fine.
        op_node = add_operation_node(&ob->id,
                                     DEG_NODE_TYPE_PARAMETERS,
                                     NULL,
 -                                   DEG_OPCODE_PLACEHOLDER,
 -                                   "Parameters Eval");
 +                                   DEG_OPCODE_PARAMETERS_EVAL);
        op_node->set_as_exit();
  
        /* Temporary uber-update node, which does everything.
         */
        op_node = add_operation_node(&ob->id,
                                     DEG_NODE_TYPE_GEOMETRY,
 -                                   function_bind(BKE_object_eval_uber_data, _1, scene, ob),
 +                                   function_bind(BKE_object_eval_uber_data,
 +                                                 _1,
 +                                                 scene_cow,
 +                                                 object_cow),
                                     DEG_OPCODE_GEOMETRY_UBEREVAL);
        op_node->set_as_exit();
  
        }
  
        /* materials */
 -      for (int a = 1; a <= ob->totcol; a++) {
 -              Material *ma = give_current_material(ob, a);
 -              if (ma != NULL) {
 -                      build_material(ma);
 +      if (ob->totcol != 0) {
 +              if (ob->type == OB_MESH) {
 +                      add_operation_node(&ob->id,
 +                                         DEG_NODE_TYPE_SHADING,
 +                                         function_bind(BKE_object_eval_update_shading, _1,
 +                                                       object_cow),
 +                                         DEG_OPCODE_SHADING);
 +              }
 +
 +              for (int a = 1; a <= ob->totcol; a++) {
 +                      Material *ma = give_current_material(ob, a);
 +                      if (ma != NULL) {
 +                              build_material(ma);
 +                      }
                }
        }
  
                // add geometry collider relations
        }
  
 +      ID *obdata = (ID *)ob->data;
        if (obdata->tag & LIB_TAG_DOIT) {
                return;
        }
 +      obdata->tag |= LIB_TAG_DOIT;
 +      /* Make sure we've got an ID node before requesting CoW pointer. */
 +      (void) add_id_node((ID *)obdata);
 +      ID *obdata_cow = get_cow_id(obdata);
  
        /* ShapeKeys */
        Key *key = BKE_key_from_object(ob);
                                                     DEG_NODE_TYPE_GEOMETRY,
                                                     function_bind(BKE_mesh_eval_geometry,
                                                                   _1,
 -                                                                 (Mesh *)obdata),
 +                                                                 (Mesh *)obdata_cow),
                                                     DEG_OPCODE_PLACEHOLDER,
                                                     "Geometry Eval");
                        op_node->set_as_entry();
                                                             DEG_NODE_TYPE_GEOMETRY,
                                                             function_bind(BKE_mball_eval_geometry,
                                                                           _1,
 -                                                                         (MetaBall *)obdata),
 +                                                                         (MetaBall *)obdata_cow),
                                                             DEG_OPCODE_PLACEHOLDER,
                                                             "Geometry Eval");
                                op_node->set_as_entry();
                                                     DEG_NODE_TYPE_GEOMETRY,
                                                     function_bind(BKE_curve_eval_geometry,
                                                                   _1,
 -                                                                 (Curve *)obdata),
 +                                                                 (Curve *)obdata_cow),
                                                                   DEG_OPCODE_PLACEHOLDER,
                                                                   "Geometry Eval");
                        op_node->set_as_entry();
                                                   DEG_NODE_TYPE_GEOMETRY,
                                                   function_bind(BKE_curve_eval_path,
                                                                 _1,
 -                                                               (Curve *)obdata),
 +                                                               (Curve *)obdata_cow),
                                                   DEG_OPCODE_GEOMETRY_PATH,
                                                   "Path");
                        }
                         */
                        Curve *cu = (Curve *)obdata;
                        if (cu->bevobj != NULL) {
 -                              build_object(scene, NULL, cu->bevobj);
 +                              build_object(scene, cu->bevobj);
                        }
                        if (cu->taperobj != NULL) {
 -                              build_object(scene, NULL, cu->taperobj);
 +                              build_object(scene, cu->taperobj);
                        }
                        if (ob->type == OB_FONT && cu->textoncurve != NULL) {
 -                              build_object(scene, NULL, cu->textoncurve);
 +                              build_object(scene, cu->textoncurve);
                        }
                        break;
                }
                                                     DEG_NODE_TYPE_GEOMETRY,
                                                     function_bind(BKE_lattice_eval_geometry,
                                                                   _1,
 -                                                                 (Lattice *)obdata),
 +                                                                 (Lattice *)obdata_cow),
                                                                   DEG_OPCODE_PLACEHOLDER,
                                                                   "Geometry Eval");
                        op_node->set_as_entry();
        op_node->set_as_exit();
  
        /* Parameters for driver sources. */
 -      add_operation_node(obdata, DEG_NODE_TYPE_PARAMETERS, NULL,
 -                         DEG_OPCODE_PLACEHOLDER, "Parameters Eval");
 +      add_operation_node(obdata,
 +                         DEG_NODE_TYPE_PARAMETERS,
 +                         NULL,
 +                         DEG_OPCODE_PARAMETERS_EVAL);
  }
  
  /* Cameras */
@@@ -1000,10 -911,8 +1003,10 @@@ void DepsgraphNodeBuilder::build_camera
  
        build_animdata(&cam->id);
  
 -      add_operation_node(camera_id, DEG_NODE_TYPE_PARAMETERS, NULL,
 -                         DEG_OPCODE_PLACEHOLDER, "Parameters Eval");
 +      add_operation_node(camera_id,
 +                         DEG_NODE_TYPE_PARAMETERS,
 +                         NULL,
 +                         DEG_OPCODE_PARAMETERS_EVAL);
  
        if (cam->dof_ob != NULL) {
                /* TODO(sergey): For now parametrs are on object level. */
@@@ -1027,10 -936,8 +1030,10 @@@ void DepsgraphNodeBuilder::build_lamp(O
        add_component_node(lamp_id, DEG_NODE_TYPE_PARAMETERS);
  
        /* TODO(sergey): Is it really how we're supposed to work with drivers? */
 -      add_operation_node(lamp_id, DEG_NODE_TYPE_PARAMETERS, NULL,
 -                         DEG_OPCODE_PLACEHOLDER, "Parameters Eval");
 +      add_operation_node(lamp_id,
 +                         DEG_NODE_TYPE_PARAMETERS,
 +                         NULL,
 +                         DEG_OPCODE_PARAMETERS_EVAL);
  
        /* lamp's nodetree */
        if (la->nodetree) {
@@@ -1053,10 -960,8 +1056,10 @@@ void DepsgraphNodeBuilder::build_nodetr
        build_animdata(ntree_id);
  
        /* Parameters for drivers. */
 -      op_node = add_operation_node(ntree_id, DEG_NODE_TYPE_PARAMETERS, NULL,
 -                                   DEG_OPCODE_PLACEHOLDER, "Parameters Eval");
 +      op_node = add_operation_node(ntree_id,
 +                                   DEG_NODE_TYPE_PARAMETERS,
 +                                   NULL,
 +                                   DEG_OPCODE_PARAMETERS_EVAL);
        op_node->set_as_exit();
  
        /* nodetree's nodes... */
@@@ -1096,10 -1001,8 +1099,10 @@@ void DepsgraphNodeBuilder::build_materi
        /* material itself */
        add_id_node(ma_id);
  
 -      add_operation_node(ma_id, DEG_NODE_TYPE_SHADING, NULL,
 -                         DEG_OPCODE_PLACEHOLDER, "Material Update");
 +      add_operation_node(ma_id,
 +                         DEG_NODE_TYPE_SHADING,
 +                         function_bind(BKE_material_eval, _1, ma),
 +                         DEG_OPCODE_MATERIAL_UPDATE);
  
        /* material animation */
        build_animdata(ma_id);
@@@ -1205,35 -1108,10 +1208,35 @@@ void DepsgraphNodeBuilder::build_mask(M
        build_animdata(mask_id);
  }
  
 -void DepsgraphNodeBuilder::build_movieclip(MovieClip *clip) {
 +void DepsgraphNodeBuilder::build_movieclip(MovieClip *clip)
 +{
        ID *clip_id = &clip->id;
        add_id_node(clip_id);
        build_animdata(clip_id);
  }
  
 +void DepsgraphNodeBuilder::build_lightprobe(Object *object)
 +{
 +      LightProbe *probe = (LightProbe *)object->data;
 +      ID *probe_id = &probe->id;
 +      if (probe_id->tag & LIB_TAG_DOIT) {
 +              return;
 +      }
 +      probe_id->tag |= LIB_TAG_DOIT;
 +      /* Placeholder so we can add relations and tag ID node for update. */
 +      add_operation_node(probe_id,
 +                         DEG_NODE_TYPE_PARAMETERS,
 +                         NULL,
 +                         DEG_OPCODE_PLACEHOLDER,
 +                         "LightProbe Eval");
 +
 +      add_operation_node(&object->id,
 +                         DEG_NODE_TYPE_PARAMETERS,
 +                         NULL,
 +                         DEG_OPCODE_PLACEHOLDER,
 +                         "LightProbe Eval");
 +
 +      build_animdata(probe_id);
 +}
 +
  }  // namespace DEG
index 03cae598d245ee34da3a2153c8ac311094f393bf,a1abcb9641139af42025371883e036874dd276ee..93d9aab18987d8f5009ab8fc9b18391e2d6e01d8
@@@ -61,7 -61,6 +61,7 @@@ extern "C" 
  #include "DNA_movieclip_types.h"
  #include "DNA_node_types.h"
  #include "DNA_particle_types.h"
 +#include "DNA_lightprobe_types.h"
  #include "DNA_object_types.h"
  #include "DNA_rigidbody_types.h"
  #include "DNA_scene_types.h"
@@@ -302,10 -301,10 +302,10 @@@ void DepsgraphRelationBuilder::add_oper
        }
  }
  
 -void DepsgraphRelationBuilder::add_collision_relations(const OperationKey &key, Scene *scene, Object *ob, Group *group, int layer, bool dupli, const char *name)
 +void DepsgraphRelationBuilder::add_collision_relations(const OperationKey &key, Scene *scene, Object *ob, Group *group, bool dupli, const char *name)
  {
        unsigned int numcollobj;
 -      Object **collobjs = get_collisionobjects_ext(scene, ob, group, layer, &numcollobj, eModifierType_Collision, dupli);
 +      Object **collobjs = get_collisionobjects_ext(scene, ob, group, &numcollobj, eModifierType_Collision, dupli);
  
        for (unsigned int i = 0; i < numcollobj; i++)
        {
@@@ -343,7 -342,7 +343,7 @@@ void DepsgraphRelationBuilder::add_forc
                                        add_relation(mod_key, key, name);
                                }
                                else if (eff->psys != psys) {
 -                                      OperationKey eff_key(&eff->ob->id, DEG_NODE_TYPE_EVAL_PARTICLES, DEG_OPCODE_PSYS_EVAL, eff->psys->name);
 +                                      OperationKey eff_key(&eff->ob->id, DEG_NODE_TYPE_EVAL_PARTICLES, DEG_OPCODE_PARTICLE_SYSTEM_EVAL, eff->psys->name);
                                        add_relation(eff_key, key, name);
                                }
                        }
                        }
  
                        if (add_absorption && (eff->pd->flag & PFIELD_VISIBILITY)) {
 -                              add_collision_relations(key, scene, ob, NULL, eff->ob->lay, true, "Force Absorption");
 +                              add_collision_relations(key, scene, ob, NULL, true, "Force Absorption");
                        }
                }
        }
@@@ -423,7 -422,7 +423,7 @@@ void DepsgraphRelationBuilder::build_ob
        OperationKey parent_transform_key(&ob->id, DEG_NODE_TYPE_TRANSFORM, DEG_OPCODE_TRANSFORM_PARENT);
        OperationKey final_transform_key(&ob->id, DEG_NODE_TYPE_TRANSFORM, DEG_OPCODE_TRANSFORM_FINAL);
  
 -      OperationKey ob_ubereval_key(&ob->id, DEG_NODE_TYPE_TRANSFORM, DEG_OPCODE_OBJECT_UBEREVAL);
 +      OperationKey ob_ubereval_key(&ob->id, DEG_NODE_TYPE_TRANSFORM, DEG_OPCODE_TRANSFORM_OBJECT_UBEREVAL);
  
        /* parenting */
        if (ob->parent != NULL) {
                                        build_proxy_rig(ob);
                                }
                                else {
 -                                      build_rig(scene, ob);
 +                                      build_rig(bmain, scene, ob);
                                }
                                break;
  
                        case OB_CAMERA: /* Camera */
                                build_camera(ob);
                                break;
 +
 +                      case OB_LIGHTPROBE:
 +                              build_lightprobe(ob);
 +                              break;
                }
  
                Key *key = BKE_key_from_object(ob);
@@@ -976,8 -971,8 +976,8 @@@ void DepsgraphRelationBuilder::build_dr
                                IDDepsNode *to_node = (IDDepsNode *)rel->to;
  
                                /* we only care about objects with pose data which use this... */
 -                              if (GS(to_node->id->name) == ID_OB) {
 -                                      Object *ob = (Object *)to_node->id;
 +                              if (GS(to_node->id_orig->name) == ID_OB) {
 +                                      Object *ob = (Object *)to_node->id_orig;
                                        bPoseChannel *pchan = BKE_pose_channel_find_name(ob->pose, bone_name); // NOTE: ob->pose may be NULL
  
                                        if (pchan) {
@@@ -1136,7 -1131,7 +1136,7 @@@ void DepsgraphRelationBuilder::build_wo
        if (world->nodetree != NULL) {
                build_nodetree(world->nodetree);
                ComponentKey ntree_key(&world->nodetree->id, DEG_NODE_TYPE_PARAMETERS);
 -              ComponentKey world_key(world_id, DEG_NODE_TYPE_PARAMETERS);
 +              ComponentKey world_key(world_id, DEG_NODE_TYPE_SHADING);
                add_relation(ntree_key, world_key, "NTree->World Parameters");
        }
  }
@@@ -1173,7 -1168,7 +1173,7 @@@ void DepsgraphRelationBuilder::build_ri
                         *    XXX: there's probably a difference between passive and active
                         *         - passive don't change, so may need to know full transform...
                         */
 -                      OperationKey rbo_key(&ob->id, DEG_NODE_TYPE_TRANSFORM, DEG_OPCODE_TRANSFORM_RIGIDBODY);
 +                      OperationKey rbo_key(&ob->id, DEG_NODE_TYPE_TRANSFORM, DEG_OPCODE_RIGIDBODY_TRANSFORM_COPY);
  
                        eDepsOperation_Code trans_opcode = ob->parent ? DEG_OPCODE_TRANSFORM_PARENT : DEG_OPCODE_TRANSFORM_LOCAL;
                        OperationKey trans_op(&ob->id, DEG_NODE_TYPE_TRANSFORM, trans_opcode);
                                 */
                                OperationKey uber_key(&ob->id,
                                                      DEG_NODE_TYPE_TRANSFORM,
 -                                                    DEG_OPCODE_OBJECT_UBEREVAL);
 +                                                    DEG_OPCODE_TRANSFORM_OBJECT_UBEREVAL);
                                add_relation(rbo_key, uber_key, "RBO Sync -> Uber (Temp)");
                        }
  
                         * constraint affects the physics sim for these objects
                         */
                        ComponentKey trans_key(&ob->id, DEG_NODE_TYPE_TRANSFORM);
 -                      OperationKey ob1_key(&rbc->ob1->id, DEG_NODE_TYPE_TRANSFORM, DEG_OPCODE_TRANSFORM_RIGIDBODY);
 -                      OperationKey ob2_key(&rbc->ob2->id, DEG_NODE_TYPE_TRANSFORM, DEG_OPCODE_TRANSFORM_RIGIDBODY);
 +                      OperationKey ob1_key(&rbc->ob1->id, DEG_NODE_TYPE_TRANSFORM, DEG_OPCODE_RIGIDBODY_TRANSFORM_COPY);
 +                      OperationKey ob2_key(&rbc->ob2->id, DEG_NODE_TYPE_TRANSFORM, DEG_OPCODE_RIGIDBODY_TRANSFORM_COPY);
  
                        /* - constrained-objects sync depends on the constraint-holder */
                        add_relation(trans_key, ob1_key, "RigidBodyConstraint -> RBC.Object_1");
@@@ -1244,49 -1239,29 +1244,53 @@@ void DepsgraphRelationBuilder::build_pa
        OperationKey obdata_ubereval_key(&ob->id,
                                         DEG_NODE_TYPE_GEOMETRY,
                                         DEG_OPCODE_GEOMETRY_UBEREVAL);
 -                                 DEG_OPCODE_PSYS_EVAL_INIT);
+       OperationKey eval_init_key(&ob->id,
+                                  DEG_NODE_TYPE_EVAL_PARTICLES,
++                                 DEG_OPCODE_PARTICLE_SYSTEM_EVAL_INIT);
+       /* TODO(sergey): Are all particle systems depends on time?
+        * Hair without dynamics i.e.
+        */
+       add_relation(time_src_key, eval_init_key, "TimeSrc -> PSys");
  
        /* particle systems */
        LINKLIST_FOREACH (ParticleSystem *, psys, &ob->particlesystem) {
                ParticleSettings *part = psys->part;
  
 -              /* particle settings */
 -              build_animdata(&part->id);
 -
 -              /* this particle system */
 -              OperationKey psys_key(&ob->id, DEG_NODE_TYPE_EVAL_PARTICLES, DEG_OPCODE_PSYS_EVAL, psys->name);
 +              /* Build particle settings relations.
 +               *
 +               * NOTE: The call itself ensures settings are only build once.
 +               */
 +              build_particle_settings(part);
 +
 +              /* This particle system. */
 +              OperationKey psys_key(&ob->id,
 +                                    DEG_NODE_TYPE_EVAL_PARTICLES,
 +                                    DEG_OPCODE_PARTICLE_SYSTEM_EVAL,
 +                                    psys->name);
 +
 +              /* Update particle system when settings changes. */
 +              OperationKey particle_settings_key(&part->id,
 +                                                 DEG_NODE_TYPE_PARAMETERS,
 +                                                 DEG_OPCODE_PARTICLE_SETTINGS_EVAL);
 +              OperationKey particle_settings_recalc_clear_key(
 +                      &part->id,
 +                      DEG_NODE_TYPE_PARAMETERS,
 +                      DEG_OPCODE_PARTICLE_SETTINGS_RECALC_CLEAR);
 +              OperationKey psys_settings_key(&ob->id,
 +                                             DEG_NODE_TYPE_EVAL_PARTICLES,
 +                                             DEG_OPCODE_PARTICLE_SETTINGS_EVAL,
 +                                             psys->name);
 +              add_relation(particle_settings_key, psys_settings_key, "Particle Settings Change");
 +              add_relation(psys_settings_key, psys_key, "Particle Settings Update");
 +              add_relation(psys_key,
 +                           particle_settings_recalc_clear_key,
 +                           "Particle Settings Recalc Clear");
  
                /* XXX: if particle system is later re-enabled, we must do full rebuild? */
                if (!psys_check_enabled(ob, psys, G.is_rendering))
                        continue;
  
-               /* TODO(sergey): Are all particle systems depends on time?
-                * Hair without dynamics i.e.
-                */
-               add_relation(time_src_key, psys_key, "TimeSrc -> PSys");
+               add_relation(eval_init_key, psys_key, "Init -> PSys");
  
                /* TODO(sergey): Currently particle update is just a placeholder,
                 * hook it to the ubereval node so particle system is getting updated
  
                /* collisions */
                if (part->type != PART_HAIR) {
 -                      add_collision_relations(psys_key, scene, ob, part->collision_group, ob->lay, true, "Particle Collision");
 +                      add_collision_relations(psys_key,
 +                                              scene,
 +                                              ob,
 +                                              part->collision_group,
 +                                              true,
 +                                              "Particle Collision");
                }
 -              else if ((psys->flag & PSYS_HAIR_DYNAMICS) && psys->clmd && psys->clmd->coll_parms) {
 -                      add_collision_relations(psys_key, scene, ob, psys->clmd->coll_parms->group, ob->lay | scene->lay, true, "Hair Collision");
 +              else if ((psys->flag & PSYS_HAIR_DYNAMICS) &&
 +                       psys->clmd != NULL &&
 +                       psys->clmd->coll_parms != NULL) {
 +                      add_collision_relations(psys_key,
 +                                              scene,
 +                                              ob,
 +                                              psys->clmd->coll_parms->group,
 +                                              true,
 +                                              "Hair Collision");
                }
  
                /* effectors */
 -              add_forcefield_relations(psys_key, scene, ob, psys, part->effector_weights, part->type == PART_HAIR, "Particle Field");
 +              add_forcefield_relations(psys_key,
 +                                       scene,
 +                                       ob,
 +                                       psys,
 +                                       part->effector_weights,
 +                                       part->type == PART_HAIR,
 +                                       "Particle Field");
  
                /* boids */
                if (part->boids) {
        ComponentKey transform_key(&ob->id, DEG_NODE_TYPE_TRANSFORM);
        add_relation(transform_key, obdata_ubereval_key, "Partcile Eval");
  
 -      /* pointcache */
 -      // TODO...
 +      /* TODO(sergey): Do we need a point cache operations here? */
 +}
 +
 +void DepsgraphRelationBuilder::build_particle_settings(ParticleSettings *part)
 +{
 +      ID *part_id = &part->id;
 +      if (part_id->tag & LIB_TAG_DOIT) {
 +              return;
 +      }
 +      part_id->tag |= LIB_TAG_DOIT;
 +
 +      /* Animation data relations. */
 +      build_animdata(&part->id);
 +
 +      OperationKey eval_key(part_id,
 +                            DEG_NODE_TYPE_PARAMETERS,
 +                            DEG_OPCODE_PARTICLE_SETTINGS_EVAL);
 +      OperationKey recalc_clear_key(part_id,
 +                                   DEG_NODE_TYPE_PARAMETERS,
 +                                   DEG_OPCODE_PARTICLE_SETTINGS_RECALC_CLEAR);
 +      add_relation(eval_key, recalc_clear_key, "Particle Settings Clear Recalc");
  }
  
  void DepsgraphRelationBuilder::build_cloth(Scene * /*scene*/,
@@@ -1452,22 -1390,12 +1456,22 @@@ void DepsgraphRelationBuilder::build_ob
        /* link components to each other */
        add_relation(obdata_geom_key, geom_key, "Object Geometry Base Data");
  
 +      OperationKey obdata_ubereval_key(&ob->id,
 +                                       DEG_NODE_TYPE_GEOMETRY,
 +                                       DEG_OPCODE_GEOMETRY_UBEREVAL);
 +
 +      /* Special case: modifiers and DerivedMesh creation queries scene for various
 +       * things like data mask to be used. We add relation here to ensure object is
 +       * never evaluated prior to Scene's CoW is ready.
 +       */
 +      OperationKey scene_key(&scene->id,
 +                             DEG_NODE_TYPE_PARAMETERS,
 +                             DEG_OPCODE_PLACEHOLDER,
 +                             "Scene Eval");
 +      add_relation(scene_key, obdata_ubereval_key, "CoW Relation");
 +
        /* Modifiers */
        if (ob->modifiers.first != NULL) {
 -              OperationKey obdata_ubereval_key(&ob->id,
 -                                               DEG_NODE_TYPE_GEOMETRY,
 -                                               DEG_OPCODE_GEOMETRY_UBEREVAL);
 -
                LINKLIST_FOREACH (ModifierData *, md, &ob->modifiers) {
                        const ModifierTypeInfo *mti = modifierType_getInfo((ModifierType)md->type);
  
                        Material *ma = give_current_material(ob, a);
                        if (ma != NULL) {
                                build_material(ma);
 +
 +                              if (ob->type == OB_MESH) {
 +                                      OperationKey material_key(&ma->id,
 +                                                                DEG_NODE_TYPE_SHADING,
 +                                                                DEG_OPCODE_MATERIAL_UPDATE);
 +                                      OperationKey shading_key(&ob->id, DEG_NODE_TYPE_SHADING, DEG_OPCODE_SHADING);
 +                                      add_relation(material_key, shading_key, "Material Update");
 +                              }
                        }
                }
        }
@@@ -1699,7 -1619,8 +1703,7 @@@ void DepsgraphRelationBuilder::build_no
  
        OperationKey parameters_key(ntree_id,
                                    DEG_NODE_TYPE_PARAMETERS,
 -                                  DEG_OPCODE_PLACEHOLDER,
 -                                  "Parameters Eval");
 +                                  DEG_OPCODE_PARAMETERS_EVAL);
  
        /* nodetree's nodes... */
        LINKLIST_FOREACH (bNode *, bnode, &ntree->nodes) {
                                }
                                OperationKey group_parameters_key(&group_ntree->id,
                                                                  DEG_NODE_TYPE_PARAMETERS,
 -                                                                DEG_OPCODE_PLACEHOLDER,
 -                                                                "Parameters Eval");
 +                                                                DEG_OPCODE_PARAMETERS_EVAL);
                                add_relation(group_parameters_key, parameters_key, "Group Node");
                        }
                }
@@@ -1750,10 -1672,12 +1754,10 @@@ void DepsgraphRelationBuilder::build_ma
                build_nodetree(ma->nodetree);
                OperationKey ntree_key(&ma->nodetree->id,
                                       DEG_NODE_TYPE_PARAMETERS,
 -                                     DEG_OPCODE_PLACEHOLDER,
 -                                     "Parameters Eval");
 +                                     DEG_OPCODE_PARAMETERS_EVAL);
                OperationKey material_key(&ma->id,
                                          DEG_NODE_TYPE_SHADING,
 -                                        DEG_OPCODE_PLACEHOLDER,
 -                                        "Material Update");
 +                                        DEG_OPCODE_MATERIAL_UPDATE);
                add_relation(ntree_key, material_key, "Material's NTree");
        }
  }
@@@ -1827,83 -1751,4 +1831,83 @@@ void DepsgraphRelationBuilder::build_mo
        build_animdata(&clip->id);
  }
  
 +void DepsgraphRelationBuilder::build_lightprobe(Object *object)
 +{
 +      LightProbe *probe = (LightProbe *)object->data;
 +      ID *probe_id = &probe->id;
 +      if (probe_id->tag & LIB_TAG_DOIT) {
 +              return;
 +      }
 +      probe_id->tag |= LIB_TAG_DOIT;
 +      build_animdata(&probe->id);
 +
 +      OperationKey probe_key(probe_id,
 +                             DEG_NODE_TYPE_PARAMETERS,
 +                             DEG_OPCODE_PLACEHOLDER,
 +                             "LightProbe Eval");
 +      OperationKey object_key(&object->id,
 +                              DEG_NODE_TYPE_PARAMETERS,
 +                              DEG_OPCODE_PLACEHOLDER,
 +                              "LightProbe Eval");
 +      add_relation(probe_key, object_key, "LightProbe Update");
 +}
 +
 +void DepsgraphRelationBuilder::build_copy_on_write_relations()
 +{
 +      GHASH_FOREACH_BEGIN(IDDepsNode *, id_node, m_graph->id_hash)
 +      {
 +              build_copy_on_write_relations(id_node);
 +      }
 +      GHASH_FOREACH_END();
 +}
 +
 +void DepsgraphRelationBuilder::build_copy_on_write_relations(IDDepsNode *id_node)
 +{
 +      ID *id_orig = id_node->id_orig;
 +
 +      TimeSourceKey time_source_key;
 +      OperationKey copy_on_write_key(id_orig,
 +                                     DEG_NODE_TYPE_COPY_ON_WRITE,
 +                                     DEG_OPCODE_COPY_ON_WRITE);
 +      /* XXX: This is a quick hack to make Alt-A to work. */
 +      add_relation(time_source_key, copy_on_write_key, "Fluxgate capacitor hack");
 +      /* Resat of code is using rather low level trickery, so need to get some
 +       * explicit pointers.
 +       */
 +      DepsNode *node_cow = find_node(copy_on_write_key);
 +      OperationDepsNode *op_cow = node_cow->get_exit_operation();
 +      /* Plug any other components to this one. */
 +      GHASH_FOREACH_BEGIN(ComponentDepsNode *, comp_node, id_node->components)
 +      {
 +              if (comp_node->type == DEG_NODE_TYPE_COPY_ON_WRITE) {
 +                      /* Copy-on-write component never depends on itself. */
 +                      continue;
 +              }
 +              /* All entry operations of each component should wait for a proper
 +               * copy of ID.
 +               */
 +              OperationDepsNode *op_entry = comp_node->get_entry_operation();
 +              if (op_entry != NULL) {
 +                      m_graph->add_new_relation(op_cow, op_entry, "CoW Dependency");
 +              }
 +              /* All dangling operations should also be executed after copy-on-write. */
 +              GHASH_FOREACH_BEGIN(OperationDepsNode *, op_node, comp_node->operations_map)
 +              {
 +                      if (op_node->inlinks.size() == 0) {
 +                              m_graph->add_new_relation(op_cow, op_node, "CoW Dependency");
 +                      }
 +              }
 +              GHASH_FOREACH_END();
 +              /* NOTE: We currently ignore implicit relations to an external
 +               * datablocks for copy-on-write operations. This means, for example,
 +               * copy-on-write component of Object will not wait for copy-on-write
 +               * component of it's Mesh. This is because pointers are all known
 +               * already so remapping will happen all correct. And then If some object
 +               * evaluation step needs geometry, it will have transitive dependency
 +               * to Mesh copy-on-write already.
 +               */
 +      }
 +      GHASH_FOREACH_END();
 +}
 +
  }  // namespace DEG
index be2630384ea838f835708267f0d58d72aab10324,e5033affe2f6c11b345c462e2db34d0d7f7f226f..405c8e8733960a7dcfe5a97aae44ce7b69975542
@@@ -66,9 -66,7 +66,9 @@@ static GHash *_depsnode_typeinfo_regist
  void deg_register_node_typeinfo(DepsNodeFactory *factory)
  {
        BLI_assert(factory != NULL);
 -      BLI_ghash_insert(_depsnode_typeinfo_registry, SET_INT_IN_POINTER(factory->type()), factory);
 +      BLI_ghash_insert(_depsnode_typeinfo_registry,
 +                       SET_INT_IN_POINTER(factory->type()),
 +                       factory);
  }
  
  /* Getters ------------------------------------------------- */
@@@ -77,8 -75,7 +77,8 @@@
  DepsNodeFactory *deg_get_node_factory(const eDepsNode_Type type)
  {
        /* look up type - at worst, it doesn't exist in table yet, and we fail */
 -      return (DepsNodeFactory *)BLI_ghash_lookup(_depsnode_typeinfo_registry, SET_INT_IN_POINTER(type));
 +      return (DepsNodeFactory *)BLI_ghash_lookup(_depsnode_typeinfo_registry,
 +                                                 SET_INT_IN_POINTER(type));
  }
  
  /* Get typeinfo for provided node */
@@@ -98,51 -95,31 +98,52 @@@ static const char *stringify_opcode(eDe
  {
        switch (opcode) {
  #define STRINGIFY_OPCODE(name) case DEG_OPCODE_##name: return #name
 +              /* Generic Operations. */
                STRINGIFY_OPCODE(OPERATION);
 +              STRINGIFY_OPCODE(PARAMETERS_EVAL);
                STRINGIFY_OPCODE(PLACEHOLDER);
 +              /* Animation, Drivers, etc. */
                STRINGIFY_OPCODE(ANIMATION);
                STRINGIFY_OPCODE(DRIVER);
 +              /* Transform. */
                STRINGIFY_OPCODE(TRANSFORM_LOCAL);
                STRINGIFY_OPCODE(TRANSFORM_PARENT);
                STRINGIFY_OPCODE(TRANSFORM_CONSTRAINTS);
 +              STRINGIFY_OPCODE(TRANSFORM_FINAL);
 +              STRINGIFY_OPCODE(TRANSFORM_OBJECT_UBEREVAL);
 +              /* Rigid body. */
                STRINGIFY_OPCODE(RIGIDBODY_REBUILD);
                STRINGIFY_OPCODE(RIGIDBODY_SIM);
 -              STRINGIFY_OPCODE(TRANSFORM_RIGIDBODY);
 -              STRINGIFY_OPCODE(TRANSFORM_FINAL);
 -              STRINGIFY_OPCODE(OBJECT_UBEREVAL);
 +              STRINGIFY_OPCODE(RIGIDBODY_TRANSFORM_COPY);
 +              /* Geometry. */
                STRINGIFY_OPCODE(GEOMETRY_UBEREVAL);
                STRINGIFY_OPCODE(GEOMETRY_PATH);
 +              /* Pose. */
                STRINGIFY_OPCODE(POSE_INIT);
                STRINGIFY_OPCODE(POSE_DONE);
                STRINGIFY_OPCODE(POSE_IK_SOLVER);
                STRINGIFY_OPCODE(POSE_SPLINE_IK_SOLVER);
 +              /* Bone. */
                STRINGIFY_OPCODE(BONE_LOCAL);
                STRINGIFY_OPCODE(BONE_POSE_PARENT);
                STRINGIFY_OPCODE(BONE_CONSTRAINTS);
                STRINGIFY_OPCODE(BONE_READY);
                STRINGIFY_OPCODE(BONE_DONE);
 -              STRINGIFY_OPCODE(PSYS_EVAL);
 -              STRINGIFY_OPCODE(PSYS_EVAL_INIT);
 +              /* Particles. */
++              STRINGIFY_OPCODE(PARTICLE_SYSTEM_EVAL_INIT);
 +              STRINGIFY_OPCODE(PARTICLE_SYSTEM_EVAL);
 +              STRINGIFY_OPCODE(PARTICLE_SETTINGS_EVAL);
 +              STRINGIFY_OPCODE(PARTICLE_SETTINGS_RECALC_CLEAR);
 +              /* Collections. */
 +              STRINGIFY_OPCODE(SCENE_LAYER_INIT);
 +              STRINGIFY_OPCODE(SCENE_LAYER_EVAL);
 +              STRINGIFY_OPCODE(SCENE_LAYER_DONE);
 +              /* Copy on write. */
 +              STRINGIFY_OPCODE(COPY_ON_WRITE);
 +              /* Shading. */
 +              STRINGIFY_OPCODE(SHADING);
 +              STRINGIFY_OPCODE(MATERIAL_UPDATE);
 +              STRINGIFY_OPCODE(WORLD_UPDATE);
  
                case DEG_NUM_OPCODES: return "SpecialCase";
  #undef STRINGIFY_OPCODE
index af18abdb6845838ad070c9d0d4483c7592b56f25,f05f82caa3d45bd610aa437c0e455c216071db00..ba776d5ebd51ba0d7c42ad6ed6cf71ec05c454af
@@@ -103,7 -103,10 +103,7 @@@ typedef enum eDepsNode_Type 
        DEG_NODE_TYPE_PARAMETERS,
        /* Generic "Proxy-Inherit" Component. */
        DEG_NODE_TYPE_PROXY,
 -      /* Animation Component
 -       *
 -       * XXX: merge in with parameters?
 -       */
 +      /* Animation Component */
        DEG_NODE_TYPE_ANIMATION,
        /* Transform Component (Parenting/Constraints) */
        DEG_NODE_TYPE_TRANSFORM,
        DEG_NODE_TYPE_GEOMETRY,
        /* Sequencer Component (Scene Only) */
        DEG_NODE_TYPE_SEQUENCER,
 +      /* Component which contains all operations needed for layer collections
 +       * evaluation.
 +       */
 +      DEG_NODE_TYPE_LAYER_COLLECTIONS,
 +      /* Entry component of majority of ID nodes: prepares CoW pointers for
 +       * execution.
 +       */
 +      DEG_NODE_TYPE_COPY_ON_WRITE,
  
        /* **** Evaluation-Related Outer Types (with Subdata) **** */
  
  
  /* Identifiers for common operations (as an enum). */
  typedef enum eDepsOperation_Code {
 -      /* Generic Operations ------------------------------ */
 +      /* Generic Operations. ------------------------------ */
  
        /* Placeholder for operations which don't need special mention */
        DEG_OPCODE_OPERATION = 0,
  
 +      /* Generic parameters evaluation. */
 +      DEG_OPCODE_PARAMETERS_EVAL,
 +
        // XXX: Placeholder while porting depsgraph code
        DEG_OPCODE_PLACEHOLDER,
  
        /* Animation, Drivers, etc. ------------------------ */
 -
        /* NLA + Action */
        DEG_OPCODE_ANIMATION,
 -
        /* Driver */
        DEG_OPCODE_DRIVER,
  
 -      /* Transform --------------------------------------- */
 -
 +      /* Transform. -------------------------------------- */
        /* Transform entry point - local transforms only */
        DEG_OPCODE_TRANSFORM_LOCAL,
 -
        /* Parenting */
        DEG_OPCODE_TRANSFORM_PARENT,
 -
        /* Constraints */
        DEG_OPCODE_TRANSFORM_CONSTRAINTS,
 +      /* Transform exit point */
 +      DEG_OPCODE_TRANSFORM_FINAL,
 +      /* Handle object-level updates, mainly proxies hacks and recalc flags.  */
 +      DEG_OPCODE_TRANSFORM_OBJECT_UBEREVAL,
  
 -      /* Rigidbody Sim - Perform Sim */
 +      /* Rigid body. -------------------------------------- */
 +      /* Perform Simulation */
        DEG_OPCODE_RIGIDBODY_REBUILD,
        DEG_OPCODE_RIGIDBODY_SIM,
 +      /* Copy results to object */
 +      DEG_OPCODE_RIGIDBODY_TRANSFORM_COPY,
  
 -      /* Rigidbody Sim - Copy Results to Object */
 -      DEG_OPCODE_TRANSFORM_RIGIDBODY,
 -
 -      /* Transform exitpoint */
 -      DEG_OPCODE_TRANSFORM_FINAL,
 -
 -      /* XXX: ubereval is for temporary porting purposes only */
 -      DEG_OPCODE_OBJECT_UBEREVAL,
 -
 -      /* Geometry ---------------------------------------- */
 -
 -      /* XXX: Placeholder - UberEval */
 +      /* Geometry. ---------------------------------------- */
 +      /* Evaluate the whole geometry, including modifiers. */
        DEG_OPCODE_GEOMETRY_UBEREVAL,
 -
        /* Curve Objects - Path Calculation (used for path-following tools, */
        DEG_OPCODE_GEOMETRY_PATH,
  
 -      /* Pose -------------------------------------------- */
 -
 +      /* Pose. -------------------------------------------- */
        /* Init IK Trees, etc. */
        DEG_OPCODE_POSE_INIT,
 -
        /* Free IK Trees + Compute Deform Matrices */
        DEG_OPCODE_POSE_DONE,
 -
        /* IK/Spline Solvers */
        DEG_OPCODE_POSE_IK_SOLVER,
        DEG_OPCODE_POSE_SPLINE_IK_SOLVER,
  
 -      /* Bone -------------------------------------------- */
 -
 -      /* Bone local transforms - Entrypoint */
 +      /* Bone. -------------------------------------------- */
 +      /* Bone local transforms - entry point */
        DEG_OPCODE_BONE_LOCAL,
 -
        /* Pose-space conversion (includes parent + restpose, */
        DEG_OPCODE_BONE_POSE_PARENT,
 -
        /* Constraints */
        DEG_OPCODE_BONE_CONSTRAINTS,
 -
        /* Bone transforms are ready
         *
         * - "READY"  This (internal, noop is used to signal that all pre-IK
        DEG_OPCODE_BONE_READY,
        DEG_OPCODE_BONE_DONE,
  
 -      /* Particles --------------------------------------- */
 -
 -      /* XXX: placeholder - Particle System eval */
 -      DEG_OPCODE_PSYS_EVAL_INIT,
 -      DEG_OPCODE_PSYS_EVAL,
 +      /* Particles. --------------------------------------- */
 +      /* Particle System evaluation. */
++      DEG_OPCODE_PARTICLE_SYSTEM_EVAL_INIT,
 +      DEG_OPCODE_PARTICLE_SYSTEM_EVAL,
 +      DEG_OPCODE_PARTICLE_SETTINGS_EVAL,
 +      DEG_OPCODE_PARTICLE_SETTINGS_RECALC_CLEAR,
 +
 +      /* Collections. ------------------------------------- */
 +      DEG_OPCODE_SCENE_LAYER_INIT,
 +      DEG_OPCODE_SCENE_LAYER_EVAL,
 +      DEG_OPCODE_SCENE_LAYER_DONE,
 +
 +      /* Copy on Write. ------------------------------------ */
 +      DEG_OPCODE_COPY_ON_WRITE,
 +
 +      /* Shading. ------------------------------------------- */
 +      DEG_OPCODE_SHADING,
 +      DEG_OPCODE_MATERIAL_UPDATE,
 +      DEG_OPCODE_WORLD_UPDATE,
  
        DEG_NUM_OPCODES,
  } eDepsOperation_Code;
index 42386c7ecac7ece18d4d86d10a535b4d444d83c7,7b4fac08a7b03fbd534164ab4cc763b0f914a1c8..3a7935bf6e07808c9349da14f78415f1a5263d26
@@@ -255,7 -255,6 +255,7 @@@ typedef enum 
        UI_BTYPE_CHECKBOX               = (13 << 9),  /* similar to toggle, display a 'tick' */
        UI_BTYPE_CHECKBOX_N             = (14 << 9),
        UI_BTYPE_COLOR                  = (15 << 9),
 +      UI_BTYPE_TAB                    = (16 << 9),
        UI_BTYPE_SCROLL                 = (18 << 9),
        UI_BTYPE_BLOCK                  = (19 << 9),
        UI_BTYPE_LABEL                  = (20 << 9),
   * Functions to draw various shapes, taking theme settings into account.
   * Used for code that draws its own UI style elements. */
  
 -void UI_draw_roundbox(float minx, float miny, float maxx, float maxy, float rad);
  void UI_draw_roundbox_corner_set(int type);
 +void UI_draw_roundbox_aa(bool filled, float minx, float miny, float maxx, float maxy, float rad, const float color[4]);
 +void UI_draw_roundbox_4fv(bool filled, float minx, float miny, float maxx, float maxy, float rad, const float col[4]);
 +void UI_draw_roundbox_3ubAlpha(bool filled, float minx, float miny, float maxx, float maxy, float rad, const unsigned char col[3], unsigned char alpha);
 +void UI_draw_roundbox_3fvAlpha(bool filled, float minx, float miny, float maxx, float maxy, float rad, const float col[3], float alpha);
 +void UI_draw_roundbox_shade_x(bool filled, float minx, float miny, float maxx, float maxy, float rad, float shadetop, float shadedown, const float col[4]);
 +
 +#if 0 /* unused */
  int  UI_draw_roundbox_corner_get(void);
 -void UI_draw_roundbox_unfilled(float minx, float miny, float maxx, float maxy, float rad);
 +void UI_draw_roundbox_shade_y(bool filled, float minx, float miny, float maxx, float maxy, float rad, float shadeleft, float shaderight, const float col[4]);
 +#endif
 +
  void UI_draw_box_shadow(unsigned char alpha, float minx, float miny, float maxx, float maxy);
 -void UI_draw_roundbox_gl_mode(int mode, float minx, float miny, float maxx, float maxy, float rad);
 -void UI_draw_roundbox_shade_x(int mode, float minx, float miny, float maxx, float maxy, float rad, float shadetop, float shadedown);
 -void UI_draw_roundbox_shade_y(int mode, float minx, float miny, float maxx, float maxy, float rad, float shadeLeft, float shadeRight);
 -void UI_draw_text_underline(int pos_x, int pos_y, int len, int height);
 +void UI_draw_text_underline(int pos_x, int pos_y, int len, int height, const float color[4]);
  
 -void UI_draw_safe_areas(
 -        float x1, float x2, float y1, float y2,
 +void UI_draw_safe_areas(uint pos, float x1, float x2, float y1, float y2,
          const float title_aspect[2], const float action_aspect[2]);
  
  /* state for scrolldrawing */
@@@ -791,6 -786,7 +791,6 @@@ void UI_popup_handlers_remove_all(struc
  void UI_init(void);
  void UI_init_userdef(void);
  void UI_reinit_font(void);
 -void UI_reinit_gl_state(void);
  void UI_exit(void);
  
  /* Layout
@@@ -855,6 -851,9 +855,6 @@@ enum 
        UI_CNR_ALL          = (UI_CNR_TOP_LEFT | UI_CNR_TOP_RIGHT | UI_CNR_BOTTOM_RIGHT | UI_CNR_BOTTOM_LEFT)
  };
  
 -/* not apart of the corner flags but mixed in some functions  */
 -#define UI_RB_ALPHA (UI_CNR_ALL + 1)
 -
  uiLayout *UI_block_layout(uiBlock *block, int dir, int type, int x, int y, int size, int em, int padding, struct uiStyle *style);
  void UI_block_layout_set_current(uiBlock *block, uiLayout *layout);
  void UI_block_layout_resolve(uiBlock *block, int *x, int *y);
@@@ -912,17 -911,6 +912,17 @@@ void uiTemplateIDPreview(uiLayout *layo
                           const char *newop, const char *openop, const char *unlinkop, int rows, int cols);
  void uiTemplateAnyID(uiLayout *layout, struct PointerRNA *ptr, const char *propname, 
                       const char *proptypename, const char *text);
 +void uiTemplateSearch(
 +        uiLayout *layout, struct bContext *C,
 +        struct PointerRNA *ptr, const char *propname,
 +        struct PointerRNA *searchptr, const char *searchpropname,
 +        const char *newop, const char *unlinkop);
 +void uiTemplateSearchPreview(
 +        uiLayout *layout, struct bContext *C,
 +        struct PointerRNA *ptr, const char *propname,
 +        struct PointerRNA *searchptr, const char *searchpropname,
 +        const char *newop, const char *unlinkop,
 +        const int rows, const int cols);
  void uiTemplatePathBuilder(uiLayout *layout, struct PointerRNA *ptr, const char *propname, 
                             struct PointerRNA *root_ptr, const char *text);
  uiLayout *uiTemplateModifier(uiLayout *layout, struct bContext *C, struct PointerRNA *ptr);
@@@ -956,11 -944,6 +956,11 @@@ void uiTemplateHeader3D(uiLayout *layou
  void uiTemplateEditModeSelection(uiLayout *layout, struct bContext *C);
  void uiTemplateReportsBanner(uiLayout *layout, struct bContext *C);
  void uiTemplateKeymapItemProperties(uiLayout *layout, struct PointerRNA *ptr);
 +void uiTemplateOverrideProperty(
 +        uiLayout *layout, struct PointerRNA *collection_props_ptr, struct PointerRNA *scene_props_ptr,
 +        const char *propname,
 +        const char *name, const char *text_ctxt, int translate, int icon,
 +        const char *custom_template);
  void uiTemplateComponentMenu(uiLayout *layout, struct PointerRNA *ptr, const char *propname, const char *name);
  void uiTemplateNodeSocket(uiLayout *layout, struct bContext *C, float *color);
  void uiTemplateCacheFile(uiLayout *layout, struct bContext *C, struct PointerRNA *ptr, const char *propname);
@@@ -1058,22 -1041,20 +1058,22 @@@ void UI_context_active_but_prop_get_tem
  
  /* Styled text draw */
  void UI_fontstyle_set(const struct uiFontStyle *fs);
 -void UI_fontstyle_draw_ex(
 -        const struct uiFontStyle *fs, const struct rcti *rect, const char *str,
 -        size_t len, float *r_xofs, float *r_yofs);
 -void UI_fontstyle_draw(const struct uiFontStyle *fs, const struct rcti *rect, const char *str);
 -void UI_fontstyle_draw_rotated(const struct uiFontStyle *fs, const struct rcti *rect, const char *str);
 -void UI_fontstyle_draw_simple(const struct uiFontStyle *fs, float x, float y, const char *str);
 +void UI_fontstyle_draw_ex(const struct uiFontStyle *fs, const struct rcti *rect, const char *str,
 +                          const unsigned char col[4], size_t len, float *r_xofs, float *r_yofs);
 +void UI_fontstyle_draw(const struct uiFontStyle *fs, const struct rcti *rect, const char *str,
 +                       const unsigned char col[4]);
 +void UI_fontstyle_draw_rotated(const struct uiFontStyle *fs, const struct rcti *rect, const char *str,
 +                               const unsigned char col[4]);
 +void UI_fontstyle_draw_simple(const struct uiFontStyle *fs, float x, float y, const char *str,
 +                              const unsigned char col[4]);
  void UI_fontstyle_draw_simple_backdrop(
          const struct uiFontStyle *fs, float x, float y, const char *str,
 -        const unsigned char fg[4], const unsigned char bg[4]);
 +        const float col_fg[4], const float col_bg[4]);
  
  int UI_fontstyle_string_width(const struct uiFontStyle *fs, const char *str);
  int UI_fontstyle_height_max(const struct uiFontStyle *fs);
  
 -void UI_draw_icon_tri(float x, float y, char dir);
 +void UI_draw_icon_tri(float x, float y, char dir, const float[4]);
  
  struct uiStyle *UI_style_get(void);           /* use for fonts etc */
  struct uiStyle *UI_style_get_dpi(void);       /* DPI scaled settings for drawing */
@@@ -1101,7 -1082,7 +1101,7 @@@ void UI_butstore_unregister(uiButStore 
  
  
  /* Float precision helpers */
- #define UI_PRECISION_FLOAT_MAX 7
+ #define UI_PRECISION_FLOAT_MAX 6
  /* For float buttons the 'step' (or a1), is scaled */
  #define UI_PRECISION_FLOAT_SCALE 0.01f
  
index 869be844b05aa544b0a5a7686358ccf2f5d09415,1927d7280f3c3e66650c456316261481d29c6d3a..f0317087ddced3c8582c9a563f2d03351403e92b
@@@ -215,90 -215,8 +215,90 @@@ int uiDefAutoButsRNA
        return tot;
  }
  
 -/***************************** ID Utilities *******************************/
 +/* *** RNA collection search menu *** */
  
 +typedef struct CollItemSearch {
 +      struct CollItemSearch *next, *prev;
 +      void *data;
 +      char *name;
 +      int index;
 +      int iconid;
 +} CollItemSearch;
 +
 +static int sort_search_items_list(const void *a, const void *b)
 +{
 +      const CollItemSearch *cis1 = a;
 +      const CollItemSearch *cis2 = b;
 +
 +      if (BLI_strcasecmp(cis1->name, cis2->name) > 0)
 +              return 1;
 +      else
 +              return 0;
 +}
 +
 +void ui_rna_collection_search_cb(const struct bContext *C, void *arg, const char *str, uiSearchItems *items)
 +{
 +      uiRNACollectionSearch *data = arg;
 +      char *name;
 +      int i = 0, iconid = 0, flag = RNA_property_flag(data->target_prop);
 +      ListBase *items_list = MEM_callocN(sizeof(ListBase), "items_list");
 +      CollItemSearch *cis;
 +      const bool skip_filter = !(data->but_changed && *data->but_changed);
 +
 +      /* build a temporary list of relevant items first */
 +      RNA_PROP_BEGIN (&data->search_ptr, itemptr, data->search_prop)
 +      {
 +
 +              if (flag & PROP_ID_SELF_CHECK)
 +                      if (itemptr.data == data->target_ptr.id.data)
 +                              continue;
 +
 +              /* use filter */
 +              if (RNA_property_type(data->target_prop) == PROP_POINTER) {
 +                      if (RNA_property_pointer_poll(&data->target_ptr, data->target_prop, &itemptr) == 0)
 +                              continue;
 +              }
 +
 +              name = RNA_struct_name_get_alloc(&itemptr, NULL, 0, NULL); /* could use the string length here */
 +              iconid = 0;
 +              if (itemptr.type && RNA_struct_is_ID(itemptr.type)) {
 +                      iconid = ui_id_icon_get(C, itemptr.data, false);
 +              }
 +
 +              if (name) {
 +                      if (skip_filter || BLI_strcasestr(name, str)) {
 +                              cis = MEM_callocN(sizeof(CollItemSearch), "CollectionItemSearch");
 +                              cis->data = itemptr.data;
 +                              cis->name = MEM_dupallocN(name);
 +                              cis->index = i;
 +                              cis->iconid = iconid;
 +                              BLI_addtail(items_list, cis);
 +                      }
 +                      MEM_freeN(name);
 +              }
 +
 +              i++;
 +      }
 +      RNA_PROP_END;
 +
 +      BLI_listbase_sort(items_list, sort_search_items_list);
 +
 +      /* add search items from temporary list */
 +      for (cis = items_list->first; cis; cis = cis->next) {
 +              if (UI_search_item_add(items, cis->name, cis->data, cis->iconid) == false) {
 +                      break;
 +              }
 +      }
 +
 +      for (cis = items_list->first; cis; cis = cis->next) {
 +              MEM_freeN(cis->name);
 +      }
 +      BLI_freelistN(items_list);
 +      MEM_freeN(items_list);
 +}
 +
 +
 +/***************************** ID Utilities *******************************/ 
  int UI_icon_from_id(ID *id)
  {
        Object *ob;
@@@ -347,7 -265,7 +347,7 @@@ int UI_icon_from_report_type(int type
   */
  int UI_calc_float_precision(int prec, double value)
  {
-       static const double pow10_neg[UI_PRECISION_FLOAT_MAX + 1] = {1e0, 1e-1, 1e-2, 1e-3, 1e-4, 1e-5, 1e-6, 1e-7};
+       static const double pow10_neg[UI_PRECISION_FLOAT_MAX + 1] = {1e0, 1e-1, 1e-2, 1e-3, 1e-4, 1e-5, 1e-6};
        static const double max_pow = 10000000.0;  /* pow(10, UI_PRECISION_FLOAT_MAX) */
  
        BLI_assert(prec <= UI_PRECISION_FLOAT_MAX);
index 491dddb066a2fa0e8017533824df542369b25d11,4ed1e85fb48e2403d0042c6f6a5d943b9499ab95..c824537a21adfa510c813b0286d9c661be5bf17d
@@@ -45,7 -45,6 +45,7 @@@
  #include "DNA_object_fluidsim.h"
  #include "DNA_object_force.h"
  #include "DNA_object_types.h"
 +#include "DNA_lightprobe_types.h"
  #include "DNA_scene_types.h"
  #include "DNA_vfont_types.h"
  #include "DNA_actuator_types.h"
  #include "BKE_animsys.h"
  #include "BKE_armature.h"
  #include "BKE_camera.h"
 +#include "BKE_collection.h"
  #include "BKE_context.h"
  #include "BKE_constraint.h"
  #include "BKE_curve.h"
 -#include "BKE_depsgraph.h"
  #include "BKE_DerivedMesh.h"
  #include "BKE_displist.h"
  #include "BKE_effect.h"
@@@ -75,7 -74,6 +75,7 @@@
  #include "BKE_group.h"
  #include "BKE_lamp.h"
  #include "BKE_lattice.h"
 +#include "BKE_layer.h"
  #include "BKE_library.h"
  #include "BKE_library_query.h"
  #include "BKE_library_remap.h"
@@@ -94,9 -92,6 +94,9 @@@
  #include "BKE_speaker.h"
  #include "BKE_texture.h"
  
 +#include "DEG_depsgraph.h"
 +#include "DEG_depsgraph_build.h"
 +
  #include "RNA_access.h"
  #include "RNA_define.h"
  #include "RNA_enum_types.h"
  
  #include "UI_resources.h"
  
 -#include "GPU_material.h"
 +#include "GPU_lamp.h"
  
  #include "object_intern.h"
  
@@@ -152,16 -147,6 +152,16 @@@ static EnumPropertyItem field_type_item
        {0, NULL, 0, NULL, NULL}
  };
  
 +static EnumPropertyItem lightprobe_type_items[] = {
 +      {LIGHTPROBE_TYPE_CUBE, "SPHERE", ICON_MESH_UVSPHERE, "Reflection Cubemap",
 +     "Reflection probe with spherical or cubic attenuation"},
 +      {LIGHTPROBE_TYPE_PLANAR, "PLANAR", ICON_MESH_PLANE, "Reflection Plane",
 +     "Planar reflection probe"},
 +      {LIGHTPROBE_TYPE_GRID, "GRID", ICON_MESH_GRID, "Irradiance Volume",
 +     "Irradiance probe to capture diffuse indirect lighting"},
 +      {0, NULL, 0, NULL, NULL}
 +};
 +
  /************************** Exported *****************************/
  
  void ED_object_location_from_view(bContext *C, float loc[3])
@@@ -421,11 -406,10 +421,11 @@@ Object *ED_object_add_type
          bContext *C,
          int type, const char *name,
          const float loc[3], const float rot[3],
 -        bool enter_editmode, unsigned int layer)
 +        bool enter_editmode, unsigned int UNUSED(layer))
  {
        Main *bmain = CTX_data_main(C);
        Scene *scene = CTX_data_scene(C);
 +      SceneLayer *sl = CTX_data_scene_layer(C);
        Object *ob;
  
        /* for as long scene has editmode... */
                ED_object_editmode_exit(C, EM_FREEDATA | EM_FREEUNDO | EM_WAITCURSOR | EM_DO_UNDO);  /* freedata, and undo */
  
        /* deselects all, sets scene->basact */
 -      ob = BKE_object_add(bmain, scene, type, name);
 -      BASACT->lay = ob->lay = layer;
 +      ob = BKE_object_add(bmain, scene, sl, type, name);
        /* editor level activate, notifiers */
 -      ED_base_object_activate(C, BASACT);
 +      ED_object_base_activate(C, sl->basact);
  
        /* more editor stuff */
 -      ED_object_base_init_transform(C, BASACT, loc, rot);
 +      ED_object_base_init_transform(C, sl->basact, loc, rot);
  
        /* Ignore collisions by default for non-mesh objects */
        if (type != OB_MESH) {
                ob->gameflag &= ~(OB_SENSOR | OB_RIGID_BODY | OB_SOFT_BODY | OB_COLLISION | OB_CHARACTER | OB_OCCLUDER | OB_DYNAMIC | OB_NAVMESH); /* copied from rna_object.c */
        }
  
 -      DAG_id_type_tag(bmain, ID_OB);
 -      DAG_relations_tag_update(bmain);
 +      DEG_id_type_tag(bmain, ID_OB);
 +      DEG_relations_tag_update(bmain);
        if (ob->data) {
                ED_render_id_flush_update(bmain, ob->data);
        }
  
        WM_event_add_notifier(C, NC_SCENE | ND_LAYER_CONTENT, scene);
  
 +      /* TODO(sergey): Use proper flag for tagging here. */
 +      DEG_id_tag_update(&scene->id, 0);
 +
        return ob;
  }
  
@@@ -511,79 -493,6 +511,79 @@@ void OBJECT_OT_add(wmOperatorType *ot
        ED_object_add_generic_props(ot, true);
  }
  
 +/********************** Add Probe Operator **********************/
 +
 +/* for object add operator */
 +static int lightprobe_add_exec(bContext *C, wmOperator *op)
 +{
 +      Object *ob;
 +      LightProbe *probe;
 +      int type;
 +      bool enter_editmode;
 +      unsigned int layer;
 +      float loc[3], rot[3];
 +      float radius;
 +
 +      WM_operator_view3d_unit_defaults(C, op);
 +      if (!ED_object_add_generic_get_opts(C, op, 'Z', loc, rot, &enter_editmode, &layer, NULL))
 +              return OPERATOR_CANCELLED;
 +
 +      type = RNA_enum_get(op->ptr, "type");
 +      radius = RNA_float_get(op->ptr, "radius");
 +
 +      const char *name = CTX_DATA_(BLT_I18NCONTEXT_ID_OBJECT, "Light Probe");
 +      ob = ED_object_add_type(C, OB_LIGHTPROBE, name, loc, rot, false, layer);
 +      BKE_object_obdata_size_init(ob, radius);
 +
 +      probe = (LightProbe *)ob->data;
 +      probe->type = type;
 +
 +      switch (type) {
 +              case LIGHTPROBE_TYPE_GRID:
 +                      probe->distinf = 0.3f;
 +                      probe->falloff = 1.0f;
 +                      probe->clipsta = 0.01f;
 +                      break;
 +              case LIGHTPROBE_TYPE_PLANAR:
 +                      probe->distinf = 0.1f;
 +                      probe->falloff = 0.5f;
 +                      probe->clipsta = 0.001f;
 +                      ob->empty_drawsize = 0.5f;
 +                      break;
 +              case LIGHTPROBE_TYPE_CUBE:
 +                      probe->attenuation_type = LIGHTPROBE_SHAPE_ELIPSOID;
 +                      break;
 +              default:
 +                      BLI_assert(!"Lightprobe type not configured.");
 +                      break;
 +      }
 +
 +      DEG_relations_tag_update(CTX_data_main(C));
 +
 +      return OPERATOR_FINISHED;
 +}
 +
 +void OBJECT_OT_lightprobe_add(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name = "Add Light Probe";
 +      ot->description = "Add a light probe object";
 +      ot->idname = "OBJECT_OT_lightprobe_add";
 +
 +      /* api callbacks */
 +      ot->exec = lightprobe_add_exec;
 +      ot->poll = ED_operator_objectmode;
 +
 +      /* flags */
 +      ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
 +
 +      /* properties */
 +      ot->prop = RNA_def_enum(ot->srna, "type", lightprobe_type_items, 0, "Type", "");
 +
 +      ED_object_add_unit_props(ot);
 +      ED_object_add_generic_props(ot, true);
 +}
 +
  /********************* Add Effector Operator ********************/
  
  /* for object add operator */
@@@ -627,7 -536,7 +627,7 @@@ static int effector_add_exec(bContext *
  
        ob->pd = object_add_collision_fields(type);
  
 -      DAG_relations_tag_update(CTX_data_main(C));
 +      DEG_relations_tag_update(CTX_data_main(C));
  
        return OPERATOR_FINISHED;
  }
@@@ -732,7 -641,7 +732,7 @@@ static int object_metaball_add_exec(bCo
                newob = true;
        }
        else {
 -              DAG_id_tag_update(&obedit->id, OB_RECALC_DATA);
 +              DEG_id_tag_update(&obedit->id, OB_RECALC_DATA);
        }
  
        ED_object_new_primitive_matrix(C, obedit, loc, rot, mat);
@@@ -836,7 -745,7 +836,7 @@@ static int object_armature_add_exec(bCo
                newob = true;
        }
        else {
 -              DAG_id_tag_update(&obedit->id, OB_RECALC_DATA);
 +              DEG_id_tag_update(&obedit->id, OB_RECALC_DATA);
        }
  
        if (obedit == NULL) {
@@@ -922,7 -831,7 +922,7 @@@ static int empty_drop_named_image_invok
  {
        Scene *scene = CTX_data_scene(C);
  
 -      Base *base = NULL;
 +      BaseLegacy *base = NULL;
        Image *ima = NULL;
        Object *ob = NULL;
  
@@@ -1094,7 -1003,7 +1094,7 @@@ static int group_instance_add_exec(bCon
                id_us_plus(&group->id);
  
                /* works without this except if you try render right after, see: 22027 */
 -              DAG_relations_tag_update(bmain);
 +              DEG_relations_tag_update(bmain);
  
                WM_event_add_notifier(C, NC_SCENE | ND_OB_ACTIVE, scene);
  
@@@ -1201,20 -1110,22 +1201,20 @@@ static void object_delete_check_glsl_up
  
  /* remove base from a specific scene */
  /* note: now unlinks constraints as well */
 -void ED_base_object_free_and_unlink(Main *bmain, Scene *scene, Base *base)
 +void ED_base_object_free_and_unlink(Main *bmain, Scene *scene, Object *ob)
  {
 -      if (BKE_library_ID_is_indirectly_used(bmain, base->object) &&
 -          ID_REAL_USERS(base->object) <= 1 && ID_EXTRA_USERS(base->object) == 0)
 +      if (BKE_library_ID_is_indirectly_used(bmain, ob) &&
 +          ID_REAL_USERS(ob) <= 1 && ID_EXTRA_USERS(ob) == 0)
        {
                /* We cannot delete indirectly used object... */
                printf("WARNING, undeletable object '%s', should have been catched before reaching this function!",
 -                     base->object->id.name + 2);
 +                     ob->id.name + 2);
                return;
        }
  
 -      BKE_scene_base_unlink(scene, base);
 -      object_delete_check_glsl_update(base->object);
 -      BKE_libblock_free_us(bmain, base->object);
 -      MEM_freeN(base);
 -      DAG_id_type_tag(bmain, ID_OB);
 +      object_delete_check_glsl_update(ob);
 +      BKE_collections_object_remove(bmain, scene, ob, true);
 +      DEG_id_type_tag(bmain, ID_OB);
  }
  
  static int object_delete_exec(bContext *C, wmOperator *op)
        if (CTX_data_edit_object(C)) 
                return OPERATOR_CANCELLED;
  
 -      CTX_DATA_BEGIN (C, Base *, base, selected_bases)
 +      CTX_DATA_BEGIN (C, Object *, ob, selected_objects)
        {
 -              const bool is_indirectly_used = BKE_library_ID_is_indirectly_used(bmain, base->object);
 -              if (base->object->id.tag & LIB_TAG_INDIRECT) {
 +              const bool is_indirectly_used = BKE_library_ID_is_indirectly_used(bmain, ob);
 +              if (ob->id.tag & LIB_TAG_INDIRECT) {
                        /* Can this case ever happen? */
 -                      BKE_reportf(op->reports, RPT_WARNING, "Cannot delete indirectly linked object '%s'", base->object->id.name + 2);
 +                      BKE_reportf(op->reports, RPT_WARNING, "Cannot delete indirectly linked object '%s'", ob->id.name + 2);
                        continue;
                }
 -              else if (is_indirectly_used && ID_REAL_USERS(base->object) <= 1 && ID_EXTRA_USERS(base->object) == 0) {
 +              else if (is_indirectly_used && ID_REAL_USERS(ob) <= 1 && ID_EXTRA_USERS(ob) == 0) {
                        BKE_reportf(op->reports, RPT_WARNING,
                                "Cannot delete object '%s' from scene '%s', indirectly used objects need at least one user",
 -                              base->object->id.name + 2, scene->id.name + 2);
 +                              ob->id.name + 2, scene->id.name + 2);
                        continue;
                }
  
                 * custom scene/object/base handling, and use generic lib remap/query for that.
                 * But this is for later (aka 2.8, once layers & co are settled and working).
                 */
 -              if (use_global && base->object->id.lib == NULL) {
 +              if (use_global && ob->id.lib == NULL) {
                        /* We want to nuke the object, let's nuke it the easy way (not for linked data though)... */
 -                      BKE_libblock_delete(bmain, &base->object->id);
 +                      BKE_libblock_delete(bmain, &ob->id);
                        changed = true;
                        continue;
                }
                for (bGPdata *gpd = bmain->gpencil.first; gpd; gpd = gpd->id.next) {
                        for (bGPDlayer *gpl = gpd->layers.first; gpl; gpl = gpl->next) {
                                if (gpl->parent != NULL) {
 -                                      Object *ob = gpl->parent;
 -                                      Object *curob = base->object;
 -                                      if (ob == curob) {
 +                                      if (gpl->parent == ob) {
                                                gpl->parent = NULL;
                                        }
                                }
                        }
                }
  
 -              /* deselect object -- it could be used in other scenes */
 -              base->object->flag &= ~SELECT;
 -
                /* remove from current scene only */
 -              ED_base_object_free_and_unlink(bmain, scene, base);
 +              ED_base_object_free_and_unlink(bmain, scene, ob);
                changed = true;
  
                if (use_global) {
                        Scene *scene_iter;
 -                      Base *base_other;
 -
                        for (scene_iter = bmain->scene.first; scene_iter; scene_iter = scene_iter->id.next) {
                                if (scene_iter != scene && !ID_IS_LINKED_DATABLOCK(scene_iter)) {
 -                                      base_other = BKE_scene_base_find(scene_iter, base->object);
 -                                      if (base_other) {
 -                                              if (is_indirectly_used && ID_REAL_USERS(base->object) <= 1 && ID_EXTRA_USERS(base->object) == 0) {
 -                                                      BKE_reportf(op->reports, RPT_WARNING,
 -                                                                  "Cannot delete object '%s' from scene '%s', indirectly used objects need at least one user",
 -                                                                  base->object->id.name + 2, scene_iter->id.name + 2);
 -                                                      break;
 -                                              }
 -                                              ED_base_object_free_and_unlink(bmain, scene_iter, base_other);
 +                                      if (is_indirectly_used && ID_REAL_USERS(ob) <= 1 && ID_EXTRA_USERS(ob) == 0) {
 +                                              BKE_reportf(op->reports, RPT_WARNING,
 +                                                          "Cannot delete object '%s' from scene '%s', indirectly used objects need at least one user",
 +                                                          ob->id.name + 2, scene_iter->id.name + 2);
 +                                              break;
                                        }
 +                                      ED_base_object_free_and_unlink(bmain, scene_iter, ob);
                                }
                        }
                }
        /* delete has to handle all open scenes */
        BKE_main_id_tag_listbase(&bmain->scene, LIB_TAG_DOIT, true);
        for (win = wm->windows.first; win; win = win->next) {
 -              scene = win->screen->scene;
 -              
 +              scene = WM_window_get_active_scene(win);
 +
                if (scene->id.tag & LIB_TAG_DOIT) {
                        scene->id.tag &= ~LIB_TAG_DOIT;
                        
 -                      DAG_relations_tag_update(bmain);
 +                      DEG_relations_tag_update(bmain);
  
                        WM_event_add_notifier(C, NC_SCENE | ND_OB_ACTIVE, scene);
                        WM_event_add_notifier(C, NC_SCENE | ND_LAYER_CONTENT, scene);
@@@ -1423,7 -1344,6 +1423,7 @@@ static void make_object_duplilist_real(
                                         const bool use_hierarchy)
  {
        Main *bmain = CTX_data_main(C);
 +      SceneLayer *sl = CTX_data_scene_layer(C);
        ListBase *lb;
        DupliObject *dob;
        GHash *dupli_gh = NULL, *parent_gh = NULL;
                 */
                if (ob->mat == NULL) ob->totcol = 0;
  
 -              basen = MEM_dupallocN(base);
 -              basen->flag &= ~(OB_FROMDUPLI | OB_FROMGROUP);
 -              ob->flag = basen->flag;
 -              basen->lay = base->lay;
 -              BLI_addhead(&scene->base, basen);   /* addhead: othwise eternal loop */
 -              basen->object = ob;
 +              BKE_collection_object_add_from(scene, dob->ob, ob);
 +              basen = BKE_scene_layer_base_find(sl, ob);
 +
 +              BKE_scene_object_base_flag_sync_from_base(basen);
  
                /* make sure apply works */
                BKE_animdata_free(&ob->id, true);
                BKE_constraints_free(&ob->constraints);
                ob->curve_cache = NULL;
                ob->transflag &= ~OB_DUPLI;
 -              ob->lay = base->lay;
  
                copy_m4_m4(ob->obmat, dob->mat);
                BKE_object_apply_mat4(ob, ob->obmat, false, false);
                set_sca_new_poins_ob(ob);
                BKE_id_clear_newpoin(&dob->ob->id);
  
 -              DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
 +              DEG_id_tag_update(&ob->id, OB_RECALC_DATA);
        }
  
        if (use_hierarchy) {
                                BKE_object_apply_mat4(ob_dst, dob->mat, false, true);
  
                                /* to set ob_dst->orig and in case theres any other discrepicies */
 -                              DAG_id_tag_update(&ob_dst->id, OB_RECALC_OB);
 +                              DEG_id_tag_update(&ob_dst->id, OB_RECALC_OB);
                        }
                }
        }
  
                        /* similer to the code above, see comments */
                        BKE_object_apply_mat4(ob_dst, dob->mat, false, true);
 -                      DAG_id_tag_update(&ob_dst->id, OB_RECALC_OB);
 +                      DEG_id_tag_update(&ob_dst->id, OB_RECALC_OB);
                }
        }
  
                        if (object->proxy_group == base->object) {
                                object->proxy = NULL;
                                object->proxy_from = NULL;
 -                              DAG_id_tag_update(&object->id, OB_RECALC_OB);
 +                              DEG_id_tag_update(&object->id, OB_RECALC_OB);
                        }
                }
        }
@@@ -1605,7 -1528,7 +1605,7 @@@ static int object_duplicates_make_real_
        }
        CTX_DATA_END;
  
 -      DAG_relations_tag_update(bmain);
 +      DEG_relations_tag_update(bmain);
        WM_event_add_notifier(C, NC_SCENE, scene);
        WM_main_add_notifier(NC_OBJECT | ND_DRAW, NULL);
  
@@@ -1678,7 -1601,7 +1678,7 @@@ static int convert_poll(bContext *C
  }
  
  /* Helper for convert_exec */
 -static Base *duplibase_for_convert(Main *bmain, Scene *scene, Base *base, Object *ob)
 +static Base *duplibase_for_convert(Main *bmain, Scene *scene, SceneLayer *sl, Base *base, Object *ob)
  {
        Object *obn;
        Base *basen;
        }
  
        obn = BKE_object_copy(bmain, ob);
 -      DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
 -
 -      basen = MEM_mallocN(sizeof(Base), "duplibase");
 -      *basen = *base;
 -      BLI_addhead(&scene->base, basen);   /* addhead: otherwise eternal loop */
 -      basen->object = obn;
 -      basen->flag |= SELECT;
 -      obn->flag |= SELECT;
 -      base->flag &= ~SELECT;
 -      ob->flag &= ~SELECT;
 +      DEG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
 +      BKE_collection_object_add_from(scene, ob, obn);
  
 +      basen = BKE_scene_layer_base_find(sl, obn);
 +      ED_object_base_select(basen, BA_SELECT);
 +      ED_object_base_select(basen, BA_DESELECT);
        return basen;
  }
  
@@@ -1701,7 -1629,6 +1701,7 @@@ static int convert_exec(bContext *C, wm
  {
        Main *bmain = CTX_data_main(C);
        Scene *scene = CTX_data_scene(C);
 +      SceneLayer *sl = CTX_data_scene_layer(C);
        Base *basen = NULL, *basact = NULL;
        Object *ob, *ob1, *newob, *obact = CTX_data_active_object(C);
        DerivedMesh *dm;
        /* don't forget multiple users! */
  
        {
 -              Base *base;
 +              BaseLegacy *base;
  
                for (base = scene->base.first; base; base = base->next) {
                        ob = base->object;
         * on other objects data masks too, see: T50950. */
        {
                for (CollectionPointerLink *link = selected_editable_bases.first; link; link = link->next) {
 -                      Base *base = link->ptr.data;
 +                      BaseLegacy *base = link->ptr.data;
                        ob = base->object;
  
                        /* The way object type conversion works currently (enforcing conversion of *all* objetcs using converted
                         * However, changing this is more design than bugfix, not to mention convoluted code below,
                         * so that will be for later.
                         * But at the very least, do not do that with linked IDs! */
-                       if ((ID_IS_LINKED_DATABLOCK(ob) || ID_IS_LINKED_DATABLOCK(ob->data)) && !keep_original) {
+                       if ((ID_IS_LINKED_DATABLOCK(ob) || (ob->data && ID_IS_LINKED_DATABLOCK(ob->data))) && !keep_original) {
                                keep_original = true;
                                BKE_reportf(op->reports, RPT_INFO,
                                            "Converting some linked object/object data, enforcing 'Keep Original' option to True");
                        }
  
 -                      DAG_id_tag_update(&base->object->id, OB_RECALC_DATA);
 +                      DEG_id_tag_update(&base->object->id, OB_RECALC_DATA);
                }
  
                uint64_t customdata_mask_prev = scene->customdata_mask;
        }
  
        for (CollectionPointerLink *link = selected_editable_bases.first; link; link = link->next) {
 -              Base *base = link->ptr.data;
 +              BaseLegacy *base = link->ptr.data;
                ob = base->object;
  
                if (ob->flag & OB_DONE || !IS_TAGGED(ob->data)) {
                        ob->flag |= OB_DONE;
  
                        if (keep_original) {
 -                              basen = duplibase_for_convert(bmain, scene, base, NULL);
 +                              basen = duplibase_for_convert(bmain, scene, sl, base, NULL);
                                newob = basen->object;
  
                                /* decrement original mesh's usage count  */
                        ob->flag |= OB_DONE;
  
                        if (keep_original) {
 -                              basen = duplibase_for_convert(bmain, scene, base, NULL);
 +                              basen = duplibase_for_convert(bmain, scene, sl, base, NULL);
                                newob = basen->object;
  
                                /* decrement original mesh's usage count  */
                        }
                        else {
                                newob = ob;
 -                              DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
 +                              DEG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
                        }
  
                        /* make new mesh data from the original copy */
                        ob->flag |= OB_DONE;
  
                        if (keep_original) {
 -                              basen = duplibase_for_convert(bmain, scene, base, NULL);
 +                              basen = duplibase_for_convert(bmain, scene, sl, base, NULL);
                                newob = basen->object;
  
                                /* decrement original curve's usage count  */
                                        for (ob1 = bmain->object.first; ob1; ob1 = ob1->id.next) {
                                                if (ob1->data == ob->data) {
                                                        ob1->type = OB_CURVE;
 -                                                      DAG_id_tag_update(&ob1->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
 +                                                      DEG_id_tag_update(&ob1->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
                                                }
                                        }
                                }
  
                        if (target == OB_MESH) {
                                if (keep_original) {
 -                                      basen = duplibase_for_convert(bmain, scene, base, NULL);
 +                                      basen = duplibase_for_convert(bmain, scene, sl, base, NULL);
                                        newob = basen->object;
  
                                        /* decrement original curve's usage count  */
                        if (!(baseob->flag & OB_DONE)) {
                                baseob->flag |= OB_DONE;
  
 -                              basen = duplibase_for_convert(bmain, scene, base, baseob);
 +                              basen = duplibase_for_convert(bmain, scene, sl, base, baseob);
                                newob = basen->object;
  
                                mb = newob->data;
                }
  
                if (!keep_original && (ob->flag & OB_DONE)) {
 -                      DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
 +                      DEG_id_tag_update(&ob->id, OB_RECALC_DATA);
                        ((ID *)ob->data)->tag &= ~LIB_TAG_DOIT; /* flag not to convert this datablock again */
                }
        }
  
        if (!keep_original) {
                if (mballConverted) {
 -                      Base *base, *base_next;
 -
 -                      for (base = scene->base.first; base; base = base_next) {
 -                              base_next = base->next;
 -
 -                              ob = base->object;
 +                      FOREACH_SCENE_OBJECT(scene, ob_mball)
 +                      {
                                if (ob->type == OB_MBALL) {
                                        if (ob->flag & OB_DONE) {
                                                Object *ob_basis = NULL;
 -                                              if (BKE_mball_is_basis(ob) ||
 -                                                  ((ob_basis = BKE_mball_basis_find(scene, ob)) && (ob_basis->flag & OB_DONE)))
 +                                              if (BKE_mball_is_basis(ob_mball) ||
 +                                                  ((ob_basis = BKE_mball_basis_find(scene, ob_mball)) && (ob_basis->flag & OB_DONE)))
                                                {
 -                                                      ED_base_object_free_and_unlink(bmain, scene, base);
 +                                                      ED_base_object_free_and_unlink(bmain, scene, ob_mball);
                                                }
                                        }
                                }
                        }
 +                      FOREACH_SCENE_OBJECT_END
                }
  
                /* delete object should renew depsgraph */
 -              DAG_relations_tag_update(bmain);
 +              DEG_relations_tag_update(bmain);
        }
  
  // XXX        ED_object_editmode_enter(C, 0);
  
        if (basact) {
                /* active base was changed */
 -              ED_base_object_activate(C, basact);
 -              BASACT = basact;
 +              ED_object_base_activate(C, basact);
 +              BASACT_NEW = basact;
        }
 -      else if (BASACT->object->flag & OB_DONE) {
 -              WM_event_add_notifier(C, NC_OBJECT | ND_MODIFIER, BASACT->object);
 -              WM_event_add_notifier(C, NC_OBJECT | ND_DATA, BASACT->object);
 +      else if (BASACT_NEW->object->flag & OB_DONE) {
 +              WM_event_add_notifier(C, NC_OBJECT | ND_MODIFIER, BASACT_NEW->object);
 +              WM_event_add_notifier(C, NC_OBJECT | ND_DATA, BASACT_NEW->object);
        }
  
 -      DAG_relations_tag_update(bmain);
 +      DEG_relations_tag_update(bmain);
        WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, scene);
        WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, scene);
  
@@@ -2078,36 -2008,39 +2078,36 @@@ void OBJECT_OT_convert(wmOperatorType *
  /* used below, assumes id.new is correct */
  /* leaves selection of base/object unaltered */
  /* Does set ID->newid pointers. */
 -static Base *object_add_duplicate_internal(Main *bmain, Scene *scene, Base *base, int dupflag)
 +static Base *object_add_duplicate_internal(Main *bmain, Scene *scene, SceneLayer *sl, Object *ob, int dupflag)
  {
  #define ID_NEW_REMAP_US(a)    if (      (a)->id.newid) { (a) = (void *)(a)->id.newid;       (a)->id.us++; }
  #define ID_NEW_REMAP_US2(a)   if (((ID *)a)->newid)    { (a) = ((ID  *)a)->newid;     ((ID *)a)->us++;    }
  
        Base *basen = NULL;
        Material ***matarar;
 -      Object *ob, *obn;
 +      Object *obn;
        ID *id;
        int a, didit;
  
 -      ob = base->object;
        if (ob->mode & OB_MODE_POSE) {
                ; /* nothing? */
        }
        else {
                obn = ID_NEW_SET(ob, BKE_object_copy(bmain, ob));
 -              DAG_id_tag_update(&obn->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
 +              DEG_id_tag_update(&obn->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
  
 -              basen = MEM_mallocN(sizeof(Base), "duplibase");
 -              *basen = *base;
 -              BLI_addhead(&scene->base, basen);   /* addhead: prevent eternal loop */
 -              basen->object = obn;
 +              BKE_collection_object_add_from(scene, ob, obn);
 +              basen = BKE_scene_layer_base_find(sl, obn);
  
                /* 1) duplis should end up in same group as the original
                 * 2) Rigid Body sim participants MUST always be part of a group...
                 */
                // XXX: is 2) really a good measure here?
 -              if ((basen->flag & OB_FROMGROUP) || ob->rigidbody_object || ob->rigidbody_constraint) {
 +              if ((ob->flag & OB_FROMGROUP) != 0 || ob->rigidbody_object || ob->rigidbody_constraint) {
                        Group *group;
                        for (group = bmain->group.first; group; group = group->id.next) {
                                if (BKE_group_object_exists(group, ob))
 -                                      BKE_group_object_add(group, obn, scene, basen);
 +                                      BKE_group_object_add(group, obn);
                        }
                }
  
                                }
                                break;
                        case OB_ARMATURE:
 -                              DAG_id_tag_update(&obn->id, OB_RECALC_DATA);
 +                              DEG_id_tag_update(&obn->id, OB_RECALC_DATA);
                                if (obn->pose)
                                        BKE_pose_tag_recalc(bmain, obn->pose);
                                if (dupflag & USER_DUP_ARM) {
   * note: don't call this within a loop since clear_* funcs loop over the entire database.
   * note: caller must do DAG_relations_tag_update(bmain);
   *       this is not done automatic since we may duplicate many objects in a batch */
 -Base *ED_object_add_duplicate(Main *bmain, Scene *scene, Base *base, int dupflag)
 +Base *ED_object_add_duplicate(Main *bmain, Scene *scene, SceneLayer *sl, Base *base, int dupflag)
  {
        Base *basen;
        Object *ob;
  
        clear_sca_new_poins();  /* BGE logic */
  
 -      basen = object_add_duplicate_internal(bmain, scene, base, dupflag);
 +      basen = object_add_duplicate_internal(bmain, scene, sl, base->object, dupflag);
        if (basen == NULL) {
                return NULL;
        }
@@@ -2349,7 -2282,6 +2349,7 @@@ static int duplicate_exec(bContext *C, 
  {
        Main *bmain = CTX_data_main(C);
        Scene *scene = CTX_data_scene(C);
 +      SceneLayer *sl = CTX_data_scene_layer(C);
        const bool linked = RNA_boolean_get(op->ptr, "linked");
        int dupflag = (linked) ? 0 : U.dupflag;
  
  
        CTX_DATA_BEGIN (C, Base *, base, selected_bases)
        {
 -              Base *basen = object_add_duplicate_internal(bmain, scene, base, dupflag);
 +              Base *basen = object_add_duplicate_internal(bmain, scene, sl, base->object, dupflag);
  
                /* note that this is safe to do with this context iterator,
                 * the list is made in advance */
 -              ED_base_object_select(base, BA_DESELECT);
 +              ED_object_base_select(base, BA_DESELECT);
 +              ED_object_base_select(basen, BA_SELECT);
  
                if (basen == NULL) {
                        continue;
                }
  
                /* new object becomes active */
 -              if (BASACT == base)
 -                      ED_base_object_activate(C, basen);
 +              if (BASACT_NEW == base)
 +                      ED_object_base_activate(C, basen);
  
                if (basen->object->data) {
 -                      DAG_id_tag_update(basen->object->data, 0);
 +                      DEG_id_tag_update(basen->object->data, 0);
                }
        }
        CTX_DATA_END;
  
        BKE_main_id_clear_newpoins(bmain);
  
 -      DAG_relations_tag_update(bmain);
 +      DEG_relations_tag_update(bmain);
 +      /* TODO(sergey): Use proper flag for tagging here. */
 +      DEG_id_tag_update(&CTX_data_scene(C)->id, 0);
  
        WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, scene);
  
@@@ -2420,9 -2349,9 +2420,9 @@@ static int add_named_exec(bContext *C, 
        wmWindow *win = CTX_wm_window(C);
        const wmEvent *event = win ? win->eventstate : NULL;
        Main *bmain = CTX_data_main(C);
 -      View3D *v3d = CTX_wm_view3d(C);  /* may be NULL */
        Scene *scene = CTX_data_scene(C);
 -      Base *basen, *base;
 +      SceneLayer *sl = CTX_data_scene_layer(C);
 +      Base *basen;
        Object *ob;
        const bool linked = RNA_boolean_get(op->ptr, "linked");
        int dupflag = (linked) ? 0 : U.dupflag;
                return OPERATOR_CANCELLED;
        }
  
 -      base = MEM_callocN(sizeof(Base), "duplibase");
 -      base->object = ob;
 -      base->flag = ob->flag;
 -
        /* prepare dupli */
        clear_sca_new_poins();  /* BGE logic */
  
 -      basen = object_add_duplicate_internal(bmain, scene, base, dupflag);
 +      basen = object_add_duplicate_internal(bmain, scene, sl, ob, dupflag);
 +      BKE_scene_object_base_flag_sync_from_object(basen);
  
        if (basen == NULL) {
 -              MEM_freeN(base);
                BKE_report(op->reports, RPT_ERROR, "Object could not be duplicated");
                return OPERATOR_CANCELLED;
        }
  
 -      basen->lay = basen->object->lay = BKE_screen_view3d_layer_active(v3d, scene);
        basen->object->restrictflag &= ~OB_RESTRICT_VIEW;
  
        if (event) {
                ED_view3d_cursor3d_position(C, basen->object->loc, mval);
        }
        
 -      ED_base_object_select(basen, BA_SELECT);
 -      ED_base_object_activate(C, basen);
 +      ED_object_base_select(basen, BA_SELECT);
 +      ED_object_base_activate(C, basen);
  
        copy_object_set_idnew(C);
  
        BKE_main_id_clear_newpoins(bmain);
  
 -      DAG_relations_tag_update(bmain);
 -
 -      MEM_freeN(base);
 +      DEG_relations_tag_update(bmain);
  
        WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT | ND_OB_ACTIVE, scene);
  
index eb6eebf3c548a8949508df77c06d6c36ffb8d83e,09b0847b3064c067939d8ba4f8435caed3b872fc..30830e4e7bc21a3e903b569c6eede5d3a6fb3a22
  #include "BLI_task.h"
  
  #include "BKE_context.h"
 -#include "BKE_depsgraph.h"
  #include "BKE_brush.h"
  #include "BKE_image.h"
  #include "BKE_paint.h"
  #include "BKE_report.h"
  #include "BKE_texture.h"
  
 +#include "DEG_depsgraph.h"
 +
  #include "ED_paint.h"
  #include "ED_screen.h"
  
@@@ -798,6 -797,7 +798,7 @@@ static void paint_2d_ibuf_rgb_set(ImBu
                float map_alpha = (rgb[3] == 0.0f) ? rrgbf[3] : rrgbf[3] / rgb[3];
  
                mul_v3_v3fl(rrgbf, rgb, map_alpha);
+               rrgbf[3] = rgb[3];
        }
        else {
                unsigned char straight[4];
                rrgb[0] = straight[0];
                rrgb[1] = straight[1];
                rrgb[2] = straight[2];
+               rrgb[3] = straight[3];
        }
  }
  
@@@ -996,7 -997,7 +998,7 @@@ static void paint_2d_lift_smear(ImBuf *
                IMB_rectblend(ibufb, ibufb, ibuf, NULL, NULL, NULL, 0, region[a].destx, region[a].desty,
                              region[a].destx, region[a].desty,
                              region[a].srcx, region[a].srcy,
-                             region[a].width, region[a].height, IMB_BLEND_COPY_RGB, false);
+                             region[a].width, region[a].height, IMB_BLEND_COPY, false);
  }
  
  static ImBuf *paint_2d_lift_clone(ImBuf *ibuf, ImBuf *ibufb, int *pos)
@@@ -1097,6 -1098,7 +1099,7 @@@ static int paint_2d_op(void *state, ImB
        /* lift from canvas */
        if (s->tool == PAINT_TOOL_SOFTEN) {
                paint_2d_lift_soften(s, s->canvas, ibufb, bpos, tile);
+               blend = IMB_BLEND_INTERPOLATE;
        }
        else if (s->tool == PAINT_TOOL_SMEAR) {
                if (lastpos[0] == pos[0] && lastpos[1] == pos[1])
  
                paint_2d_convert_brushco(ibufb, lastpos, blastpos);
                paint_2d_lift_smear(s->canvas, ibufb, blastpos, tile);
+               blend = IMB_BLEND_INTERPOLATE;
        }
        else if (s->tool == PAINT_TOOL_CLONE && s->clonecanvas) {
                liftpos[0] = pos[0] - offset[0] * s->canvas->x;
@@@ -1356,7 -1359,7 +1360,7 @@@ void paint_2d_redraw(const bContext *C
  
                /* compositor listener deals with updating */
                WM_event_add_notifier(C, NC_IMAGE | NA_EDITED, s->image);
 -              DAG_id_tag_update(&s->image->id, 0);
 +              DEG_id_tag_update(&s->image->id, 0);
        }
        else {
                if (!s->sima || !s->sima->lock)
index eeb357d43e725b01fd5521475178e70ea7b13712,162658f01bb69a810129a9859a3db4cbdb1517f3..5d3ceb3f7df7865d1d7696a9b505661dc6d2df85
@@@ -64,6 -64,7 +64,6 @@@
  #include "BKE_camera.h"
  #include "BKE_context.h"
  #include "BKE_colortools.h"
 -#include "BKE_depsgraph.h"
  #include "BKE_DerivedMesh.h"
  #include "BKE_idprop.h"
  #include "BKE_brush.h"
@@@ -79,8 -80,6 +79,8 @@@
  #include "BKE_scene.h"
  #include "BKE_texture.h"
  
 +#include "DEG_depsgraph.h"
 +
  #include "UI_interface.h"
  
  #include "ED_mesh.h"
@@@ -3488,7 -3487,7 +3488,7 @@@ static void proj_paint_layer_clone_init
  
        /* use clone mtface? */
        if (ps->do_layer_clone) {
 -              const int layer_num = CustomData_get_clone_layer(&((Mesh *)ps->ob->data)->pdata, CD_MTEXPOLY);
 +              const int layer_num = CustomData_get_clone_layer(&((Mesh *)ps->ob->data)->ldata, CD_MLOOPUV);
  
                ps->dm_mloopuv_clone = MEM_mallocN(ps->dm_totpoly * sizeof(MLoopUV *), "proj_paint_mtfaces");
  
@@@ -3851,7 -3850,7 +3851,7 @@@ static void project_paint_begin
  
        if (ps->do_layer_stencil || ps->do_stencil_brush) {
                //int layer_num = CustomData_get_stencil_layer(&ps->dm->loopData, CD_MLOOPUV);
 -              int layer_num = CustomData_get_stencil_layer(&((Mesh *)ps->ob->data)->pdata, CD_MTEXPOLY);
 +              int layer_num = CustomData_get_stencil_layer(&((Mesh *)ps->ob->data)->ldata, CD_MLOOPUV);
                if (layer_num != -1)
                        ps->dm_mloopuv_stencil = CustomData_get_layer_n(&ps->dm->loopData, CD_MLOOPUV, layer_num);
  
@@@ -3939,7 -3938,7 +3939,7 @@@ static void project_paint_end(ProjPaint
                ProjPaintImage *projIma;
                for (a = 0, projIma = ps->projImages; a < ps->image_tot; a++, projIma++) {
                        BKE_image_release_ibuf(projIma->ima, projIma->ibuf, NULL);
 -                      DAG_id_tag_update(&projIma->ima->id, 0);
 +                      DEG_id_tag_update(&projIma->ima->id, 0);
                }
        }
  
@@@ -4755,6 -4754,9 +4755,9 @@@ static void *do_projectpaint_thread(voi
                                                        copy_v3_v3(texrgb, texrgba);
                                                        mask *= texrgba[3];
                                                }
+                                               else {
+                                                       zero_v3(texrgb);
+                                               }
  
                                                /* extra mask for normal, layer stencil, .. */
                                                mask *= ((float)projPixel->mask) * (1.0f / 65535.0f);
@@@ -5022,7 -5024,6 +5025,7 @@@ void paint_proj_stroke
        /* clone gets special treatment here to avoid going through image initialization */
        if (ps_handle->is_clone_cursor_pick) {
                Scene *scene = ps_handle->scene;
 +              struct Depsgraph *graph = CTX_data_depsgraph(C);
                View3D *v3d = CTX_wm_view3d(C);
                ARegion *ar = CTX_wm_region(C);
                float *cursor = ED_view3d_cursor3d_get(scene, v3d);
  
                view3d_operator_needs_opengl(C);
  
 -              if (!ED_view3d_autodist(scene, ar, v3d, mval_i, cursor, false, NULL))
 +              if (!ED_view3d_autodist(graph, ar, v3d, mval_i, cursor, false, NULL)) {
                        return;
 +              }
  
                ED_region_tag_redraw(ar);
  
@@@ -5210,7 -5210,7 +5213,7 @@@ void *paint_proj_new_stroke(bContext *C
  
                project_state_init(C, ob, ps, mode);
  
 -              if (ps->ob == NULL || !(ps->ob->lay & ps->v3d->lay)) {
 +              if (ps->ob == NULL) {
                        ps_handle->ps_views_tot = i + 1;
                        goto fail;
                }
@@@ -5312,12 -5312,11 +5315,12 @@@ static int texture_paint_camera_project
  {
        Image *image = BLI_findlink(&CTX_data_main(C)->image, RNA_enum_get(op->ptr, "image"));
        Scene *scene = CTX_data_scene(C);
 +      SceneLayer *sl = CTX_data_scene_layer(C);
        ProjPaintState ps = {NULL};
        int orig_brush_size;
        IDProperty *idgroup;
        IDProperty *view_data = NULL;
 -      Object *ob = OBACT;
 +      Object *ob = OBACT_NEW;
        bool uvs, mat, tex;
  
        if (ob == NULL || ob->type != OB_MESH) {
@@@ -5446,7 -5445,6 +5449,7 @@@ static int texture_paint_image_from_vie
        char filename[FILE_MAX];
  
        Scene *scene = CTX_data_scene(C);
 +      SceneLayer *sl = CTX_data_scene_layer(C);
        ToolSettings *settings = scene->toolsettings;
        int w = settings->imapaint.screen_grab_size[0];
        int h = settings->imapaint.screen_grab_size[1];
        if (h > maxsize) h = maxsize;
  
        ibuf = ED_view3d_draw_offscreen_imbuf(
 -              scene, CTX_wm_view3d(C), CTX_wm_region(C),
 +              scene, sl, CTX_wm_view3d(C), CTX_wm_region(C),
                w, h, IB_rect, false, R_ALPHAPREMUL, 0, false, NULL,
                NULL, NULL, err_out);
        if (!ibuf) {
@@@ -5595,7 -5593,7 +5598,7 @@@ bool BKE_paint_proj_mesh_data_check(Sce
        }
        
        me = BKE_mesh_from_object(ob);
 -      layernum = CustomData_number_of_layers(&me->pdata, CD_MTEXPOLY);
 +      layernum = CustomData_number_of_layers(&me->ldata, CD_MLOOPUV);
  
        if (layernum == 0) {
                hasuvs = false;
@@@ -5744,7 -5742,7 +5747,7 @@@ static bool proj_paint_add_slot(bContex
                        BKE_texpaint_slot_refresh_cache(scene, ma);
                        BKE_image_signal(ima, NULL, IMA_SIGNAL_USER_NEW_IMAGE);
                        WM_event_add_notifier(C, NC_IMAGE | NA_ADDED, ima);
 -                      DAG_id_tag_update(&ma->id, 0);
 +                      DEG_id_tag_update(&ma->id, 0);
                        ED_area_tag_redraw(CTX_wm_area(C));
                        
                        BKE_paint_proj_mesh_data_check(scene, ob, NULL, NULL, NULL, NULL);
@@@ -5860,7 -5858,7 +5863,7 @@@ static int texture_paint_delete_texture
        ma->mtex[slot->index] = NULL;
        
        BKE_texpaint_slot_refresh_cache(scene, ma);
 -      DAG_id_tag_update(&ma->id, 0);
 +      DEG_id_tag_update(&ma->id, 0);
        WM_event_add_notifier(C, NC_MATERIAL, ma);
        /* we need a notifier for data change since we change the displayed modifier uvs */
        WM_event_add_notifier(C, NC_GEOM | ND_DATA, ob->data);
@@@ -5918,7 -5916,7 +5921,7 @@@ static int add_simple_uvs_exec(bContex
  
        BKE_paint_proj_mesh_data_check(scene, ob, NULL, NULL, NULL, NULL);
        
 -      DAG_id_tag_update(ob->data, 0);
 +      DEG_id_tag_update(ob->data, 0);
        WM_event_add_notifier(C, NC_GEOM | ND_DATA, ob->data);
        WM_event_add_notifier(C, NC_SCENE | ND_TOOLSETTINGS, scene);
        return OPERATOR_FINISHED;
index e36cc275ba93974deb348d07b3d391e987a31c1c,10628d8cccbba51e5224e6db6fa2bd91899a115f..21756fc435d5a9b2e27b3f42e0394ab907d5b83d
@@@ -59,7 -59,8 +59,7 @@@
  #include "RNA_access.h"
  #include "RNA_define.h"
  
 -#include "BIF_gl.h"
 -#include "BIF_glutil.h"
 +#include "GPU_matrix.h"
  
  #include "IMB_colormanagement.h"
  #include "IMB_imbuf_types.h"
@@@ -129,12 -130,17 +129,12 @@@ bool paint_convert_bb_to_rect(rcti *rec
   * 2D screens-space bounding box into four 3D planes) */
  void paint_calc_redraw_planes(float planes[4][4],
                                const ARegion *ar,
 -                              RegionView3D *rv3d,
                                Object *ob,
                                const rcti *screen_rect)
  {
        BoundBox bb;
 -      bglMats mats;
        rcti rect;
  
 -      memset(&bb, 0, sizeof(BoundBox));
 -      view3d_get_transformation(ar, rv3d, ob, &mats);
 -
        /* use some extra space just in case */
        rect = *screen_rect;
        rect.xmin -= 2;
        rect.ymin -= 2;
        rect.ymax += 2;
  
 -      ED_view3d_clipping_calc(&bb, planes, &mats, &rect);
 +      ED_view3d_clipping_calc(&bb, planes, ar, ob, &rect);
        negate_m4(planes);
  }
  
@@@ -284,8 -290,8 +284,8 @@@ static void imapaint_pick_uv(Scene *sce
  
        /* get the needed opengl matrices */
        glGetIntegerv(GL_VIEWPORT, view);
 -      glGetFloatv(GL_MODELVIEW_MATRIX,  (float *)matrix);
 -      glGetFloatv(GL_PROJECTION_MATRIX, (float *)proj);
 +      gpuGetModelViewMatrix(matrix);
 +      gpuGetProjectionMatrix(proj);
        view[0] = view[1] = 0;
        mul_m4_m4m4(matrix, matrix, ob->obmat);
        mul_m4_m4m4(matrix, proj, matrix);
@@@ -420,7 -426,7 +420,7 @@@ void paint_sample_color(bContext *C, AR
        Scene *scene = CTX_data_scene(C);
        Paint *paint = BKE_paint_get_active_from_context(C);
        Palette *palette = BKE_paint_palette(paint);
-       PaletteColor *color;
+       PaletteColor *color = NULL;
        Brush *br = BKE_paint_brush(BKE_paint_get_active_from_context(C));
        unsigned int col;
        const unsigned char *cp;
  
        if (CTX_wm_view3d(C) && texpaint_proj) {
                /* first try getting a colour directly from the mesh faces if possible */
 -              Object *ob = OBACT;
 +              SceneLayer *sl = CTX_data_scene_layer(C);
 +              Object *ob = OBACT_NEW;
                bool sample_success = false;
                ImagePaintSettings *imapaint = &scene->toolsettings->imapaint;
                bool use_material = (imapaint->mode == IMAGEPAINT_MODE_MATERIAL);
@@@ -556,9 -561,8 +556,9 @@@ static int brush_curve_preset_exec(bCon
  
        if (br) {
                Scene *scene = CTX_data_scene(C);
 +              SceneLayer *sl = CTX_data_scene_layer(C);
                BKE_brush_curve_preset(br, RNA_enum_get(op->ptr, "shape"));
 -              BKE_paint_invalidate_cursor_overlay(scene, br->curve);
 +              BKE_paint_invalidate_cursor_overlay(scene, sl, br->curve);
        }
  
        return OPERATOR_FINISHED;
index 659dfbfd57d507afcbfcc16fa941ca46941d3111,3c64d1ebb7154607ef75992f5208dea66c133b9c..1cd2fcb442b827f6f404983b877b469834905f5d
@@@ -56,6 -56,7 +56,6 @@@
  
  #include "BKE_colortools.h"
  #include "BKE_context.h"
 -#include "BKE_depsgraph.h"
  #include "BKE_DerivedMesh.h"
  #include "BKE_icons.h"
  #include "BKE_image.h"
@@@ -69,8 -70,6 +69,8 @@@
  #include "BKE_sound.h"
  #include "BKE_scene.h"
  
 +#include "DEG_depsgraph.h"
 +
  #include "GPU_draw.h"
  #include "GPU_buffers.h"
  
@@@ -793,7 -792,6 +793,7 @@@ static int image_view_selected_exec(bCo
        SpaceImage *sima;
        ARegion *ar;
        Scene *scene;
 +      SceneLayer *sl;
        Object *obedit;
        Image *ima;
  
        sima = CTX_wm_space_image(C);
        ar = CTX_wm_region(C);
        scene = CTX_data_scene(C);
 +      sl = CTX_data_scene_layer(C);
        obedit = CTX_data_edit_object(C);
  
        ima = ED_space_image(sima);
                        return OPERATOR_CANCELLED;
                }
        }
 -      else if (ED_space_image_check_show_maskedit(scene, sima)) {
 +      else if (ED_space_image_check_show_maskedit(sl, sima)) {
                if (!ED_mask_selected_minmax(C, min, max)) {
                        return OPERATOR_CANCELLED;
                }
@@@ -1172,6 -1169,7 +1172,7 @@@ static int image_sequence_get_len(ListB
                }
                return frame_curr - (*ofs);
        }
+       *ofs = 0;
        return 0;
  }
  
@@@ -2348,7 -2346,7 +2349,7 @@@ static int image_reload_exec(bContext *
        
        // XXX other users?
        BKE_image_signal(ima, (sima) ? &sima->iuser : NULL, IMA_SIGNAL_RELOAD);
 -      DAG_id_tag_update(&ima->id, 0);
 +      DEG_id_tag_update(&ima->id, 0);
  
        WM_event_add_notifier(C, NC_IMAGE | NA_EDITED, ima);