From d12fb5053c1e5d1030529fc924c890bd7d8f5534 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Mon, 28 Nov 2011 18:52:35 +0000 Subject: [PATCH] trust 'bm->elem_index_dirty' is correct, this avoids looping over edges/faces/verts if its not needed, but if set wrong it could crash too so added verification when built with debeg. also added debug define to help solve invalid values with valgrind's memcheck. gives noticeable speedup since there was a loop no all faces to set index values on redraw. --- source/blender/bmesh/intern/bmesh_mesh.c | 74 +++++++++++++-------- source/blender/bmesh/intern/bmesh_newcore.c | 33 +++++++++ 2 files changed, 80 insertions(+), 27 deletions(-) diff --git a/source/blender/bmesh/intern/bmesh_mesh.c b/source/blender/bmesh/intern/bmesh_mesh.c index 19a1fac3910..1162dbaf255 100644 --- a/source/blender/bmesh/intern/bmesh_mesh.c +++ b/source/blender/bmesh/intern/bmesh_mesh.c @@ -490,37 +490,53 @@ void BM_ElemIndex_Ensure(BMesh *bm, const char hflag) BMIter iter; BMHeader *ele; - /* TODO, mark arrays as dirty, only calculate if needed!, - * uncomment bm->elem_index_dirty checks */ +#ifdef DEBUG + BM_ELEM_INDEX_VALIDATE(bm, "Should Never Fail!", __func__); +#endif - if ((hflag & BM_VERT) /* && (bm->elem_index_dirty & BM_VERT) */) { - int index= 0; - BM_ITER(ele, &iter, bm, BM_VERTS_OF_MESH, NULL) { - BM_SetIndex(ele, index); /* set_ok */ - index++; + if (hflag & BM_VERT) { + if (bm->elem_index_dirty & BM_VERT) { + int index= 0; + BM_ITER(ele, &iter, bm, BM_VERTS_OF_MESH, NULL) { + BM_SetIndex(ele, index); /* set_ok */ + index++; + } + bm->elem_index_dirty &= ~BM_VERT; + BLI_assert(index == bm->totvert); + } + else { + // printf("%s: skipping vert index calc!\n", __func__); } - bm->elem_index_dirty &= ~BM_VERT; - BLI_assert(index == bm->totvert); } - if ((hflag & BM_EDGE) /* && (bm->elem_index_dirty & BM_EDGE) */) { - int index= 0; - BM_ITER(ele, &iter, bm, BM_EDGES_OF_MESH, NULL) { - BM_SetIndex(ele, index); /* set_ok */ - index++; + if (hflag & BM_EDGE) { + if (bm->elem_index_dirty & BM_EDGE) { + int index= 0; + BM_ITER(ele, &iter, bm, BM_EDGES_OF_MESH, NULL) { + BM_SetIndex(ele, index); /* set_ok */ + index++; + } + bm->elem_index_dirty &= ~BM_EDGE; + BLI_assert(index == bm->totedge); + } + else { + // printf("%s: skipping edge index calc!\n", __func__); } - bm->elem_index_dirty &= ~BM_EDGE; - BLI_assert(index == bm->totedge); } - if ((hflag & BM_FACE) /* && (bm->elem_index_dirty & BM_FACES) */) { - int index= 0; - BM_ITER(ele, &iter, bm, BM_FACES_OF_MESH, NULL) { - BM_SetIndex(ele, index); /* set_ok */ - index++; + if (hflag & BM_FACE) { + if (bm->elem_index_dirty & BM_FACE) { + int index= 0; + BM_ITER(ele, &iter, bm, BM_FACES_OF_MESH, NULL) { + BM_SetIndex(ele, index); /* set_ok */ + index++; + } + bm->elem_index_dirty &= ~BM_FACE; + BLI_assert(index == bm->totface); + } + else { + // printf("%s: skipping face index calc!\n", __func__); } - bm->elem_index_dirty &= ~BM_FACE; - BLI_assert(index == bm->totface); } } @@ -553,10 +569,12 @@ void BM_ElemIndex_Validate(BMesh *bm, const char *location, const char *func, co int err_idx= 0; BM_ITER(ele, &iter, bm, types[i], NULL) { - if (BM_GetIndex(ele) != index) { - err_val= BM_GetIndex(ele); - err_idx= index; - is_error= TRUE; + if (!is_dirty) { + if (BM_GetIndex(ele) != index) { + err_val= BM_GetIndex(ele); + err_idx= index; + is_error= TRUE; + } } BM_SetIndex(ele, index); /* set_ok */ @@ -590,6 +608,8 @@ void BM_ElemIndex_Validate(BMesh *bm, const char *location, const char *func, co } #endif #endif + (void) is_any_error; /* shut up the compiler */ + } BMVert *BM_Vert_AtIndex(BMesh *bm, const int index) diff --git a/source/blender/bmesh/intern/bmesh_newcore.c b/source/blender/bmesh/intern/bmesh_newcore.c index 453c332e5f8..ba89284ccb9 100644 --- a/source/blender/bmesh/intern/bmesh_newcore.c +++ b/source/blender/bmesh/intern/bmesh_newcore.c @@ -21,11 +21,29 @@ #include "bmesh_iterators.h" #include "bmesh_private.h" +/* use so valgrinds memcheck alerts us when undefined index is used. + * TESTING ONLY! */ +// #define USE_DEBUG_INDEX_MEMCHECK + +#ifdef USE_DEBUG_INDEX_MEMCHECK +#define DEBUG_MEMCHECK_INDEX_INVALIDATE(ele) \ + { \ + int undef_idx; \ + BM_SetIndex(ele, undef_idx); /* set_ok_invalid */ \ + } \ + +#endif + BMVert *BM_Make_Vert(BMesh *bm, float co[3], const struct BMVert *example) { BMVert *v = BLI_mempool_calloc(bm->vpool); +#ifdef USE_DEBUG_INDEX_MEMCHECK + DEBUG_MEMCHECK_INDEX_INVALIDATE(v) +#else BM_SetIndex(v, -1); /* set_ok_invalid */ +#endif + bm->elem_index_dirty |= BM_VERT; /* may add to middle of the pool */ bm->totvert++; @@ -82,7 +100,12 @@ BMEdge *BM_Make_Edge(BMesh *bm, BMVert *v1, BMVert *v2, const BMEdge *example, i e = BLI_mempool_calloc(bm->epool); +#ifdef USE_DEBUG_INDEX_MEMCHECK + DEBUG_MEMCHECK_INDEX_INVALIDATE(e) +#else BM_SetIndex(e, -1); /* set_ok_invalid */ +#endif + bm->elem_index_dirty |= BM_EDGE; /* may add to middle of the pool */ bm->totedge++; @@ -232,7 +255,12 @@ BMFace *BM_Make_Face(BMesh *bm, BMVert **verts, BMEdge **edges, int len, int nod f = BLI_mempool_calloc(bm->fpool); +#ifdef USE_DEBUG_INDEX_MEMCHECK + DEBUG_MEMCHECK_INDEX_INVALIDATE(f) +#else BM_SetIndex(f, -1); /* set_ok_invalid */ +#endif + bm->elem_index_dirty |= BM_FACE; /* may add to middle of the pool */ bm->totface++; @@ -928,7 +956,12 @@ static BMFace *bmesh_addpolylist(BMesh *bm, BMFace *UNUSED(example)) f->head.htype = BM_FACE; BLI_addtail(&f->loops, lst); +#ifdef USE_DEBUG_INDEX_MEMCHECK + DEBUG_MEMCHECK_INDEX_INVALIDATE(f) +#else BM_SetIndex(f, -1); /* set_ok_invalid */ +#endif + bm->elem_index_dirty |= BM_FACE; /* may add to middle of the pool */ bm->totface++;