Merge branch 'master' into blender2.8
[blender.git] / source / blender / editors / object / object_bake_api.c
index 3e0f621..677a98c 100644 (file)
 #include "BKE_report.h"
 #include "BKE_modifier.h"
 #include "BKE_mesh.h"
+#include "BKE_scene.h"
 #include "BKE_screen.h"
-#include "BKE_depsgraph.h"
+
+#include "DEG_depsgraph.h"
+#include "DEG_depsgraph_build.h"
 
 #include "RE_engine.h"
 #include "RE_pipeline.h"
@@ -84,6 +87,7 @@ typedef struct BakeAPIRender {
        Object *ob;
        Main *main;
        Scene *scene;
+       ViewLayer *view_layer;
        ReportList *reports;
        ListBase selected_objects;
 
@@ -272,7 +276,7 @@ static void refresh_images(BakeImages *bake_images)
                Image *ima = bake_images->data[i].image;
                if (ima->ok == IMA_OK_LOADED) {
                        GPU_free_image(ima);
-                       DAG_id_tag_update(&ima->id, 0);
+                       DEG_id_tag_update(&ima->id, 0);
                }
        }
 }
@@ -619,11 +623,11 @@ static size_t initialize_internal_images(BakeImages *bake_images, ReportList *re
 }
 
 /* create new mesh with edit mode changes and modifiers applied */
-static Mesh *bake_mesh_new_from_object(Main *bmain, Scene *scene, Object *ob)
+static Mesh *bake_mesh_new_from_object(Depsgraph *depsgraph, Main *bmain, Scene *scene, Object *ob)
 {
        ED_object_editmode_load(bmain, ob);
 
-       Mesh *me = BKE_mesh_new_from_object(bmain, scene, ob, 1, 2, 0, 0);
+       Mesh *me = BKE_mesh_new_from_object(depsgraph, bmain, scene, ob, 1, 0, 0);
        if (me->flag & ME_AUTOSMOOTH) {
                BKE_mesh_split_faces(me, true);
        }
@@ -632,7 +636,8 @@ static Mesh *bake_mesh_new_from_object(Main *bmain, Scene *scene, Object *ob)
 }
 
 static int bake(
-        Render *re, Main *bmain, Scene *scene, Object *ob_low, ListBase *selected_objects, ReportList *reports,
+        Render *re, Main *bmain, Scene *scene, ViewLayer *view_layer, Object *ob_low, ListBase *selected_objects,
+        ReportList *reports,
         const eScenePassType pass_type, const int pass_filter, const int margin,
         const eBakeSaveMode save_mode, const bool is_clear, const bool is_split_materials,
         const bool is_automatic_name, const bool is_selected_to_active, const bool is_cage,
@@ -640,6 +645,8 @@ static int bake(
         const char *custom_cage, const char *filepath, const int width, const int height,
         const char *identifier, ScrArea *sa, const char *uv_layer)
 {
+       Depsgraph *depsgraph = DEG_graph_new(scene, view_layer, DAG_EVAL_RENDER);
+
        int op_result = OPERATOR_CANCELLED;
        bool ok = false;
 
@@ -781,8 +788,12 @@ static int bake(
                }
        }
 
+       /* Make sure depsgraph is up to date. */
+       DEG_graph_build_from_view_layer(depsgraph, bmain, scene, view_layer);
+       BKE_scene_graph_update_tagged(depsgraph, bmain);
+
        /* get the mesh as it arrives in the renderer */
-       me_low = bake_mesh_new_from_object(bmain, scene, ob_low);
+       me_low = bake_mesh_new_from_object(depsgraph, bmain, scene, ob_low);
 
        /* populate the pixel array with the face data */
        if ((is_selected_to_active && (ob_cage == NULL) && is_cage) == false)
@@ -797,7 +808,7 @@ static int bake(
 
                /* prepare cage mesh */
                if (ob_cage) {
-                       me_cage = bake_mesh_new_from_object(bmain, scene, ob_cage);
+                       me_cage = bake_mesh_new_from_object(depsgraph, bmain, scene, ob_cage);
                        if ((me_low->totpoly != me_cage->totpoly) || (me_low->totloop != me_cage->totloop)) {
                                BKE_report(reports, RPT_ERROR,
                                           "Invalid cage object, the cage mesh must have the same number "
@@ -829,7 +840,7 @@ static int bake(
                        ob_low->modifiers = modifiers_tmp;
 
                        /* get the cage mesh as it arrives in the renderer */
-                       me_cage = bake_mesh_new_from_object(bmain, scene, ob_low);
+                       me_cage = bake_mesh_new_from_object(depsgraph, bmain, scene, ob_low);
                        RE_bake_pixels_populate(me_cage, pixel_array_low, num_pixels, &bake_images, uv_layer);
                }
 
@@ -855,7 +866,7 @@ static int bake(
                        tmd->quad_method = MOD_TRIANGULATE_QUAD_FIXED;
                        tmd->ngon_method = MOD_TRIANGULATE_NGON_EARCLIP;
 
-                       highpoly[i].me = bake_mesh_new_from_object(bmain, scene, highpoly[i].ob);
+                       highpoly[i].me = bake_mesh_new_from_object(depsgraph, bmain, scene, highpoly[i].ob);
                        highpoly[i].ob->restrictflag &= ~OB_RESTRICT_RENDER;
 
                        /* lowpoly to highpoly transformation matrix */
@@ -882,7 +893,7 @@ static int bake(
 
                /* the baking itself */
                for (i = 0; i < tot_highpoly; i++) {
-                       ok = RE_bake_engine(re, highpoly[i].ob, i, pixel_array_high,
+                       ok = RE_bake_engine(re, depsgraph, highpoly[i].ob, i, pixel_array_high,
                                            num_pixels, depth, pass_type, pass_filter, result);
                        if (!ok) {
                                BKE_reportf(reports, RPT_ERROR, "Error baking from object \"%s\"", highpoly[i].ob->id.name + 2);
@@ -909,7 +920,7 @@ cage_cleanup:
                ob_low->restrictflag &= ~OB_RESTRICT_RENDER;
 
                if (RE_bake_has_engine(re)) {
-                       ok = RE_bake_engine(re, ob_low, 0, pixel_array_low, num_pixels, depth, pass_type, pass_filter, result);
+                       ok = RE_bake_engine(re, depsgraph, ob_low, 0, pixel_array_low, num_pixels, depth, pass_type, pass_filter, result);
                }
                else {
                        BKE_report(reports, RPT_ERROR, "Current render engine does not support baking");
@@ -958,7 +969,7 @@ cage_cleanup:
                                                md->mode &= ~eModifierMode_Render;
                                        }
 
-                                       me_nores = bake_mesh_new_from_object(bmain, scene, ob_low);
+                                       me_nores = bake_mesh_new_from_object(depsgraph, bmain, scene, ob_low);
                                        RE_bake_pixels_populate(me_nores, pixel_array_low, num_pixels, &bake_images, uv_layer);
 
                                        RE_bake_normal_world_to_tangent(pixel_array_low, num_pixels, depth, result, me_nores, normal_swizzle, ob_low->obmat);
@@ -1109,6 +1120,8 @@ cleanup:
        if (me_cage)
                BKE_libblock_free(bmain, me_cage);
 
+       DEG_graph_free(depsgraph);
+
        return op_result;
 }
 
@@ -1119,6 +1132,7 @@ static void bake_init_api_data(wmOperator *op, bContext *C, BakeAPIRender *bkr)
 
        bkr->ob = CTX_data_active_object(C);
        bkr->main = CTX_data_main(C);
+       bkr->view_layer = CTX_data_view_layer(C);
        bkr->scene = CTX_data_scene(C);
        bkr->sa = sc ? BKE_screen_find_big_area(sc, SPACE_IMAGE, 10) : NULL;
 
@@ -1202,7 +1216,7 @@ static int bake_exec(bContext *C, wmOperator *op)
 
        if (bkr.is_selected_to_active) {
                result = bake(
-                       bkr.render, bkr.main, bkr.scene, bkr.ob, &bkr.selected_objects, bkr.reports,
+                       bkr.render, bkr.main, bkr.scene, bkr.view_layer, bkr.ob, &bkr.selected_objects, bkr.reports,
                        bkr.pass_type, bkr.pass_filter, bkr.margin, bkr.save_mode,
                        bkr.is_clear, bkr.is_split_materials, bkr.is_automatic_name, true, bkr.is_cage,
                        bkr.cage_extrusion, bkr.normal_space, bkr.normal_swizzle,
@@ -1215,7 +1229,7 @@ static int bake_exec(bContext *C, wmOperator *op)
                for (link = bkr.selected_objects.first; link; link = link->next) {
                        Object *ob_iter = link->ptr.data;
                        result = bake(
-                               bkr.render, bkr.main, bkr.scene, ob_iter, NULL, bkr.reports,
+                               bkr.render, bkr.main, bkr.scene, bkr.view_layer, ob_iter, NULL, bkr.reports,
                                bkr.pass_type, bkr.pass_filter, bkr.margin, bkr.save_mode,
                                is_clear, bkr.is_split_materials, bkr.is_automatic_name, false, bkr.is_cage,
                                bkr.cage_extrusion, bkr.normal_space, bkr.normal_swizzle,
@@ -1260,7 +1274,7 @@ static void bake_startjob(void *bkv, short *UNUSED(stop), short *do_update, floa
 
        if (bkr->is_selected_to_active) {
                bkr->result = bake(
-                       bkr->render, bkr->main, bkr->scene, bkr->ob, &bkr->selected_objects, bkr->reports,
+                       bkr->render, bkr->main, bkr->scene, bkr->view_layer, bkr->ob, &bkr->selected_objects, bkr->reports,
                        bkr->pass_type, bkr->pass_filter, bkr->margin, bkr->save_mode,
                        bkr->is_clear, bkr->is_split_materials, bkr->is_automatic_name, true, bkr->is_cage,
                        bkr->cage_extrusion, bkr->normal_space, bkr->normal_swizzle,
@@ -1273,7 +1287,7 @@ static void bake_startjob(void *bkv, short *UNUSED(stop), short *do_update, floa
                for (link = bkr->selected_objects.first; link; link = link->next) {
                        Object *ob_iter = link->ptr.data;
                        bkr->result = bake(
-                               bkr->render, bkr->main, bkr->scene, ob_iter, NULL, bkr->reports,
+                               bkr->render, bkr->main, bkr->scene, bkr->view_layer, ob_iter, NULL, bkr->reports,
                                bkr->pass_type, bkr->pass_filter, bkr->margin, bkr->save_mode,
                                is_clear, bkr->is_split_materials, bkr->is_automatic_name, false, bkr->is_cage,
                                bkr->cage_extrusion, bkr->normal_space, bkr->normal_swizzle,