From a81cc5cbcb6c9e9ef4bee610ac1d325d33884e98 Mon Sep 17 00:00:00 2001 From: Hans Goudey Date: Mon, 21 Feb 2022 11:40:59 -0500 Subject: Fix: Avoid potential use of dirty normals Instead of accessing the `CD_NORMAL` layer directly, use the proper API for accessing mesh normals. Even if the layer exists, the values might be incorrect due to a deformation. Related to ef0e21f0ae71d, 969c4a45ce09100e, and T95839. --- source/blender/python/mathutils/mathutils_bvhtree.c | 6 ++---- source/blender/render/intern/bake.c | 4 +++- source/blender/render/intern/multires_bake.c | 6 +++--- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/source/blender/python/mathutils/mathutils_bvhtree.c b/source/blender/python/mathutils/mathutils_bvhtree.c index 588d3753eab..28687ce2d13 100644 --- a/source/blender/python/mathutils/mathutils_bvhtree.c +++ b/source/blender/python/mathutils/mathutils_bvhtree.c @@ -1180,10 +1180,8 @@ static PyObject *C_BVHTree_FromObject(PyObject *UNUSED(cls), PyObject *args, PyO tree = BLI_bvhtree_new((int)tris_len, epsilon, PY_BVH_TREE_TYPE_DEFAULT, PY_BVH_AXIS_DEFAULT); if (tree) { orig_index = MEM_mallocN(sizeof(*orig_index) * (size_t)tris_len, __func__); - CustomData *pdata = &mesh->pdata; - orig_normal = CustomData_get_layer(pdata, CD_NORMAL); /* can be NULL */ - if (orig_normal) { - orig_normal = MEM_dupallocN(orig_normal); + if (!BKE_mesh_poly_normals_are_dirty(mesh)) { + orig_normal = MEM_dupallocN(BKE_mesh_poly_normals_ensure(mesh)); } for (i = 0; i < tris_len; i++, lt++) { diff --git a/source/blender/render/intern/bake.c b/source/blender/render/intern/bake.c index 883e026472b..b11d9629e03 100644 --- a/source/blender/render/intern/bake.c +++ b/source/blender/render/intern/bake.c @@ -482,7 +482,9 @@ static TriTessFace *mesh_calc_tri_tessface(Mesh *me, bool tangent, Mesh *me_eval looptri = MEM_mallocN(sizeof(*looptri) * tottri, __func__); triangles = MEM_callocN(sizeof(TriTessFace) * tottri, __func__); - const float(*precomputed_normals)[3] = CustomData_get_layer(&me->pdata, CD_NORMAL); + const float(*precomputed_normals)[3] = BKE_mesh_poly_normals_are_dirty(me) ? + NULL : + BKE_mesh_poly_normals_ensure(me); const bool calculate_normal = precomputed_normals ? false : true; if (precomputed_normals != NULL) { diff --git a/source/blender/render/intern/multires_bake.c b/source/blender/render/intern/multires_bake.c index 9468e4c5b0f..73f925f5905 100644 --- a/source/blender/render/intern/multires_bake.c +++ b/source/blender/render/intern/multires_bake.c @@ -483,7 +483,6 @@ static void do_multires_bake(MultiresBakeRender *bkr, MPoly *mpoly = dm->getPolyArray(dm); MLoop *mloop = dm->getLoopArray(dm); MLoopUV *mloopuv = dm->getLoopDataArray(dm, CD_MLOOPUV); - const float *precomputed_normals = dm->getPolyDataArray(dm, CD_NORMAL); float *pvtangent = NULL; ListBase threads; @@ -498,6 +497,7 @@ static void do_multires_bake(MultiresBakeRender *bkr, memcpy(temp_mesh->mpoly, dm->getPolyArray(dm), temp_mesh->totpoly * sizeof(*temp_mesh->mpoly)); memcpy(temp_mesh->mloop, dm->getLoopArray(dm), temp_mesh->totloop * sizeof(*temp_mesh->mloop)); const float(*vert_normals)[3] = BKE_mesh_vertex_normals_ensure(temp_mesh); + const float(*poly_normals)[3] = BKE_mesh_poly_normals_ensure(temp_mesh); if (require_tangent) { if (CustomData_get_layer_index(&dm->loopData, CD_TANGENT) == -1) { @@ -513,7 +513,7 @@ static void do_multires_bake(MultiresBakeRender *bkr, NULL, 0, vert_normals, - (const float(*)[3])CustomData_get_layer(&dm->polyData, CD_NORMAL), + poly_normals, (const float(*)[3])dm->getLoopDataArray(dm, CD_NORMAL), (const float(*)[3])dm->getVertDataArray(dm, CD_ORCO), /* may be nullptr */ /* result */ @@ -558,7 +558,7 @@ static void do_multires_bake(MultiresBakeRender *bkr, handle->data.mlooptri = mlooptri; handle->data.mloop = mloop; handle->data.pvtangent = pvtangent; - handle->data.precomputed_normals = precomputed_normals; /* don't strictly need this */ + handle->data.precomputed_normals = (float *)poly_normals; /* don't strictly need this */ handle->data.w = ibuf->x; handle->data.h = ibuf->y; handle->data.lores_dm = dm; -- cgit v1.2.3