From aa5ce2fcc1e349bdcb48a2f054d6179af293413f Mon Sep 17 00:00:00 2001 From: Hans Goudey Date: Wed, 12 Oct 2022 10:08:10 -0500 Subject: Cleanup: Use const vertex pointer argument --- source/blender/blenkernel/intern/mesh_normals.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'source/blender/blenkernel/intern/mesh_normals.cc') diff --git a/source/blender/blenkernel/intern/mesh_normals.cc b/source/blender/blenkernel/intern/mesh_normals.cc index e589aff1c73..76f2078e6c8 100644 --- a/source/blender/blenkernel/intern/mesh_normals.cc +++ b/source/blender/blenkernel/intern/mesh_normals.cc @@ -470,7 +470,7 @@ void BKE_mesh_calc_normals(Mesh *mesh) #endif } -void BKE_mesh_calc_normals_looptri(MVert *mverts, +void BKE_mesh_calc_normals_looptri(const MVert *mverts, int numVerts, const MLoop *mloop, const MLoopTri *looptri, @@ -508,7 +508,7 @@ void BKE_mesh_calc_normals_looptri(MVert *mverts, /* Following Mesh convention; we use vertex coordinate itself for normal in this case. */ for (int i = 0; i < numVerts; i++) { - MVert *mv = &mverts[i]; + const MVert *mv = &mverts[i]; float *no = tnorms[i]; if (UNLIKELY(normalize_v3(no) == 0.0f)) { -- cgit v1.2.3 From c34c6d3e25f2f4d96d124cb5ec43c4392e7de4dc Mon Sep 17 00:00:00 2001 From: Hans Goudey Date: Wed, 12 Oct 2022 20:55:26 -0500 Subject: Mesh: Move runtime data out of DNA This commit replaces the `Mesh_Runtime` struct embedded in `Mesh` with `blender::bke::MeshRuntime`. This has quite a few benefits: - It's possible to use C++ types like `std::mutex`, `Array`, `BitVector`, etc. more easily - Meshes saved in files are slightly smaller - Copying and writing meshes is a bit more obvious without clearing of runtime data, etc. The first is by far the most important. It will allows us to avoid a bunch of manual memory management boilerplate that is error-prone and annoying. It should also simplify future CoW improvements for runtime data. This patch doesn't change anything besides changing `mesh.runtime.data` to `mesh.runtime->data`. The cleanups above will happen separately. Differential Revision: https://developer.blender.org/D16180 --- source/blender/blenkernel/intern/mesh_normals.cc | 68 ++++++++++++------------ 1 file changed, 34 insertions(+), 34 deletions(-) (limited to 'source/blender/blenkernel/intern/mesh_normals.cc') diff --git a/source/blender/blenkernel/intern/mesh_normals.cc b/source/blender/blenkernel/intern/mesh_normals.cc index 76f2078e6c8..c2594ea3816 100644 --- a/source/blender/blenkernel/intern/mesh_normals.cc +++ b/source/blender/blenkernel/intern/mesh_normals.cc @@ -95,72 +95,72 @@ static void add_v3_v3_atomic(float r[3], const float a[3]) void BKE_mesh_normals_tag_dirty(Mesh *mesh) { - mesh->runtime.vert_normals_dirty = true; - mesh->runtime.poly_normals_dirty = true; + mesh->runtime->vert_normals_dirty = true; + mesh->runtime->poly_normals_dirty = true; } float (*BKE_mesh_vertex_normals_for_write(Mesh *mesh))[3] { - if (mesh->runtime.vert_normals == nullptr) { - mesh->runtime.vert_normals = (float(*)[3])MEM_malloc_arrayN( + if (mesh->runtime->vert_normals == nullptr) { + mesh->runtime->vert_normals = (float(*)[3])MEM_malloc_arrayN( mesh->totvert, sizeof(float[3]), __func__); } - BLI_assert(MEM_allocN_len(mesh->runtime.vert_normals) >= sizeof(float[3]) * mesh->totvert); + BLI_assert(MEM_allocN_len(mesh->runtime->vert_normals) >= sizeof(float[3]) * mesh->totvert); - return mesh->runtime.vert_normals; + return mesh->runtime->vert_normals; } float (*BKE_mesh_poly_normals_for_write(Mesh *mesh))[3] { - if (mesh->runtime.poly_normals == nullptr) { - mesh->runtime.poly_normals = (float(*)[3])MEM_malloc_arrayN( + if (mesh->runtime->poly_normals == nullptr) { + mesh->runtime->poly_normals = (float(*)[3])MEM_malloc_arrayN( mesh->totpoly, sizeof(float[3]), __func__); } - BLI_assert(MEM_allocN_len(mesh->runtime.poly_normals) >= sizeof(float[3]) * mesh->totpoly); + BLI_assert(MEM_allocN_len(mesh->runtime->poly_normals) >= sizeof(float[3]) * mesh->totpoly); - return mesh->runtime.poly_normals; + return mesh->runtime->poly_normals; } void BKE_mesh_vertex_normals_clear_dirty(Mesh *mesh) { - mesh->runtime.vert_normals_dirty = false; + mesh->runtime->vert_normals_dirty = false; BKE_mesh_assert_normals_dirty_or_calculated(mesh); } void BKE_mesh_poly_normals_clear_dirty(Mesh *mesh) { - mesh->runtime.poly_normals_dirty = false; + mesh->runtime->poly_normals_dirty = false; BKE_mesh_assert_normals_dirty_or_calculated(mesh); } bool BKE_mesh_vertex_normals_are_dirty(const Mesh *mesh) { - return mesh->runtime.vert_normals_dirty; + return mesh->runtime->vert_normals_dirty; } bool BKE_mesh_poly_normals_are_dirty(const Mesh *mesh) { - return mesh->runtime.poly_normals_dirty; + return mesh->runtime->poly_normals_dirty; } void BKE_mesh_clear_derived_normals(Mesh *mesh) { - MEM_SAFE_FREE(mesh->runtime.vert_normals); - MEM_SAFE_FREE(mesh->runtime.poly_normals); + MEM_SAFE_FREE(mesh->runtime->vert_normals); + MEM_SAFE_FREE(mesh->runtime->poly_normals); - mesh->runtime.vert_normals_dirty = true; - mesh->runtime.poly_normals_dirty = true; + mesh->runtime->vert_normals_dirty = true; + mesh->runtime->poly_normals_dirty = true; } void BKE_mesh_assert_normals_dirty_or_calculated(const Mesh *mesh) { - if (!mesh->runtime.vert_normals_dirty) { - BLI_assert(mesh->runtime.vert_normals || mesh->totvert == 0); + if (!mesh->runtime->vert_normals_dirty) { + BLI_assert(mesh->runtime->vert_normals || mesh->totvert == 0); } - if (!mesh->runtime.poly_normals_dirty) { - BLI_assert(mesh->runtime.poly_normals || mesh->totpoly == 0); + if (!mesh->runtime->poly_normals_dirty) { + BLI_assert(mesh->runtime->poly_normals || mesh->totpoly == 0); } } @@ -348,20 +348,20 @@ void BKE_mesh_calc_normals_poly_and_vertex(const MVert *mvert, const float (*BKE_mesh_vertex_normals_ensure(const Mesh *mesh))[3] { if (!BKE_mesh_vertex_normals_are_dirty(mesh)) { - BLI_assert(mesh->runtime.vert_normals != nullptr || mesh->totvert == 0); - return mesh->runtime.vert_normals; + BLI_assert(mesh->runtime->vert_normals != nullptr || mesh->totvert == 0); + return mesh->runtime->vert_normals; } if (mesh->totvert == 0) { return nullptr; } - ThreadMutex *normals_mutex = (ThreadMutex *)mesh->runtime.normals_mutex; + ThreadMutex *normals_mutex = (ThreadMutex *)mesh->runtime->normals_mutex; BLI_mutex_lock(normals_mutex); if (!BKE_mesh_vertex_normals_are_dirty(mesh)) { - BLI_assert(mesh->runtime.vert_normals != nullptr); + BLI_assert(mesh->runtime->vert_normals != nullptr); BLI_mutex_unlock(normals_mutex); - return mesh->runtime.vert_normals; + return mesh->runtime->vert_normals; } float(*vert_normals)[3]; @@ -397,20 +397,20 @@ const float (*BKE_mesh_vertex_normals_ensure(const Mesh *mesh))[3] const float (*BKE_mesh_poly_normals_ensure(const Mesh *mesh))[3] { if (!BKE_mesh_poly_normals_are_dirty(mesh)) { - BLI_assert(mesh->runtime.poly_normals != nullptr || mesh->totpoly == 0); - return mesh->runtime.poly_normals; + BLI_assert(mesh->runtime->poly_normals != nullptr || mesh->totpoly == 0); + return mesh->runtime->poly_normals; } if (mesh->totpoly == 0) { return nullptr; } - ThreadMutex *normals_mutex = (ThreadMutex *)mesh->runtime.normals_mutex; + ThreadMutex *normals_mutex = (ThreadMutex *)mesh->runtime->normals_mutex; BLI_mutex_lock(normals_mutex); if (!BKE_mesh_poly_normals_are_dirty(mesh)) { - BLI_assert(mesh->runtime.poly_normals != nullptr); + BLI_assert(mesh->runtime->poly_normals != nullptr); BLI_mutex_unlock(normals_mutex); - return mesh->runtime.poly_normals; + return mesh->runtime->poly_normals; } float(*poly_normals)[3]; @@ -441,7 +441,7 @@ const float (*BKE_mesh_poly_normals_ensure(const Mesh *mesh))[3] void BKE_mesh_ensure_normals_for_display(Mesh *mesh) { - switch ((eMeshWrapperType)mesh->runtime.wrapper_type) { + switch (mesh->runtime->wrapper_type) { case ME_WRAPPER_TYPE_SUBD: case ME_WRAPPER_TYPE_MDATA: BKE_mesh_vertex_normals_ensure(mesh); @@ -449,7 +449,7 @@ void BKE_mesh_ensure_normals_for_display(Mesh *mesh) break; case ME_WRAPPER_TYPE_BMESH: { struct BMEditMesh *em = mesh->edit_mesh; - EditMeshData *emd = mesh->runtime.edit_data; + EditMeshData *emd = mesh->runtime->edit_data; if (emd->vertexCos) { BKE_editmesh_cache_ensure_vert_normals(em, emd); BKE_editmesh_cache_ensure_poly_normals(em, emd); -- cgit v1.2.3 From c67e5628d22f8348492b6205081fe1798d50689c Mon Sep 17 00:00:00 2001 From: Hans Goudey Date: Wed, 12 Oct 2022 22:31:02 -0500 Subject: Cleanup: Use std::mutex for mesh runtime mutexes Instead of allocating three separate ThreadMutex pointers, just embed std::mutex into the struct directly. --- source/blender/blenkernel/intern/mesh_normals.cc | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) (limited to 'source/blender/blenkernel/intern/mesh_normals.cc') diff --git a/source/blender/blenkernel/intern/mesh_normals.cc b/source/blender/blenkernel/intern/mesh_normals.cc index c2594ea3816..ebb5a72d137 100644 --- a/source/blender/blenkernel/intern/mesh_normals.cc +++ b/source/blender/blenkernel/intern/mesh_normals.cc @@ -356,11 +356,9 @@ const float (*BKE_mesh_vertex_normals_ensure(const Mesh *mesh))[3] return nullptr; } - ThreadMutex *normals_mutex = (ThreadMutex *)mesh->runtime->normals_mutex; - BLI_mutex_lock(normals_mutex); + std::lock_guard lock{mesh->runtime->normals_mutex}; if (!BKE_mesh_vertex_normals_are_dirty(mesh)) { BLI_assert(mesh->runtime->vert_normals != nullptr); - BLI_mutex_unlock(normals_mutex); return mesh->runtime->vert_normals; } @@ -390,7 +388,6 @@ const float (*BKE_mesh_vertex_normals_ensure(const Mesh *mesh))[3] BKE_mesh_poly_normals_clear_dirty(&mesh_mutable); }); - BLI_mutex_unlock(normals_mutex); return vert_normals; } @@ -405,11 +402,9 @@ const float (*BKE_mesh_poly_normals_ensure(const Mesh *mesh))[3] return nullptr; } - ThreadMutex *normals_mutex = (ThreadMutex *)mesh->runtime->normals_mutex; - BLI_mutex_lock(normals_mutex); + std::lock_guard lock{mesh->runtime->normals_mutex}; if (!BKE_mesh_poly_normals_are_dirty(mesh)) { BLI_assert(mesh->runtime->poly_normals != nullptr); - BLI_mutex_unlock(normals_mutex); return mesh->runtime->poly_normals; } @@ -435,7 +430,6 @@ const float (*BKE_mesh_poly_normals_ensure(const Mesh *mesh))[3] BKE_mesh_poly_normals_clear_dirty(&mesh_mutable); }); - BLI_mutex_unlock(normals_mutex); return poly_normals; } -- cgit v1.2.3