Cleanup: Use const variables for object's evaluated mesh

Generally the evaluated mesh should not be changed, since that is the
job of the modifier stack. Current code is far from const correct in
that regard. This commit uses a const variable for the reult of
`BKE_object_get_evaluated_mesh` in some cases. The most common
remaining case is retrieving a BVH tree from the mesh.
This commit is contained in:
Hans Goudey 2021-07-01 23:03:09 -05:00
parent 016a2707f5
commit 9f5c0ffb5e
13 changed files with 42 additions and 39 deletions

View File

@ -243,7 +243,7 @@ void BKE_object_dimensions_set(struct Object *ob, const float value[3], int axis
void BKE_object_empty_draw_type_set(struct Object *ob, const int value);
void BKE_object_boundbox_flag(struct Object *ob, int flag, const bool set);
void BKE_object_boundbox_calc_from_mesh(struct Object *ob, struct Mesh *me_eval);
void BKE_object_boundbox_calc_from_mesh(struct Object *ob, const struct Mesh *me_eval);
void BKE_object_minmax(struct Object *ob, float r_min[3], float r_max[3], const bool use_hidden);
bool BKE_object_minmax_dupli(struct Depsgraph *depsgraph,
struct Scene *scene,

View File

@ -470,7 +470,7 @@ static void contarget_get_mesh_mat(Object *ob, const char *substring, float mat[
/* when not in EditMode, use the 'final' evaluated mesh, depsgraph
* ensures we build with CD_MDEFORMVERT layer
*/
Mesh *me_eval = BKE_object_get_evaluated_mesh(ob);
const Mesh *me_eval = BKE_object_get_evaluated_mesh(ob);
BMEditMesh *em = BKE_editmesh_from_object(ob);
float plane[3];
float imat[3][3], tmat[3][3];
@ -488,17 +488,17 @@ static void contarget_get_mesh_mat(Object *ob, const char *substring, float mat[
float normal[3] = {0.0f, 0.0f, 0.0f};
float weightsum = 0.0f;
if (me_eval) {
MDeformVert *dvert = CustomData_get_layer(&me_eval->vdata, CD_MDEFORMVERT);
const MDeformVert *dvert = CustomData_get_layer(&me_eval->vdata, CD_MDEFORMVERT);
int numVerts = me_eval->totvert;
/* check that dvert is a valid pointers (just in case) */
if (dvert) {
MDeformVert *dv = dvert;
MVert *mv = me_eval->mvert;
/* get the average of all verts with that are in the vertex-group */
for (int i = 0; i < numVerts; i++, dv++, mv++) {
MDeformWeight *dw = BKE_defvert_find_index(dv, defgroup);
for (int i = 0; i < numVerts; i++) {
const MDeformVert *dv = &dvert[i];
const MVert *mv = &me_eval->mvert[i];
const MDeformWeight *dw = BKE_defvert_find_index(dv, defgroup);
if (dw && dw->weight > 0.0f) {
float nor[3];

View File

@ -1694,7 +1694,7 @@ static void boundbox_displist_object(Object *ob)
ob->runtime.bb = (BoundBox *)MEM_callocN(sizeof(BoundBox), __func__);
}
Mesh *mesh_eval = BKE_object_get_evaluated_mesh(ob);
const Mesh *mesh_eval = BKE_object_get_evaluated_mesh(ob);
if (mesh_eval) {
BKE_object_boundbox_calc_from_mesh(ob, mesh_eval);
}

View File

@ -716,7 +716,7 @@ int get_effector_data(EffectorCache *eff,
}
else if (eff->pd && eff->pd->shape == PFIELD_SHAPE_POINTS) {
/* TODO: hair and points object support */
Mesh *me_eval = BKE_object_get_evaluated_mesh(eff->ob);
const Mesh *me_eval = BKE_object_get_evaluated_mesh(eff->ob);
if (me_eval != NULL) {
copy_v3_v3(efd->loc, me_eval->mvert[*efd->index].co);
normal_short_to_float_v3(efd->nor, me_eval->mvert[*efd->index].no);
@ -830,7 +830,7 @@ static void get_effector_tot(
if (eff->pd->shape == PFIELD_SHAPE_POINTS) {
/* TODO: hair and points object support */
Mesh *me_eval = BKE_object_get_evaluated_mesh(eff->ob);
const Mesh *me_eval = BKE_object_get_evaluated_mesh(eff->ob);
*tot = me_eval != NULL ? me_eval->totvert : 1;
if (*tot && eff->pd->forcefield == PFIELD_HARMONIC && point->index >= 0) {

View File

@ -2480,9 +2480,9 @@ bool BKE_gpencil_convert_mesh(Main *bmain,
/* Use evaluated data to get mesh with all modifiers on top. */
Object *ob_eval = (Object *)DEG_get_evaluated_object(depsgraph, ob_mesh);
Mesh *me_eval = BKE_object_get_evaluated_mesh(ob_eval);
MPoly *mp, *mpoly = me_eval->mpoly;
MLoop *mloop = me_eval->mloop;
const Mesh *me_eval = BKE_object_get_evaluated_mesh(ob_eval);
const MPoly *mpoly = me_eval->mpoly;
const MLoop *mloop = me_eval->mloop;
int mpoly_len = me_eval->totpoly;
char element_name[200];
@ -2515,8 +2515,9 @@ bool BKE_gpencil_convert_mesh(Main *bmain,
bGPDframe *gpf_fill = BKE_gpencil_layer_frame_get(
gpl_fill, CFRA + frame_offset, GP_GETFRAME_ADD_NEW);
int i;
for (i = 0, mp = mpoly; i < mpoly_len; i++, mp++) {
MLoop *ml = &mloop[mp->loopstart];
for (i = 0; i < mpoly_len; i++) {
const MPoly *mp = &mpoly[i];
/* Find material. */
int mat_idx = 0;
Material *ma = BKE_object_material_get(ob_mesh, mp->mat_nr + 1);
@ -2539,8 +2540,10 @@ bool BKE_gpencil_convert_mesh(Main *bmain,
gps_fill->flag |= GP_STROKE_CYCLIC;
/* Add points to strokes. */
for (int j = 0; j < mp->totloop; j++, ml++) {
MVert *mv = &me_eval->mvert[ml->v];
for (int j = 0; j < mp->totloop; j++) {
const MLoop *ml = &mloop[mp->loopstart + j];
const MVert *mv = &me_eval->mvert[ml->v];
bGPDspoint *pt = &gps_fill->points[j];
copy_v3_v3(&pt->x, mv->co);
mul_m4_v3(matrix, &pt->x);

View File

@ -3860,7 +3860,7 @@ void BKE_object_boundbox_flag(Object *ob, int flag, const bool set)
}
}
void BKE_object_boundbox_calc_from_mesh(struct Object *ob, struct Mesh *me_eval)
void BKE_object_boundbox_calc_from_mesh(struct Object *ob, const struct Mesh *me_eval)
{
float min[3], max[3];
@ -4169,7 +4169,7 @@ void BKE_object_foreach_display_point(Object *ob,
void *user_data)
{
/* TODO: pointcloud and hair objects support */
Mesh *mesh_eval = BKE_object_get_evaluated_mesh(ob);
const Mesh *mesh_eval = BKE_object_get_evaluated_mesh(ob);
float co[3];
if (mesh_eval != NULL) {

View File

@ -335,14 +335,14 @@ static void make_child_duplis(const DupliContext *ctx,
/** \name Internal Data Access Utilities
* \{ */
static Mesh *mesh_data_from_duplicator_object(Object *ob,
BMEditMesh **r_em,
const float (**r_vert_coords)[3],
const float (**r_vert_normals)[3])
static const Mesh *mesh_data_from_duplicator_object(Object *ob,
BMEditMesh **r_em,
const float (**r_vert_coords)[3],
const float (**r_vert_normals)[3])
{
/* Gather mesh info. */
BMEditMesh *em = BKE_editmesh_from_object(ob);
Mesh *me_eval;
const Mesh *me_eval;
*r_em = nullptr;
*r_vert_coords = nullptr;
@ -603,7 +603,7 @@ static void make_duplis_verts(const DupliContext *ctx)
BMEditMesh *em = nullptr;
const float(*vert_coords)[3] = nullptr;
const float(*vert_normals)[3] = nullptr;
Mesh *me_eval = mesh_data_from_duplicator_object(
const Mesh *me_eval = mesh_data_from_duplicator_object(
parent, &em, &vert_coords, use_rotation ? &vert_normals : nullptr);
if (em == nullptr && me_eval == nullptr) {
return;
@ -1151,7 +1151,7 @@ static void make_duplis_faces(const DupliContext *ctx)
/* Gather mesh info. */
BMEditMesh *em = nullptr;
const float(*vert_coords)[3] = nullptr;
Mesh *me_eval = mesh_data_from_duplicator_object(parent, &em, &vert_coords, nullptr);
const Mesh *me_eval = mesh_data_from_duplicator_object(parent, &em, &vert_coords, nullptr);
if (em == nullptr && me_eval == nullptr) {
return;
}

View File

@ -219,7 +219,7 @@ struct GPUVertBuf *DRW_mesh_batch_cache_pos_vertbuf_get(struct Mesh *me);
struct GPUVertBuf *DRW_curve_batch_cache_pos_vertbuf_get(struct Curve *cu);
struct GPUVertBuf *DRW_mball_batch_cache_pos_vertbuf_get(struct Object *ob);
int DRW_mesh_material_count_get(struct Mesh *me);
int DRW_mesh_material_count_get(const struct Mesh *me);
/* See 'common_globals_lib.glsl' for duplicate defines. */

View File

@ -1052,7 +1052,7 @@ GPUBatch *DRW_mesh_batch_cache_get_surface_sculpt(Mesh *me)
return cache->batch.surface;
}
int DRW_mesh_material_count_get(Mesh *me)
int DRW_mesh_material_count_get(const Mesh *me)
{
return mesh_render_mat_len_get(me);
}

View File

@ -59,7 +59,7 @@ static LinkNode *knifeproject_poly_from_object(const bContext *C,
{
Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
ARegion *region = CTX_wm_region(C);
struct Mesh *me_eval;
const struct Mesh *me_eval;
bool me_eval_needs_free;
if (ob->type == OB_MESH || ob->runtime.data_eval) {
@ -113,7 +113,7 @@ static LinkNode *knifeproject_poly_from_object(const bContext *C,
BKE_nurbList_free(&nurbslist);
if (me_eval_needs_free) {
BKE_mesh_free(me_eval);
BKE_mesh_free((struct Mesh *)me_eval);
}
}

View File

@ -179,7 +179,7 @@ static MDeformVert *defweight_prev_init(MDeformVert *dvert_prev,
* (without evaluating modifiers) */
static bool vertex_paint_use_fast_update_check(Object *ob)
{
Mesh *me_eval = BKE_object_get_evaluated_mesh(ob);
const Mesh *me_eval = BKE_object_get_evaluated_mesh(ob);
if (me_eval != NULL) {
Mesh *me = BKE_mesh_from_object(ob);

View File

@ -96,7 +96,7 @@ typedef struct SceneStatsFmt {
char totgpstroke[MAX_INFO_NUM_LEN], totgppoint[MAX_INFO_NUM_LEN];
} SceneStatsFmt;
static bool stats_mesheval(Mesh *me_eval, bool is_selected, SceneStats *stats)
static bool stats_mesheval(const Mesh *me_eval, bool is_selected, SceneStats *stats)
{
if (me_eval == NULL) {
return false;
@ -149,8 +149,8 @@ static void stats_object(Object *ob,
switch (ob->type) {
case OB_MESH: {
/* we assume evaluated mesh is already built, this strictly does stats now. */
Mesh *me_eval = BKE_object_get_evaluated_mesh(ob);
if (!BLI_gset_add(objects_gset, me_eval)) {
const Mesh *me_eval = BKE_object_get_evaluated_mesh(ob);
if (!BLI_gset_add(objects_gset, (void *)me_eval)) {
break;
}
stats_mesheval(me_eval, is_selected, stats);
@ -165,8 +165,8 @@ static void stats_object(Object *ob,
case OB_SURF:
case OB_CURVE:
case OB_FONT: {
Mesh *me_eval = BKE_object_get_evaluated_mesh(ob);
if ((me_eval != NULL) && !BLI_gset_add(objects_gset, me_eval)) {
const Mesh *me_eval = BKE_object_get_evaluated_mesh(ob);
if ((me_eval != NULL) && !BLI_gset_add(objects_gset, (void *)me_eval)) {
break;
}

View File

@ -117,10 +117,10 @@ void ED_draw_object_facemap(Depsgraph *depsgraph,
return;
}
Mesh *me = ob->data;
const Mesh *me = ob->data;
{
Object *ob_eval = DEG_get_evaluated_object(depsgraph, ob);
Mesh *me_eval = BKE_object_get_evaluated_mesh(ob_eval);
const Mesh *me_eval = BKE_object_get_evaluated_mesh(ob_eval);
if (me_eval != NULL) {
me = me_eval;
}
@ -160,7 +160,7 @@ void ED_draw_object_facemap(Depsgraph *depsgraph,
const MPoly *mp;
int i;
if (me->runtime.looptris.array) {
MLoopTri *mlt = me->runtime.looptris.array;
const MLoopTri *mlt = me->runtime.looptris.array;
for (mp = mpoly, i = 0; i < mpoly_len; i++, mp++) {
if (facemap_data[i] == facemap) {
for (int j = 2; j < mp->totloop; j++) {