Merge branch 'master' into blender2.8
[blender.git] / source / blender / render / intern / source / pipeline.c
index 6ed2534f1522527a899480385e5be569073ca6e2..29a5c2c3dd6953a9431093c913ae41822780162b 100644 (file)
@@ -64,9 +64,9 @@
 #include "BKE_animsys.h"  /* <------ should this be here?, needed for sequencer update */
 #include "BKE_camera.h"
 #include "BKE_colortools.h"
-#include "BKE_depsgraph.h"
 #include "BKE_global.h"
 #include "BKE_image.h"
+#include "BKE_layer.h"
 #include "BKE_library.h"
 #include "BKE_library_remap.h"
 #include "BKE_main.h"
@@ -80,6 +80,8 @@
 #include "BKE_writeavi.h"  /* <------ should be replaced once with generic movie module */
 #include "BKE_object.h"
 
+#include "DEG_depsgraph.h"
+
 #include "PIL_time.h"
 #include "IMB_colormanagement.h"
 #include "IMB_imbuf.h"
@@ -264,7 +266,7 @@ RenderResult *RE_MultilayerConvert(void *exrhandle, const char *colorspace, bool
 
 RenderLayer *render_get_active_layer(Render *re, RenderResult *rr)
 {
-       RenderLayer *rl = BLI_findlink(&rr->layers, re->r.actlay);
+       RenderLayer *rl = BLI_findlink(&rr->layers, re->active_view_layer);
        
        if (rl)
                return rl;
@@ -274,21 +276,21 @@ RenderLayer *render_get_active_layer(Render *re, RenderResult *rr)
 
 static int render_scene_needs_vector(Render *re)
 {
-       SceneRenderLayer *srl;
-       
-       for (srl = re->r.layers.first; srl; srl = srl->next)
-               if (!(srl->layflag & SCE_LAY_DISABLE))
-                       if (srl->passflag & SCE_PASS_VECTOR)
+       ViewLayer *view_layer;
+       for (view_layer = re->view_layers.first; view_layer; view_layer = view_layer->next)
+               if (view_layer->flag & VIEW_LAYER_RENDER) {
+                       if (view_layer->passflag & SCE_PASS_VECTOR) {
                                return 1;
-
+                       }
+               }
        return 0;
 }
 
 static bool render_scene_has_layers_to_render(Scene *scene)
 {
-       SceneRenderLayer *srl;
-       for (srl = scene->r.layers.first; srl; srl = srl->next) {
-               if (!(srl->layflag & SCE_LAY_DISABLE)) {
+       ViewLayer *view_layer;
+       for (view_layer = scene->view_layers.first; view_layer; view_layer = view_layer->next) {
+               if (view_layer->flag & VIEW_LAYER_RENDER) {
                        return true;
                }
        }
@@ -353,6 +355,15 @@ Scene *RE_GetScene(Render *re)
        return NULL;
 }
 
+EvaluationContext *RE_GetEvalCtx(Render *re)
+{
+       if (re) {
+               return re->eval_ctx;
+       }
+
+       return NULL;
+}
+
 /**
  * Same as #RE_AcquireResultImage but creating the necessary views to store the result
  * fill provided result struct with a copy of thew views of what is done so far the
@@ -573,7 +584,7 @@ void RE_FreeRender(Render *re)
        BLI_rw_mutex_end(&re->resultmutex);
        BLI_rw_mutex_end(&re->partsmutex);
 
-       BLI_freelistN(&re->r.layers);
+       BLI_freelistN(&re->view_layers);
        BLI_freelistN(&re->r.views);
 
        curvemapping_free_data(&re->r.mblur_shutter_curve);
@@ -581,6 +592,7 @@ void RE_FreeRender(Render *re)
        /* main dbase can already be invalid now, some database-free code checks it */
        re->main = NULL;
        re->scene = NULL;
+       re->depsgraph = NULL;
        
        RE_Database_Free(re);   /* view render can still have full database */
        free_sample_tables(re);
@@ -650,12 +662,13 @@ void RE_FreePersistentData(void)
 /* ********* initialize state ******** */
 
 /* clear full sample and tile flags if needed */
-static int check_mode_full_sample(RenderData *rd)
+static int check_mode_full_sample(RenderData *rd, ViewRender *view_render)
 {
+       const char *engine_id = view_render->engine_id;
        int scemode = rd->scemode;
 
-       if (!STREQ(rd->engine, RE_engine_id_BLENDER_RENDER) &&
-           !STREQ(rd->engine, RE_engine_id_BLENDER_GAME))
+       if (!STREQ(engine_id, RE_engine_id_BLENDER_RENDER) &&
+           !STREQ(engine_id, RE_engine_id_BLENDER_GAME))
        {
                scemode &= ~R_FULL_SAMPLE;
        }
@@ -717,21 +730,25 @@ static void re_init_resolution(Render *re, Render *source,
 
 void render_copy_renderdata(RenderData *to, RenderData *from)
 {
-       BLI_freelistN(&to->layers);
        BLI_freelistN(&to->views);
        curvemapping_free_data(&to->mblur_shutter_curve);
 
        *to = *from;
 
-       BLI_duplicatelist(&to->layers, &from->layers);
        BLI_duplicatelist(&to->views, &from->views);
        curvemapping_copy_data(&to->mblur_shutter_curve, &from->mblur_shutter_curve);
 }
 
+void render_copy_viewrender(ViewRender *to, ViewRender *from)
+{
+       BKE_viewrender_copy(to, from);
+}
+
 /* what doesn't change during entire render sequence */
 /* disprect is optional, if NULL it assumes full window render */
 void RE_InitState(Render *re, Render *source, RenderData *rd,
-                  SceneRenderLayer *srl,
+                  ListBase *render_layers, const int active_layer,
+                  ViewRender *view_render, ViewLayer *view_layer,
                   int winx, int winy, rcti *disprect)
 {
        bool had_freestyle = (re->r.mode & R_EDGE_FRS) != 0;
@@ -742,6 +759,10 @@ void RE_InitState(Render *re, Render *source, RenderData *rd,
 
        /* copy render data and render layers for thread safety */
        render_copy_renderdata(&re->r, rd);
+       render_copy_viewrender(&re->view_render, view_render);
+       BLI_freelistN(&re->view_layers);
+       BLI_duplicatelist(&re->view_layers, render_layers);
+       re->active_view_layer = active_layer;
 
        if (source) {
                /* reuse border flags from source renderer */
@@ -771,7 +792,7 @@ void RE_InitState(Render *re, Render *source, RenderData *rd,
                return;
        }
 
-       re->r.scemode = check_mode_full_sample(&re->r);
+       re->r.scemode = check_mode_full_sample(&re->r, &re->view_render);
        
        /* fullsample wants uniform osa levels */
        if (source && (re->r.scemode & R_FULL_SAMPLE)) {
@@ -790,10 +811,10 @@ void RE_InitState(Render *re, Render *source, RenderData *rd,
                else re->osa = 0;
        }
        
-       if (srl) {
-               int index = BLI_findindex(&rd->layers, srl);
+       if (view_layer) {
+               int index = BLI_findindex(render_layers, view_layer);
                if (index != -1) {
-                       re->r.actlay = index;
+                       re->active_view_layer = index;
                        re->r.scemode |= R_SINGLE_LAYER;
                }
        }
@@ -811,12 +832,12 @@ void RE_InitState(Render *re, Render *source, RenderData *rd,
                        re->result = NULL;
                }
                else if (re->result) {
-                       SceneRenderLayer *actsrl = BLI_findlink(&re->r.layers, re->r.actlay);
+                       ViewLayer *active_render_layer = BLI_findlink(&re->view_layers, re->active_view_layer);
                        RenderLayer *rl;
                        bool have_layer = false;
 
                        for (rl = re->result->layers.first; rl; rl = rl->next)
-                               if (STREQ(rl->name, actsrl->name))
+                               if (STREQ(rl->name, active_render_layer->name))
                                        have_layer = true;
 
                        if (re->result->rectx == re->rectx && re->result->recty == re->recty &&
@@ -941,7 +962,7 @@ void RE_ChangeModeFlag(Render *re, int flag, bool clear)
 
 /* update some variables that can be animated, and otherwise wouldn't be due to
  * RenderData getting copied once at the start of animation render */
-void render_update_anim_renderdata(Render *re, RenderData *rd)
+void render_update_anim_renderdata(Render *re, RenderData *rd, ListBase *render_layers)
 {
        /* filter */
        re->r.gauss = rd->gauss;
@@ -955,8 +976,8 @@ void render_update_anim_renderdata(Render *re, RenderData *rd)
        re->r.unit_line_thickness = rd->unit_line_thickness;
 
        /* render layers */
-       BLI_freelistN(&re->r.layers);
-       BLI_duplicatelist(&re->r.layers, &rd->layers);
+       BLI_freelistN(&re->view_layers);
+       BLI_duplicatelist(&re->view_layers, render_layers);
 
        /* render views */
        BLI_freelistN(&re->r.views);
@@ -1729,7 +1750,7 @@ static void do_render_blur_3d(Render *re)
        
        /* make sure motion blur changes get reset to current frame */
        if ((re->r.scemode & (R_NO_FRAME_UPDATE|R_BUTS_PREVIEW|R_VIEWPORT_PREVIEW))==0) {
-               BKE_scene_update_for_newframe(re->eval_ctx, re->main, re->scene, re->lay);
+               BKE_scene_graph_update_for_newframe(re->eval_ctx, re->depsgraph, re->main, re->scene, NULL);
        }
        
        /* weak... the display callback wants an active renderlayer pointer... */
@@ -1958,13 +1979,14 @@ static void render_scene(Render *re, Scene *sce, int cfra)
        }
        
        /* initial setup */
-       RE_InitState(resc, re, &sce->r, NULL, winx, winy, &re->disprect);
+       RE_InitState(resc, re, &sce->r, &sce->view_layers, sce->active_view_layer, &sce->view_render, NULL, winx, winy, &re->disprect);
 
        /* We still want to use 'rendercache' setting from org (main) scene... */
        resc->r.scemode = (resc->r.scemode & ~R_EXR_CACHE_FILE) | (re->r.scemode & R_EXR_CACHE_FILE);
 
        /* still unsure entity this... */
        resc->main = re->main;
+       resc->depsgraph = re->depsgraph;
        resc->scene = sce;
        resc->lay = sce->lay;
        resc->scene_color_manage = BKE_scene_check_color_management_enabled(sce);
@@ -2045,17 +2067,10 @@ bool RE_allow_render_generic_object(Object *ob)
 #define DEPSGRAPH_WORKAROUND_HACK
 
 #ifdef DEPSGRAPH_WORKAROUND_HACK
-static void tag_dependend_objects_for_render(Scene *scene, int renderlay)
+static void tag_dependend_objects_for_render(Scene *scene, int UNUSED(renderlay))
 {
-       Scene *sce_iter;
-       Base *base;
-       for (SETLOOPER(scene, sce_iter, base)) {
-               Object *object = base->object;
-
-               if ((base->lay & renderlay) == 0) {
-                       continue;
-               }
-
+       FOREACH_OBJECT_RENDERABLE(scene, object)
+       {
                if (object->type == OB_MESH) {
                        if (RE_allow_render_generic_object(object)) {
                                ModifierData *md;
@@ -2072,22 +2087,22 @@ static void tag_dependend_objects_for_render(Scene *scene, int renderlay)
                                        if (md->type == eModifierType_Boolean) {
                                                BooleanModifierData *bmd = (BooleanModifierData *)md;
                                                if (bmd->object && bmd->object->type == OB_MESH) {
-                                                       DAG_id_tag_update(&bmd->object->id, OB_RECALC_DATA);
+                                                       DEG_id_tag_update(&bmd->object->id, OB_RECALC_DATA);
                                                }
                                        }
                                        else if (md->type == eModifierType_Array) {
                                                ArrayModifierData *amd = (ArrayModifierData *)md;
                                                if (amd->start_cap && amd->start_cap->type == OB_MESH) {
-                                                       DAG_id_tag_update(&amd->start_cap->id, OB_RECALC_DATA);
+                                                       DEG_id_tag_update(&amd->start_cap->id, OB_RECALC_DATA);
                                                }
                                                if (amd->end_cap && amd->end_cap->type == OB_MESH) {
-                                                       DAG_id_tag_update(&amd->end_cap->id, OB_RECALC_DATA);
+                                                       DEG_id_tag_update(&amd->end_cap->id, OB_RECALC_DATA);
                                                }
                                        }
                                        else if (md->type == eModifierType_Shrinkwrap) {
                                                ShrinkwrapModifierData *smd = (ShrinkwrapModifierData *)md;
                                                if (smd->target  && smd->target->type == OB_MESH) {
-                                                       DAG_id_tag_update(&smd->target->id, OB_RECALC_DATA);
+                                                       DEG_id_tag_update(&smd->target->id, OB_RECALC_DATA);
                                                }
                                        }
                                        else if (md->type == eModifierType_ParticleSystem) {
@@ -2097,7 +2112,7 @@ static void tag_dependend_objects_for_render(Scene *scene, int renderlay)
                                                switch (part->ren_as) {
                                                        case PART_DRAW_OB:
                                                                if (part->dup_ob != NULL) {
-                                                                       DAG_id_tag_update(&part->dup_ob->id, OB_RECALC_DATA);
+                                                                       DEG_id_tag_update(&part->dup_ob->id, OB_RECALC_DATA);
                                                                }
                                                                break;
                                                        case PART_DRAW_GR:
@@ -2106,7 +2121,7 @@ static void tag_dependend_objects_for_render(Scene *scene, int renderlay)
                                                                             go != NULL;
                                                                             go = go->next)
                                                                        {
-                                                                               DAG_id_tag_update(&go->ob->id, OB_RECALC_DATA);
+                                                                               DEG_id_tag_update(&go->ob->id, OB_RECALC_DATA);
                                                                        }
                                                                }
                                                                break;
@@ -2116,6 +2131,32 @@ static void tag_dependend_objects_for_render(Scene *scene, int renderlay)
                        }
                }
        }
+       FOREACH_OBJECT_RENDERABLE_END
+}
+#endif
+
+#define DEPSGRAPH_WORKAROUND_GROUP_HACK
+
+#ifdef DEPSGRAPH_WORKAROUND_GROUP_HACK
+/**
+ * Make sure the COLLECTION_VIEWPORT / COLLECTION_RENDER is considered
+ * for the collections visibility.
+ *
+ * This won't be needed anymore once we have depsgraph per render engine.
+ */
+static void tag_groups_for_render(Render *re)
+{
+       for (Group *group = re->main->group.first; group; group = group->id.next) {
+               DEG_id_tag_update(&group->id, 0);
+       }
+
+#ifdef WITH_FREESTYLE
+       if (re->freestyle_bmain) {
+               for (Group *group = re->freestyle_bmain->group.first; group; group = group->id.next) {
+                       DEG_id_tag_update(&group->id, 0);
+               }
+       }
+#endif
 }
 #endif
 
@@ -2202,6 +2243,10 @@ static void ntree_render_scenes(Render *re)
        if (re->scene->nodetree == NULL) return;
        
        tag_scenes_for_render(re);
+
+#ifdef DEPSGRAPH_WORKAROUND_GROUP_HACK
+       tag_groups_for_render(re);
+#endif
        
        /* now foreach render-result node tagged we do a full render */
        /* results are stored in a way compisitor will find it */
@@ -2256,24 +2301,24 @@ static void init_freestyle(Render *re)
 /* invokes Freestyle stroke rendering */
 static void add_freestyle(Render *re, int render)
 {
-       SceneRenderLayer *srl, *actsrl;
+       ViewLayer *view_layer, *active_view_layer;
        LinkData *link;
        Render *r;
        const bool do_link = (re->r.mode & R_MBLUR) == 0 || re->i.curblur == re->r.mblur_samples;
 
-       actsrl = BLI_findlink(&re->r.layers, re->r.actlay);
+       active_view_layer = BLI_findlink(&re->view_layers, re->active_view_layer);
 
        FRS_begin_stroke_rendering(re);
 
-       for (srl = (SceneRenderLayer *)re->r.layers.first; srl; srl = srl->next) {
+       for (view_layer = (ViewLayer *)re->view_layers.first; view_layer; view_layer = view_layer->next) {
                if (do_link) {
                        link = (LinkData *)MEM_callocN(sizeof(LinkData), "LinkData to Freestyle render");
                        BLI_addtail(&re->freestyle_renders, link);
                }
-               if ((re->r.scemode & R_SINGLE_LAYER) && srl != actsrl)
+               if ((re->r.scemode & R_SINGLE_LAYER) && view_layer != active_view_layer)
                        continue;
-               if (FRS_is_freestyle_enabled(srl)) {
-                       r = FRS_do_stroke_rendering(re, srl, render);
+               if (FRS_is_freestyle_enabled(view_layer)) {
+                       r = FRS_do_stroke_rendering(re, view_layer, render);
                        if (do_link)
                                link->data = (void *)r;
                }
@@ -2290,25 +2335,25 @@ static void composite_freestyle_renders(Render *re, int sample)
 {
        Render *freestyle_render;
        RenderView *rv;
-       SceneRenderLayer *srl, *actsrl;
+       ViewLayer *view_layer, *active_view_layer;
        LinkData *link;
 
-       actsrl = BLI_findlink(&re->r.layers, re->r.actlay);
+       active_view_layer = BLI_findlink(&re->view_layers, re->active_view_layer);
 
        link = (LinkData *)re->freestyle_renders.first;
 
        for (rv = re->result->views.first; rv; rv = rv->next) {
-               for (srl = (SceneRenderLayer *)re->r.layers.first; srl; srl = srl->next) {
-                       if ((re->r.scemode & R_SINGLE_LAYER) && srl != actsrl)
+               for (view_layer = (ViewLayer *)re->view_layers.first; view_layer; view_layer = view_layer->next) {
+                       if ((re->r.scemode & R_SINGLE_LAYER) && view_layer != active_view_layer)
                                continue;
 
-                       if (FRS_is_freestyle_enabled(srl)) {
+                       if (FRS_is_freestyle_enabled(view_layer)) {
                                freestyle_render = (Render *)link->data;
 
                                /* may be NULL in case of empty render layer */
                                if (freestyle_render) {
                                        render_result_exr_file_read_sample(freestyle_render, sample);
-                                       FRS_composite_result(re, srl, freestyle_render);
+                                       FRS_composite_result(re, view_layer, freestyle_render);
                                        RE_FreeRenderResult(freestyle_render->result);
                                        freestyle_render->result = NULL;
                                }
@@ -2419,6 +2464,10 @@ static void do_merge_fullsample(Render *re, bNodeTree *ntree)
                        }
                }
                
+#ifdef DEPSGRAPH_WORKAROUND_GROUP_HACK
+               tag_groups_for_render(re);
+#endif
+
                /* composite */
                if (ntree) {
                        ntreeCompositTagRender(re->scene);
@@ -2570,6 +2619,11 @@ void RE_MergeFullSample(Render *re, Main *bmain, Scene *sce, bNodeTree *ntree)
 #ifdef WITH_FREESTYLE
        free_all_freestyle_renders();
 #endif
+
+#ifdef DEPSGRAPH_WORKAROUND_GROUP_HACK
+       /* Restore their visibility based on the viewport visibility flags. */
+       tag_groups_for_render(re);
+#endif
 }
 
 /* returns fully composited render-result on given time step (in RenderData) */
@@ -2643,7 +2697,7 @@ static void do_render_composite_fields_blur_3d(Render *re)
                                R.i.cfra = re->i.cfra;
                                
                                if (update_newframe)
-                                       BKE_scene_update_for_newframe(re->eval_ctx, re->main, re->scene, re->lay);
+                                       BKE_scene_graph_update_for_newframe(re->eval_ctx, re->depsgraph, re->main, re->scene, NULL);
                                
                                if (re->r.scemode & R_FULL_SAMPLE)
                                        do_merge_fullsample(re, ntree);
@@ -2668,6 +2722,11 @@ static void do_render_composite_fields_blur_3d(Render *re)
        free_all_freestyle_renders();
 #endif
 
+#ifdef DEPSGRAPH_WORKAROUND_GROUP_HACK
+       /* Restore their visibility based on the viewport visibility flags. */
+       tag_groups_for_render(re);
+#endif
+
        /* weak... the display callback wants an active renderlayer pointer... */
        if (re->result != NULL) {
                re->result->renlay = render_get_active_layer(re, re->result);
@@ -2886,12 +2945,12 @@ static void do_render_all_options(Render *re)
 
 bool RE_force_single_renderlayer(Scene *scene)
 {
-       int scemode = check_mode_full_sample(&scene->r);
+       int scemode = check_mode_full_sample(&scene->r, &scene->view_render);
        if (scemode & R_SINGLE_LAYER) {
-               SceneRenderLayer *srl = BLI_findlink(&scene->r.layers, scene->r.actlay);
+               ViewLayer *view_layer = BLI_findlink(&scene->view_layers, scene->active_view_layer);
                /* force layer to be enabled */
-               if (srl->layflag & SCE_LAY_DISABLE) {
-                       srl->layflag &= ~SCE_LAY_DISABLE;
+               if ((view_layer->flag & VIEW_LAYER_RENDER) == 0) {
+                       view_layer->flag |= VIEW_LAYER_RENDER;
                        return true;
                }
        }
@@ -2907,7 +2966,7 @@ static bool check_valid_compositing_camera(Scene *scene, Object *camera_override
                        if (node->type == CMP_NODE_R_LAYERS && (node->flag & NODE_MUTED) == 0) {
                                Scene *sce = node->id ? (Scene *)node->id : scene;
                                if (sce->camera == NULL) {
-                                       sce->camera = BKE_scene_camera_find(sce);
+                                       sce->camera = BKE_view_layer_camera_find(BKE_view_layer_from_scene_get(sce));
                                }
                                if (sce->camera == NULL) {
                                        /* all render layers nodes need camera */
@@ -2965,7 +3024,7 @@ static int check_valid_camera(Scene *scene, Object *camera_override, ReportList
        const char *err_msg = "No camera found in scene \"%s\"";
 
        if (camera_override == NULL && scene->camera == NULL)
-               scene->camera = BKE_scene_camera_find(scene);
+               scene->camera = BKE_view_layer_camera_find(BKE_view_layer_from_scene_get(scene));
 
        if (!check_valid_camera_multiview(scene, scene->camera, reports))
                return false;
@@ -2980,7 +3039,9 @@ static int check_valid_camera(Scene *scene, Object *camera_override, ReportList
                                    (seq->scene != NULL))
                                {
                                        if (!seq->scene_camera) {
-                                               if (!seq->scene->camera && !BKE_scene_camera_find(seq->scene)) {
+                                               if (!seq->scene->camera &&
+                                                   !BKE_view_layer_camera_find(BKE_view_layer_from_scene_get(seq->scene)))
+                                               {
                                                        /* camera could be unneeded due to composite nodes */
                                                        Object *override = (seq->scene == scene) ? camera_override : NULL;
 
@@ -3033,7 +3094,7 @@ static int check_composite_output(Scene *scene)
 
 bool RE_is_rendering_allowed(Scene *scene, Object *camera_override, ReportList *reports)
 {
-       int scemode = check_mode_full_sample(&scene->r);
+       int scemode = check_mode_full_sample(&scene->r, &scene->view_render);
        
        if (scene->r.mode & R_BORDER) {
                if (scene->r.border.xmax <= scene->r.border.xmin ||
@@ -3146,13 +3207,15 @@ static void validate_render_settings(Render *re)
        }
 }
 
-static void update_physics_cache(Render *re, Scene *scene, int UNUSED(anim_init))
+static void update_physics_cache(Render *re, Scene *scene, ViewLayer *view_layer, int UNUSED(anim_init))
 {
        PTCacheBaker baker;
 
        memset(&baker, 0, sizeof(baker));
        baker.main = re->main;
        baker.scene = scene;
+       baker.view_layer = view_layer;
+       baker.depsgraph = BKE_scene_get_depsgraph(scene, view_layer, true);
        baker.bake = 0;
        baker.render = 1;
        baker.anim_init = 1;
@@ -3171,9 +3234,15 @@ const char *RE_GetActiveRenderView(Render *re)
        return re->viewname;
 }
 
+void RE_SetEngineByID(Render *re, const char *engine_id)
+{
+       BLI_strncpy(re->view_render.engine_id, engine_id, sizeof(re->view_render.engine_id));
+}
+
 /* evaluating scene options for general Blender render */
-static int render_initialize_from_main(Render *re, RenderData *rd, Main *bmain, Scene *scene, SceneRenderLayer *srl,
-                                       Object *camera_override, unsigned int lay_override, int anim, int anim_init)
+static int render_initialize_from_main(Render *re, RenderData *rd, Main *bmain, Scene *scene, ViewRender *view_render,
+                                       ViewLayer *view_layer, Object *camera_override, unsigned int lay_override,
+                                       int anim, int anim_init)
 {
        int winx, winy;
        rcti disprect;
@@ -3207,10 +3276,11 @@ static int render_initialize_from_main(Render *re, RenderData *rd, Main *bmain,
        re->layer_override = lay_override;
        re->i.localview = (re->lay & 0xFF000000) != 0;
        re->viewname[0] = '\0';
+       RE_SetEngineByID(re, view_render->engine_id);
 
        /* not too nice, but it survives anim-border render */
        if (anim) {
-               render_update_anim_renderdata(re, &scene->r);
+               render_update_anim_renderdata(re, &scene->r, &scene->view_layers);
                re->disprect = disprect;
                return 1;
        }
@@ -3218,6 +3288,11 @@ static int render_initialize_from_main(Render *re, RenderData *rd, Main *bmain,
        /* check all scenes involved */
        tag_scenes_for_render(re);
 
+#ifdef DEPSGRAPH_WORKAROUND_GROUP_HACK
+       /* Update group collections visibility. */
+       tag_groups_for_render(re);
+#endif
+
        /*
         * Disabled completely for now,
         * can be later set as render profile option
@@ -3225,16 +3300,17 @@ static int render_initialize_from_main(Render *re, RenderData *rd, Main *bmain,
         */
        if (0) {
                /* make sure dynamics are up to date */
-               update_physics_cache(re, scene, anim_init);
+               view_layer = BKE_view_layer_from_scene_get(scene);
+               update_physics_cache(re, scene, view_layer, anim_init);
        }
        
-       if (srl || scene->r.scemode & R_SINGLE_LAYER) {
+       if (view_layer || scene->r.scemode & R_SINGLE_LAYER) {
                BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
                render_result_single_layer_begin(re);
                BLI_rw_mutex_unlock(&re->resultmutex);
        }
        
-       RE_InitState(re, NULL, &scene->r, srl, winx, winy, &disprect);
+       RE_InitState(re, NULL, &scene->r, &scene->view_layers, scene->active_view_layer, &scene->view_render, view_layer, winx, winy, &disprect);
        if (!re->ok)  /* if an error was printed, abort */
                return 0;
        
@@ -3255,7 +3331,7 @@ void RE_SetReports(Render *re, ReportList *reports)
 }
 
 /* general Blender frame render call */
-void RE_BlenderFrame(Render *re, Main *bmain, Scene *scene, SceneRenderLayer *srl, Object *camera_override,
+void RE_BlenderFrame(Render *re, Main *bmain, Scene *scene, ViewLayer *view_layer, Object *camera_override,
                      unsigned int lay_override, int frame, const bool write_still)
 {
        BLI_callback_exec(re->main, (ID *)scene, BLI_CB_EVT_RENDER_INIT);
@@ -3265,7 +3341,9 @@ void RE_BlenderFrame(Render *re, Main *bmain, Scene *scene, SceneRenderLayer *sr
        
        scene->r.cfra = frame;
        
-       if (render_initialize_from_main(re, &scene->r, bmain, scene, srl, camera_override, lay_override, 0, 0)) {
+       if (render_initialize_from_main(re, &scene->r, bmain, scene, &scene->view_render, view_layer,
+                                       camera_override, lay_override, 0, 0))
+       {
                MEM_reset_peak_memory();
 
                BLI_callback_exec(re->main, (ID *)scene, BLI_CB_EVT_RENDER_PRE);
@@ -3304,7 +3382,7 @@ void RE_BlenderFrame(Render *re, Main *bmain, Scene *scene, SceneRenderLayer *sr
 void RE_RenderFreestyleStrokes(Render *re, Main *bmain, Scene *scene, int render)
 {
        re->result_ok= 0;
-       if (render_initialize_from_main(re, &scene->r, bmain, scene, NULL, NULL, scene->lay, 0, 0)) {
+       if (render_initialize_from_main(re, &scene->r, bmain, scene, &scene->view_render, NULL, NULL, scene->lay, 0, 0)) {
                if (render)
                        do_render_fields_blur_3d(re);
        }
@@ -3600,7 +3678,7 @@ void RE_BlenderAnim(Render *re, Main *bmain, Scene *scene, Object *camera_overri
        BLI_callback_exec(re->main, (ID *)scene, BLI_CB_EVT_RENDER_INIT);
 
        /* do not fully call for each frame, it initializes & pops output window */
-       if (!render_initialize_from_main(re, &rd, bmain, scene, NULL, camera_override, lay_override, 0, 1))
+       if (!render_initialize_from_main(re, &rd, bmain, scene, &scene->view_render, NULL, camera_override, lay_override, 0, 1))
                return;
 
        /* MULTIVIEW_TODO:
@@ -3690,22 +3768,12 @@ void RE_BlenderAnim(Render *re, Main *bmain, Scene *scene, Object *camera_overri
                        }
 
                        /* only border now, todo: camera lens. (ton) */
-                       render_initialize_from_main(re, &rd, bmain, scene, NULL, camera_override, lay_override, 1, 0);
+                       render_initialize_from_main(re, &rd, bmain, scene, &scene->view_render,
+                                                   NULL, camera_override, lay_override, 1, 0);
 
                        if (nfra != scene->r.cfra) {
-                               /*
-                                * Skip this frame, but update for physics and particles system.
-                                * From convertblender.c:
-                                * in localview, lamps are using normal layers, objects only local bits.
-                                */
-                               unsigned int updatelay;
-
-                               if (re->lay & 0xFF000000)
-                                       updatelay = re->lay & 0xFF000000;
-                               else
-                                       updatelay = re->lay;
-
-                               BKE_scene_update_for_newframe(re->eval_ctx, bmain, scene, updatelay);
+                               /* Skip this frame, but update for physics and particles system. */
+                               BKE_scene_graph_update_for_newframe(re->eval_ctx, re->depsgraph, bmain, scene, NULL);
                                continue;
                        }
                        else
@@ -3851,15 +3919,16 @@ void RE_BlenderAnim(Render *re, Main *bmain, Scene *scene, Object *camera_overri
        G.is_rendering = false;
 }
 
-void RE_PreviewRender(Render *re, Main *bmain, Scene *sce)
+void RE_PreviewRender(Render *re, Main *bmain, Scene *sce, ViewRender *view_render)
 {
        Object *camera;
+       ViewLayer *view_layer = BKE_view_layer_from_scene_get(sce);
        int winx, winy;
 
        winx = (sce->r.size * sce->r.xsch) / 100;
        winy = (sce->r.size * sce->r.ysch) / 100;
 
-       RE_InitState(re, NULL, &sce->r, NULL, winx, winy, NULL);
+       RE_InitState(re, NULL, &sce->r, &sce->view_layers, sce->active_view_layer, view_render, NULL, winx, winy, NULL);
 
        re->pool = BKE_image_pool_new();
 
@@ -3867,6 +3936,8 @@ void RE_PreviewRender(Render *re, Main *bmain, Scene *sce)
        re->scene = sce;
        re->scene_color_manage = BKE_scene_check_color_management_enabled(sce);
        re->lay = sce->lay;
+       re->depsgraph = BKE_scene_get_depsgraph(sce, view_layer, false);
+       re->eval_ctx->view_layer = view_layer;
 
        camera = RE_GetCamera(re);
        RE_SetCamera(re, camera);
@@ -3912,7 +3983,7 @@ bool RE_ReadRenderResult(Scene *scene, Scene *scenode)
        re = RE_GetSceneRender(scene);
        if (re == NULL)
                re = RE_NewSceneRender(scene);
-       RE_InitState(re, NULL, &scene->r, NULL, winx, winy, &disprect);
+       RE_InitState(re, NULL, &scene->r, &scene->view_layers, scene->active_view_layer, &scene->view_render, NULL, winx, winy, &disprect);
        re->scene = scene;
        re->scene_color_manage = BKE_scene_check_color_management_enabled(scene);
        
@@ -4145,7 +4216,6 @@ RenderPass *RE_create_gp_pass(RenderResult *rr, const char *layername, const cha
                rl = MEM_callocN(sizeof(RenderLayer), layername);
                BLI_addtail(&rr->layers, rl);
                BLI_strncpy(rl->name, layername, sizeof(rl->name));
-               rl->lay = 0;
                rl->layflag = SCE_LAY_SOLID;
                rl->passflag = SCE_PASS_COMBINED;
                rl->rectx = rr->rectx;