diff options
author | Hans Goudey <h.goudey@me.com> | 2022-09-05 19:56:34 +0300 |
---|---|---|
committer | Hans Goudey <h.goudey@me.com> | 2022-09-05 19:56:34 +0300 |
commit | 05952aa94d33eeb504fa63618ba35c2bcc8bd19b (patch) | |
tree | c9ec37adf20c3c37ccaab44869220dcbe8e987a3 /source/blender/editors/space_view3d | |
parent | 63cfc8f9f6d623f33b50c5c07976af2b22845713 (diff) |
Mesh: Remove redundant custom data pointers
For copy-on-write, we want to share attribute arrays between meshes
where possible. Mutable pointers like `Mesh.mvert` make that difficult
by making ownership vague. They also make code more complex by adding
redundancy.
The simplest solution is just removing them and retrieving layers from
`CustomData` as needed. Similar changes have already been applied to
curves and point clouds (e9f82d3dc7ee, 410a6efb747f). Removing use of
the pointers generally makes code more obvious and more reusable.
Mesh data is now accessed with a C++ API (`Mesh::edges()` or
`Mesh::edges_for_write()`), and a C API (`BKE_mesh_edges(mesh)`).
The CoW changes this commit makes possible are described in T95845
and T95842, and started in D14139 and D14140. The change also simplifies
the ongoing mesh struct-of-array refactors from T95965.
**RNA/Python Access Performance**
Theoretically, accessing mesh elements with the RNA API may become
slower, since the layer needs to be found on every random access.
However, overhead is already high enough that this doesn't make a
noticible differenc, and performance is actually improved in some
cases. Random access can be up to 10% faster, but other situations
might be a bit slower. Generally using `foreach_get/set` are the best
way to improve performance. See the differential revision for more
discussion about Python performance.
Cycles has been updated to use raw pointers and the internal Blender
mesh types, mostly because there is no sense in having this overhead
when it's already compiled with Blender. In my tests this roughly
halves the Cycles mesh creation time (0.19s to 0.10s for a 1 million
face grid).
Differential Revision: https://developer.blender.org/D15488
Diffstat (limited to 'source/blender/editors/space_view3d')
-rw-r--r-- | source/blender/editors/space_view3d/drawobject.c | 25 | ||||
-rw-r--r-- | source/blender/editors/space_view3d/view3d_iterators.c | 5 | ||||
-rw-r--r-- | source/blender/editors/space_view3d/view3d_select.cc | 21 |
3 files changed, 29 insertions, 22 deletions
diff --git a/source/blender/editors/space_view3d/drawobject.c b/source/blender/editors/space_view3d/drawobject.c index 8add6886584..2019ff28e33 100644 --- a/source/blender/editors/space_view3d/drawobject.c +++ b/source/blender/editors/space_view3d/drawobject.c @@ -16,6 +16,7 @@ #include "BKE_customdata.h" #include "BKE_editmesh.h" #include "BKE_global.h" +#include "BKE_mesh.h" #include "BKE_object.h" #include "DEG_depsgraph.h" @@ -67,9 +68,9 @@ void ED_draw_object_facemap(Depsgraph *depsgraph, if (facemap_data) { GPU_blend(GPU_BLEND_ALPHA); - const MVert *mvert = me->mvert; - const MPoly *mpoly = me->mpoly; - const MLoop *mloop = me->mloop; + const MVert *verts = BKE_mesh_vertices(me); + const MPoly *polys = BKE_mesh_polygons(me); + const MLoop *loops = BKE_mesh_loops(me); int mpoly_len = me->totpoly; int mloop_len = me->totloop; @@ -95,12 +96,12 @@ void ED_draw_object_facemap(Depsgraph *depsgraph, int i; if (me->runtime.looptris.array) { const MLoopTri *mlt = me->runtime.looptris.array; - for (mp = mpoly, i = 0; i < mpoly_len; i++, mp++) { + for (mp = polys, i = 0; i < mpoly_len; i++, mp++) { if (facemap_data[i] == facemap) { for (int j = 2; j < mp->totloop; j++) { - copy_v3_v3(GPU_vertbuf_raw_step(&pos_step), mvert[mloop[mlt->tri[0]].v].co); - copy_v3_v3(GPU_vertbuf_raw_step(&pos_step), mvert[mloop[mlt->tri[1]].v].co); - copy_v3_v3(GPU_vertbuf_raw_step(&pos_step), mvert[mloop[mlt->tri[2]].v].co); + copy_v3_v3(GPU_vertbuf_raw_step(&pos_step), verts[loops[mlt->tri[0]].v].co); + copy_v3_v3(GPU_vertbuf_raw_step(&pos_step), verts[loops[mlt->tri[1]].v].co); + copy_v3_v3(GPU_vertbuf_raw_step(&pos_step), verts[loops[mlt->tri[2]].v].co); vbo_len_used += 3; mlt++; } @@ -112,15 +113,15 @@ void ED_draw_object_facemap(Depsgraph *depsgraph, } else { /* No tessellation data, fan-fill. */ - for (mp = mpoly, i = 0; i < mpoly_len; i++, mp++) { + for (mp = polys, i = 0; i < mpoly_len; i++, mp++) { if (facemap_data[i] == facemap) { - const MLoop *ml_start = &mloop[mp->loopstart]; + const MLoop *ml_start = &loops[mp->loopstart]; const MLoop *ml_a = ml_start + 1; const MLoop *ml_b = ml_start + 2; for (int j = 2; j < mp->totloop; j++) { - copy_v3_v3(GPU_vertbuf_raw_step(&pos_step), mvert[ml_start->v].co); - copy_v3_v3(GPU_vertbuf_raw_step(&pos_step), mvert[ml_a->v].co); - copy_v3_v3(GPU_vertbuf_raw_step(&pos_step), mvert[ml_b->v].co); + copy_v3_v3(GPU_vertbuf_raw_step(&pos_step), verts[ml_start->v].co); + copy_v3_v3(GPU_vertbuf_raw_step(&pos_step), verts[ml_a->v].co); + copy_v3_v3(GPU_vertbuf_raw_step(&pos_step), verts[ml_b->v].co); vbo_len_used += 3; ml_a++; diff --git a/source/blender/editors/space_view3d/view3d_iterators.c b/source/blender/editors/space_view3d/view3d_iterators.c index 6256eeb9621..4d5068c45ad 100644 --- a/source/blender/editors/space_view3d/view3d_iterators.c +++ b/source/blender/editors/space_view3d/view3d_iterators.c @@ -23,6 +23,7 @@ #include "BKE_curve.h" #include "BKE_displist.h" #include "BKE_editmesh.h" +#include "BKE_mesh.h" #include "BKE_mesh_iterators.h" #include "BKE_mesh_runtime.h" #include "BKE_mesh_wrapper.h" @@ -205,6 +206,7 @@ typedef struct foreachScreenObjectVert_userData { void (*func)(void *userData, MVert *mv, const float screen_co[2], int index); void *userData; ViewContext vc; + MVert *verts; const bool *hide_vert; eV3DProjTest clip_flag; } foreachScreenObjectVert_userData; @@ -266,7 +268,7 @@ static void meshobject_foreachScreenVert__mapFunc(void *userData, if (data->hide_vert && data->hide_vert[index]) { return; } - struct MVert *mv = &((Mesh *)(data->vc.obact->data))->mvert[index]; + MVert *mv = &data->verts[index]; float screen_co[2]; @@ -299,6 +301,7 @@ void meshobject_foreachScreenVert( data.func = func; data.userData = userData; data.clip_flag = clip_flag; + data.verts = BKE_mesh_vertices_for_write((Mesh *)vc->obact->data); data.hide_vert = (const bool *)CustomData_get_layer_named( &me->vdata, CD_PROP_BOOL, ".hide_vert"); diff --git a/source/blender/editors/space_view3d/view3d_select.cc b/source/blender/editors/space_view3d/view3d_select.cc index f757cb45eec..714eff79777 100644 --- a/source/blender/editors/space_view3d/view3d_select.cc +++ b/source/blender/editors/space_view3d/view3d_select.cc @@ -334,7 +334,8 @@ static bool edbm_backbuf_check_and_select_verts_obmode(Mesh *me, EditSelectBuf_Cache *esel, const eSelectOp sel_op) { - MVert *mv = me->mvert; + MVert *verts = BKE_mesh_vertices_for_write(me); + MVert *mv = verts; bool changed = false; const BLI_bitmap *select_bitmap = esel->select_bitmap; @@ -363,22 +364,22 @@ static bool edbm_backbuf_check_and_select_faces_obmode(Mesh *me, EditSelectBuf_Cache *esel, const eSelectOp sel_op) { - MPoly *mpoly = me->mpoly; + MPoly *polygons = BKE_mesh_polygons_for_write(me); bool changed = false; const BLI_bitmap *select_bitmap = esel->select_bitmap; - if (mpoly) { + if (polygons) { const bool *hide_poly = (const bool *)CustomData_get_layer_named( - &me->pdata, CD_PROP_BOOL, ".hide_poly"); + &me->vdata, CD_PROP_BOOL, ".hide_poly"); - for (int index = 0; index < me->totpoly; index++, mpoly++) { + for (int index = 0; index < me->totpoly; index++) { if (!(hide_poly && hide_poly[index])) { - const bool is_select = mpoly->flag & ME_FACE_SEL; + const bool is_select = polygons[index].flag & ME_FACE_SEL; const bool is_inside = BLI_BITMAP_TEST_BOOL(select_bitmap, index); const int sel_op_result = ED_select_op_action_deselected(sel_op, is_select, is_inside); if (sel_op_result != -1) { - SET_FLAG_FROM_TEST(mpoly->flag, sel_op_result, ME_FACE_SEL); + SET_FLAG_FROM_TEST(polygons[index].flag, sel_op_result, ME_FACE_SEL); changed = true; } } @@ -2813,13 +2814,15 @@ static bool ed_wpaint_vertex_select_pick(bContext *C, Mesh *me = static_cast<Mesh *>(obact->data); /* already checked for nullptr */ uint index = 0; + MVert *verts = BKE_mesh_vertices_for_write(me); + MVert *mv; bool changed = false; bool found = ED_mesh_pick_vert(C, obact, mval, ED_MESH_PICK_DEFAULT_VERT_DIST, use_zbuf, &index); if (params->sel_op == SEL_OP_SET) { - if ((found && params->select_passthrough) && (me->mvert[index].flag & SELECT)) { + if ((found && params->select_passthrough) && (verts[index].flag & SELECT)) { found = false; } else if (found || params->deselect_all) { @@ -2829,7 +2832,7 @@ static bool ed_wpaint_vertex_select_pick(bContext *C, } if (found) { - mv = &me->mvert[index]; + mv = &verts[index]; switch (params->sel_op) { case SEL_OP_ADD: { mv->flag |= SELECT; |