[Bf-blender-cvs] [7fab7b6] master: Assert CustomData_from_bmeshpoly is used correctly

Campbell Barton noreply at git.blender.org
Wed Sep 9 08:55:41 CEST 2015


Commit: 7fab7b63f9b7bab2709721ce60977820a66f16ef
Author: Campbell Barton
Date:   Wed Sep 9 16:42:55 2015 +1000
Branches: master
https://developer.blender.org/rB7fab7b63f9b7bab2709721ce60977820a66f16ef

Assert CustomData_from_bmeshpoly is used correctly

Follow up to last commit, since bugs here aren't so obvious.

===================================================================

M	source/blender/blenkernel/BKE_customdata.h
M	source/blender/blenkernel/intern/DerivedMesh.c
M	source/blender/blenkernel/intern/customdata.c

===================================================================

diff --git a/source/blender/blenkernel/BKE_customdata.h b/source/blender/blenkernel/BKE_customdata.h
index 6fb27cf..3e78475 100644
--- a/source/blender/blenkernel/BKE_customdata.h
+++ b/source/blender/blenkernel/BKE_customdata.h
@@ -381,6 +381,10 @@ void CustomData_bmesh_update_active_layers(struct CustomData *fdata, struct Cust
 void CustomData_bmesh_do_versions_update_active_layers(struct CustomData *fdata, struct CustomData *pdata, struct CustomData *ldata);
 void CustomData_bmesh_init_pool(struct CustomData *data, int totelem, const char htype);
 
+#ifndef NDEBUG
+bool CustomData_from_bmeshpoly_test(CustomData *fdata, CustomData *pdata, CustomData *ldata, bool fallback);
+#endif
+
 /* External file storage */
 
 void CustomData_external_add(struct CustomData *data,
diff --git a/source/blender/blenkernel/intern/DerivedMesh.c b/source/blender/blenkernel/intern/DerivedMesh.c
index 286558237..d13786f 100644
--- a/source/blender/blenkernel/intern/DerivedMesh.c
+++ b/source/blender/blenkernel/intern/DerivedMesh.c
@@ -601,6 +601,8 @@ void DM_generate_tangent_tessface_data(DerivedMesh *dm, bool generate)
 		CustomData_bmesh_update_active_layers(fdata, pdata, ldata);
 	}
 
+	BLI_assert(CustomData_from_bmeshpoly_test(fdata, pdata, ldata, true));
+
 	loopindex = MEM_mallocN(sizeof(*loopindex) * totface, __func__);
 
 	for (mf_idx = 0, mf = mface; mf_idx < totface; mf_idx++, mf++) {
diff --git a/source/blender/blenkernel/intern/customdata.c b/source/blender/blenkernel/intern/customdata.c
index 3cf8730..ecd8093 100644
--- a/source/blender/blenkernel/intern/customdata.c
+++ b/source/blender/blenkernel/intern/customdata.c
@@ -2489,6 +2489,10 @@ void CustomData_to_bmeshpoly(CustomData *fdata, CustomData *pdata, CustomData *l
 void CustomData_from_bmeshpoly(CustomData *fdata, CustomData *pdata, CustomData *ldata, int total)
 {
 	int i;
+
+	/* avoid accumulating extra layers */
+	BLI_assert(!CustomData_from_bmeshpoly_test(fdata, pdata, ldata, false));
+
 	for (i = 0; i < pdata->totlayer; i++) {
 		if (pdata->layers[i].type == CD_MTEXPOLY) {
 			CustomData_add_layer_named(fdata, CD_MTFACE, CD_CALLOC, NULL, total, pdata->layers[i].name);
@@ -2515,6 +2519,41 @@ void CustomData_from_bmeshpoly(CustomData *fdata, CustomData *pdata, CustomData
 	CustomData_bmesh_update_active_layers(fdata, pdata, ldata);
 }
 
+#ifndef NDEBUG
+/**
+ * Debug check, used to assert when we expect layers to be in/out of sync.
+ *
+ * \param fallback: Use when there are no layers to handle,
+ * since callers may expect success or failure.
+ */
+bool CustomData_from_bmeshpoly_test(CustomData *fdata, CustomData *pdata, CustomData *ldata, bool fallback)
+{
+	int a_num = 0, b_num = 0;
+#define LAYER_CMP(l_a, t_a, l_b, t_b) \
+	((a_num += CustomData_number_of_layers(l_a, t_a)) == (b_num += CustomData_number_of_layers(l_b, t_b)))
+
+	if (!LAYER_CMP(pdata, CD_MTEXPOLY, fdata, CD_MTFACE))
+		return false;
+	if (!LAYER_CMP(ldata, CD_MLOOPCOL, fdata, CD_MCOL))
+		return false;
+	if (!LAYER_CMP(ldata, CD_PREVIEW_MLOOPCOL, fdata, CD_PREVIEW_MCOL))
+		return false;
+	if (!LAYER_CMP(ldata, CD_ORIGSPACE_MLOOP, fdata, CD_ORIGSPACE))
+		return false;
+	if (!LAYER_CMP(ldata, CD_NORMAL, fdata, CD_TESSLOOPNORMAL))
+		return false;
+	if (!LAYER_CMP(ldata, CD_TANGENT, fdata, CD_TANGENT))
+		return false;
+
+#undef TEST_RET
+
+	/* if no layers are on either CustomData's,
+	 * then there was nothing to do... */
+	return a_num ? true : fallback;
+}
+#endif
+
+
 void CustomData_bmesh_update_active_layers(CustomData *fdata, CustomData *pdata, CustomData *ldata)
 {
 	int act;




More information about the Bf-blender-cvs mailing list