Mesh: Remove unnecessary mesh position copying #105756

Merged
Hans Goudey merged 6 commits from sixthat/blender:remove-unneccessary-vert-pos-copy into main 2023-03-19 00:26:41 +01:00
3 changed files with 29 additions and 48 deletions
Showing only changes of commit 4587643a57 - Show all commits

View File

@ -743,11 +743,8 @@ static GeometrySet curve_calc_modifiers_post(Depsgraph *depsgraph,
Mesh *mesh = geometry_set.get_mesh_for_write();
if (mti->type == eModifierTypeType_OnlyDeform) {
int totvert;
float(*vertex_coords)[3] = BKE_mesh_vert_coords_alloc(mesh, &totvert);
mti->deformVerts(md, &mectx_deform, mesh, vertex_coords, totvert);
BKE_mesh_vert_coords_apply(mesh, vertex_coords);
MEM_freeN(vertex_coords);
mti->deformVerts(
md, &mectx_deform, mesh, BKE_mesh_vert_positions_for_write(mesh), mesh->totvert);
sixthat marked this conversation as resolved
Review

This needs a call to BKE_mesh_tag_positions_changed, otherwise various caches that depend on mesh positions won't be properly updated after the deformation.

This needs a call to `BKE_mesh_tag_positions_changed`, otherwise various caches that depend on mesh positions won't be properly updated after the deformation.
}
else {
Mesh *output_mesh = mti->modifyMesh(md, &mectx_apply, mesh);

View File

@ -685,11 +685,8 @@ void BKE_mball_data_update(Depsgraph *depsgraph, Scene *scene, Object *ob)
mesh->totcol = mball->totcol;
if (ob->parent && ob->parent->type == OB_LATTICE && ob->partype == PARSKEL) {
int verts_num;
float(*positions)[3] = BKE_mesh_vert_coords_alloc(mesh, &verts_num);
BKE_lattice_deform_coords(ob->parent, ob, positions, verts_num, 0, nullptr, 1.0f);
BKE_mesh_vert_coords_apply(mesh, positions);
MEM_freeN(positions);
BKE_lattice_deform_coords(
ob->parent, ob, BKE_mesh_vert_positions_for_write(mesh), mesh->totvert, 0, nullptr, 1.0f);
}
ob->runtime.geometry_set_eval = new GeometrySet(GeometrySet::create_with_mesh(mesh));

View File

@ -262,9 +262,8 @@ void BKE_mesh_remap_find_best_match_from_mesh(const float (*vert_positions_dst)[
float best_match = FLT_MAX, match;
const int numverts_src = me_src->totvert;
float(*vcos_src)[3] = BKE_mesh_vert_coords_alloc(me_src, nullptr);
mesh_calc_eigen_matrix(nullptr, (const float(*)[3])vcos_src, numverts_src, mat_src);
const float(*vcos_src)[3] = BKE_mesh_vert_positions(me_src);
mesh_calc_eigen_matrix(nullptr, vcos_src, numverts_src, mat_src);
mesh_calc_eigen_matrix(vert_positions_dst, nullptr, numverts_dst, mat_dst);
BLI_space_transform_global_from_matrices(r_space_transform, mat_dst, mat_src);
@ -289,8 +288,6 @@ void BKE_mesh_remap_find_best_match_from_mesh(const float (*vert_positions_dst)[
}
BLI_space_transform_global_from_matrices(r_space_transform, best_mat_dst, mat_src);
MEM_freeN(vcos_src);
}
/** \} */
@ -516,7 +513,7 @@ void BKE_mesh_remap_calc_verts_from_mesh(const int mode,
}
else if (ELEM(mode, MREMAP_MODE_VERT_EDGE_NEAREST, MREMAP_MODE_VERT_EDGEINTERP_NEAREST)) {
const blender::Span<MEdge> edges_src = me_src->edges();
float(*vcos_src)[3] = BKE_mesh_vert_coords_alloc(me_src, nullptr);
const float(*vcos_src)[3] = BKE_mesh_vert_positions(me_src);
BKE_bvhtree_from_mesh_get(&treedata, me_src, BVHTREE_FROM_EDGES, 2);
nearest.index = -1;
@ -561,8 +558,6 @@ void BKE_mesh_remap_calc_verts_from_mesh(const int mode,
BKE_mesh_remap_item_define_invalid(r_map, i);
}
}
MEM_freeN(vcos_src);
}
else if (ELEM(mode,
MREMAP_MODE_VERT_POLY_NEAREST,
@ -570,7 +565,7 @@ void BKE_mesh_remap_calc_verts_from_mesh(const int mode,
MREMAP_MODE_VERT_POLYINTERP_VNORPROJ)) {
const blender::Span<MPoly> polys_src = me_src->polys();
const blender::Span<MLoop> loops_src = me_src->loops();
float(*vcos_src)[3] = BKE_mesh_vert_coords_alloc(me_src, nullptr);
const float(*vcos_src)[3] = BKE_mesh_vert_positions(me_src);
const float(*vert_normals_dst)[3] = BKE_mesh_vert_normals_ensure(me_dst);
size_t tmp_buff_size = MREMAP_DEFAULT_BUFSIZE;
@ -598,7 +593,7 @@ void BKE_mesh_remap_calc_verts_from_mesh(const int mode,
const MLoopTri *lt = &treedata.looptri[rayhit.index];
const int sources_num = mesh_remap_interp_poly_data_get(polys_src[lt->poly],
loops_src,
(const float(*)[3])vcos_src,
vcos_src,
rayhit.co,
&tmp_buff_size,
&vcos,
@ -635,7 +630,7 @@ void BKE_mesh_remap_calc_verts_from_mesh(const int mode,
int index;
mesh_remap_interp_poly_data_get(polys_src[lt->poly],
loops_src,
(const float(*)[3])vcos_src,
vcos_src,
nearest.co,
&tmp_buff_size,
&vcos,
@ -650,7 +645,7 @@ void BKE_mesh_remap_calc_verts_from_mesh(const int mode,
else if (mode == MREMAP_MODE_VERT_POLYINTERP_NEAREST) {
const int sources_num = mesh_remap_interp_poly_data_get(polys_src[lt->poly],
loops_src,
(const float(*)[3])vcos_src,
vcos_src,
nearest.co,
&tmp_buff_size,
&vcos,
@ -670,7 +665,6 @@ void BKE_mesh_remap_calc_verts_from_mesh(const int mode,
}
}
MEM_freeN(vcos_src);
MEM_freeN(vcos);
MEM_freeN(indices);
MEM_freeN(weights);
@ -721,7 +715,7 @@ void BKE_mesh_remap_calc_edges_from_mesh(const int mode,
if (mode == MREMAP_MODE_EDGE_VERT_NEAREST) {
const int num_verts_src = me_src->totvert;
const blender::Span<MEdge> edges_src = me_src->edges();
float(*vcos_src)[3] = BKE_mesh_vert_coords_alloc(me_src, nullptr);
const float(*vcos_src)[3] = BKE_mesh_vert_positions(me_src);
MeshElemMap *vert_to_edge_src_map;
int *vert_to_edge_src_map_mem;
@ -840,7 +834,6 @@ void BKE_mesh_remap_calc_edges_from_mesh(const int mode,
}
}
MEM_freeN(vcos_src);
MEM_freeN(v_dst_to_src_map);
MEM_freeN(vert_to_edge_src_map);
MEM_freeN(vert_to_edge_src_map_mem);
@ -874,7 +867,7 @@ void BKE_mesh_remap_calc_edges_from_mesh(const int mode,
const blender::Span<MEdge> edges_src = me_src->edges();
const blender::Span<MPoly> polys_src = me_src->polys();
const blender::Span<MLoop> loops_src = me_src->loops();
float(*vcos_src)[3] = BKE_mesh_vert_coords_alloc(me_src, nullptr);
const float(*vcos_src)[3] = BKE_mesh_vert_positions(me_src);
BKE_bvhtree_from_mesh_get(&treedata, me_src, BVHTREE_FROM_LOOPTRI, 2);
@ -900,8 +893,8 @@ void BKE_mesh_remap_calc_edges_from_mesh(const int mode,
for (; nloops--; ml_src++) {
const MEdge *edge_src = &edges_src[ml_src->e];
float *co1_src = vcos_src[edge_src->v1];
float *co2_src = vcos_src[edge_src->v2];
const float *co1_src = vcos_src[edge_src->v1];
const float *co2_src = vcos_src[edge_src->v2];
float co_src[3];
float dist_sq;
@ -921,8 +914,6 @@ void BKE_mesh_remap_calc_edges_from_mesh(const int mode,
BKE_mesh_remap_item_define_invalid(r_map, i);
}
}
MEM_freeN(vcos_src);
}
else if (mode == MREMAP_MODE_EDGE_EDGEINTERP_VNORPROJ) {
const int num_rays_min = 5, num_rays_max = 100;
@ -1305,7 +1296,7 @@ void BKE_mesh_remap_calc_loops_from_mesh(const int mode,
const float(*positions_src)[3] = BKE_mesh_vert_positions(me_src);
const int num_verts_src = me_src->totvert;
float(*vcos_src)[3] = nullptr;
const float(*vcos_src)[3] = nullptr;
const blender::Span<MEdge> edges_src = me_src->edges();
const blender::Span<MPoly> polys_src = me_src->polys();
const blender::Span<MLoop> loops_src = me_src->loops();
@ -1325,7 +1316,7 @@ void BKE_mesh_remap_calc_loops_from_mesh(const int mode,
size_t islands_res_buff_size = MREMAP_DEFAULT_BUFSIZE;
if (!use_from_vert) {
vcos_src = BKE_mesh_vert_coords_alloc(me_src, nullptr);
vcos_src = BKE_mesh_vert_positions(me_src);
vcos_interp = static_cast<float(*)[3]>(
MEM_mallocN(sizeof(*vcos_interp) * buff_size_interp, __func__));
@ -2037,7 +2028,7 @@ void BKE_mesh_remap_calc_loops_from_mesh(const int mode,
if (mode == MREMAP_MODE_LOOP_POLY_NEAREST) {
mesh_remap_interp_poly_data_get(poly,
loops_src,
(const float(*)[3])vcos_src,
vcos_src,
hit_co,
&buff_size_interp,
&vcos_interp,
@ -2056,18 +2047,17 @@ void BKE_mesh_remap_calc_loops_from_mesh(const int mode,
&full_weight);
}
else {
const int sources_num = mesh_remap_interp_poly_data_get(
poly,
loops_src,
(const float(*)[3])vcos_src,
hit_co,
&buff_size_interp,
&vcos_interp,
true,
&indices_interp,
&weights_interp,
true,
nullptr);
const int sources_num = mesh_remap_interp_poly_data_get(poly,
loops_src,
vcos_src,
hit_co,
&buff_size_interp,
&vcos_interp,
true,
&indices_interp,
&weights_interp,
true,
nullptr);
mesh_remap_item_define(r_map,
lidx_dst,
@ -2109,9 +2099,6 @@ void BKE_mesh_remap_calc_loops_from_mesh(const int mode,
BLI_astar_solution_free(&as_solution);
}
if (vcos_src) {
MEM_freeN(vcos_src);
}
if (vert_to_loop_map_src) {
MEM_freeN(vert_to_loop_map_src);
}