diff --git a/source/blender/blenkernel/BKE_customdata.h b/source/blender/blenkernel/BKE_customdata.h index b97ac9cc9b9..e7161722925 100644 --- a/source/blender/blenkernel/BKE_customdata.h +++ b/source/blender/blenkernel/BKE_customdata.h @@ -283,21 +283,6 @@ bool CustomData_has_layer(const struct CustomData *data, int type); int CustomData_number_of_layers(const struct CustomData *data, int type); int CustomData_number_of_layers_typemask(const struct CustomData *data, eCustomDataMask mask); -/** - * Duplicate data of a layer with flag NOFREE, and remove that flag. - * \return the layer data. - */ -void *CustomData_duplicate_referenced_layer(struct CustomData *data, int type, int totelem); -void *CustomData_duplicate_referenced_layer_n(struct CustomData *data, - int type, - int n, - int totelem); -void *CustomData_duplicate_referenced_layer_named(struct CustomData *data, - int type, - const char *name, - int totelem); -bool CustomData_is_referenced_layer(struct CustomData *data, int type); - /** * Duplicate all the layers with flag NOFREE, and remove the flag from duplicated layers. */ @@ -409,11 +394,15 @@ void CustomData_swap_corners(struct CustomData *data, int index, const int *corn void CustomData_swap(struct CustomData *data, int index_a, int index_b); /** - * Gets a pointer to the data element at index from the first layer of type. - * \return NULL if there is no layer of type. + * Retrieve a pointer to an element of the active layer of the given \a type, chosen by the + * \a index, if it exists. */ -void *CustomData_get(const struct CustomData *data, int index, int type); -void *CustomData_get_n(const struct CustomData *data, int type, int index, int n); +void *CustomData_get_for_write(struct CustomData *data, int index, int type, int totelem); +/** + * Retrieve a pointer to an element of the \a nth layer of the given \a type, chosen by the + * \a index, if it exists. + */ +void *CustomData_get_n_for_write(struct CustomData *data, int type, int index, int n, int totelem); /* BMesh Custom Data Functions. * Should replace edit-mesh ones with these as well, due to more efficient memory alloc. */ @@ -431,12 +420,29 @@ bool CustomData_set_layer_name(struct CustomData *data, int type, int n, const c const char *CustomData_get_layer_name(const struct CustomData *data, int type, int n); /** - * Gets a pointer to the active or first layer of type. - * \return NULL if there is no layer of type. + * Retrieve the data array of the active layer of the given \a type, if it exists. Return null + * otherwise. */ -void *CustomData_get_layer(const struct CustomData *data, int type); -void *CustomData_get_layer_n(const struct CustomData *data, int type, int n); -void *CustomData_get_layer_named(const struct CustomData *data, int type, const char *name); +const void *CustomData_get_layer(const struct CustomData *data, int type); +void *CustomData_get_layer_for_write(struct CustomData *data, int type, int totelem); + +/** + * Retrieve the data array of the \a nth layer of the given \a type, if it exists. Return null + * otherwise. + */ +const void *CustomData_get_layer_n(const struct CustomData *data, int type, int n); +void *CustomData_get_layer_n_for_write(struct CustomData *data, int type, int n, int totelem); + +/** + * Retrieve the data array of the layer with the given \a name and \a type, if it exists. Return + * null otherwise. + */ +const void *CustomData_get_layer_named(const struct CustomData *data, int type, const char *name); +void *CustomData_get_layer_named_for_write(CustomData *data, + int type, + const char *name, + int totelem); + int CustomData_get_offset(const struct CustomData *data, int type); int CustomData_get_offset_named(const CustomData *data, int type, const char *name); int CustomData_get_n_offset(const struct CustomData *data, int type, int n); diff --git a/source/blender/blenkernel/BKE_key.h b/source/blender/blenkernel/BKE_key.h index d89ef744449..d868969f9b9 100644 --- a/source/blender/blenkernel/BKE_key.h +++ b/source/blender/blenkernel/BKE_key.h @@ -145,7 +145,7 @@ void BKE_keyblock_convert_to_mesh(const struct KeyBlock *kb, * \param r_loop_normals: if non-NULL, an array of vectors, same length as number of loops. */ void BKE_keyblock_mesh_calc_normals(const struct KeyBlock *kb, - const struct Mesh *mesh, + struct Mesh *mesh, float (*r_vert_normals)[3], float (*r_poly_normals)[3], float (*r_loop_normals)[3]); diff --git a/source/blender/blenkernel/BKE_mesh.h b/source/blender/blenkernel/BKE_mesh.h index 4d81507ef1c..ad964184263 100644 --- a/source/blender/blenkernel/BKE_mesh.h +++ b/source/blender/blenkernel/BKE_mesh.h @@ -759,7 +759,8 @@ void BKE_mesh_polygon_flip_ex(const struct MPoly *mpoly, bool use_loop_mdisp_flip); void BKE_mesh_polygon_flip(const struct MPoly *mpoly, struct MLoop *mloop, - struct CustomData *ldata); + struct CustomData *ldata, + int totloop); /** * Flip (invert winding of) all polygons (used to inverse their normals). * @@ -983,7 +984,7 @@ BLI_INLINE const int *BKE_mesh_material_indices(const Mesh *mesh) */ BLI_INLINE int *BKE_mesh_material_indices_for_write(Mesh *mesh) { - int *indices = (int *)CustomData_duplicate_referenced_layer_named( + int *indices = (int *)CustomData_get_layer_named_for_write( &mesh->pdata, CD_PROP_INT32, "material_index", mesh->totpoly); if (indices) { return indices; @@ -998,7 +999,7 @@ BLI_INLINE const float (*BKE_mesh_vert_positions(const Mesh *mesh))[3] } BLI_INLINE float (*BKE_mesh_vert_positions_for_write(Mesh *mesh))[3] { - return (float(*)[3])CustomData_duplicate_referenced_layer_named( + return (float(*)[3])CustomData_get_layer_named_for_write( &mesh->vdata, CD_PROP_FLOAT3, "position", mesh->totvert); } @@ -1008,7 +1009,7 @@ BLI_INLINE const MEdge *BKE_mesh_edges(const Mesh *mesh) } BLI_INLINE MEdge *BKE_mesh_edges_for_write(Mesh *mesh) { - return (MEdge *)CustomData_duplicate_referenced_layer(&mesh->edata, CD_MEDGE, mesh->totedge); + return (MEdge *)CustomData_get_layer_for_write(&mesh->edata, CD_MEDGE, mesh->totedge); } BLI_INLINE const MPoly *BKE_mesh_polys(const Mesh *mesh) @@ -1017,7 +1018,7 @@ BLI_INLINE const MPoly *BKE_mesh_polys(const Mesh *mesh) } BLI_INLINE MPoly *BKE_mesh_polys_for_write(Mesh *mesh) { - return (MPoly *)CustomData_duplicate_referenced_layer(&mesh->pdata, CD_MPOLY, mesh->totpoly); + return (MPoly *)CustomData_get_layer_for_write(&mesh->pdata, CD_MPOLY, mesh->totpoly); } BLI_INLINE const MLoop *BKE_mesh_loops(const Mesh *mesh) @@ -1026,7 +1027,7 @@ BLI_INLINE const MLoop *BKE_mesh_loops(const Mesh *mesh) } BLI_INLINE MLoop *BKE_mesh_loops_for_write(Mesh *mesh) { - return (MLoop *)CustomData_duplicate_referenced_layer(&mesh->ldata, CD_MLOOP, mesh->totloop); + return (MLoop *)CustomData_get_layer_for_write(&mesh->ldata, CD_MLOOP, mesh->totloop); } BLI_INLINE const MDeformVert *BKE_mesh_deform_verts(const Mesh *mesh) @@ -1035,7 +1036,7 @@ BLI_INLINE const MDeformVert *BKE_mesh_deform_verts(const Mesh *mesh) } BLI_INLINE MDeformVert *BKE_mesh_deform_verts_for_write(Mesh *mesh) { - MDeformVert *dvert = (MDeformVert *)CustomData_duplicate_referenced_layer( + MDeformVert *dvert = (MDeformVert *)CustomData_get_layer_for_write( &mesh->vdata, CD_MDEFORMVERT, mesh->totvert); if (dvert) { return dvert; diff --git a/source/blender/blenkernel/intern/DerivedMesh.cc b/source/blender/blenkernel/intern/DerivedMesh.cc index 709f52060fc..39a68b72d2a 100644 --- a/source/blender/blenkernel/intern/DerivedMesh.cc +++ b/source/blender/blenkernel/intern/DerivedMesh.cc @@ -94,8 +94,8 @@ static void editbmesh_calc_modifier_final_normals_or_defer( static float *dm_getVertArray(DerivedMesh *dm) { - float(*positions)[3] = (float(*)[3])CustomData_get_layer_named( - &dm->vertData, CD_PROP_FLOAT3, "position"); + float(*positions)[3] = (float(*)[3])CustomData_get_layer_named_for_write( + &dm->vertData, CD_PROP_FLOAT3, "position", dm->getNumVerts(dm)); if (!positions) { positions = (float(*)[3])CustomData_add_layer_named( @@ -109,7 +109,8 @@ static float *dm_getVertArray(DerivedMesh *dm) static MEdge *dm_getEdgeArray(DerivedMesh *dm) { - MEdge *medge = (MEdge *)CustomData_get_layer(&dm->edgeData, CD_MEDGE); + MEdge *medge = (MEdge *)CustomData_get_layer_for_write( + &dm->edgeData, CD_MEDGE, dm->getNumEdges(dm)); if (!medge) { medge = (MEdge *)CustomData_add_layer( @@ -123,7 +124,8 @@ static MEdge *dm_getEdgeArray(DerivedMesh *dm) static MLoop *dm_getLoopArray(DerivedMesh *dm) { - MLoop *mloop = (MLoop *)CustomData_get_layer(&dm->loopData, CD_MLOOP); + MLoop *mloop = (MLoop *)CustomData_get_layer_for_write( + &dm->loopData, CD_MLOOP, dm->getNumLoops(dm)); if (!mloop) { mloop = (MLoop *)CustomData_add_layer( @@ -137,7 +139,8 @@ static MLoop *dm_getLoopArray(DerivedMesh *dm) static MPoly *dm_getPolyArray(DerivedMesh *dm) { - MPoly *mpoly = (MPoly *)CustomData_get_layer(&dm->polyData, CD_MPOLY); + MPoly *mpoly = (MPoly *)CustomData_get_layer_for_write( + &dm->polyData, CD_MPOLY, dm->getNumPolys(dm)); if (!mpoly) { mpoly = (MPoly *)CustomData_add_layer( @@ -351,7 +354,7 @@ static void mesh_set_only_copy(Mesh *mesh, const CustomData_MeshMasks *mask) void *DM_get_vert_data_layer(DerivedMesh *dm, int type) { - return CustomData_get_layer(&dm->vertData, type); + return CustomData_get_layer_for_write(&dm->vertData, type, dm->getNumVerts(dm)); } void *DM_get_edge_data_layer(DerivedMesh *dm, int type) @@ -360,17 +363,17 @@ void *DM_get_edge_data_layer(DerivedMesh *dm, int type) return dm->getEdgeArray(dm); } - return CustomData_get_layer(&dm->edgeData, type); + return CustomData_get_layer_for_write(&dm->edgeData, type, dm->getNumEdges(dm)); } void *DM_get_poly_data_layer(DerivedMesh *dm, int type) { - return CustomData_get_layer(&dm->polyData, type); + return CustomData_get_layer_for_write(&dm->polyData, type, dm->getNumPolys(dm)); } void *DM_get_loop_data_layer(DerivedMesh *dm, int type) { - return CustomData_get_layer(&dm->loopData, type); + return CustomData_get_layer_for_write(&dm->loopData, type, dm->getNumLoops(dm)); } void DM_copy_vert_data( @@ -499,10 +502,10 @@ static void add_orco_mesh(Object *ob, BMEditMesh *em, Mesh *mesh, Mesh *mesh_orc BKE_mesh_orco_verts_transform((Mesh *)ob->data, orco, totvert, 0); } - if (!(layerorco = (float(*)[3])CustomData_get_layer(&mesh->vdata, layer))) { - CustomData_add_layer(&mesh->vdata, layer, CD_SET_DEFAULT, nullptr, mesh->totvert); - - layerorco = (float(*)[3])CustomData_get_layer(&mesh->vdata, layer); + layerorco = (float(*)[3])CustomData_get_layer_for_write(&mesh->vdata, layer, mesh->totvert); + if (!layerorco) { + layerorco = (float(*)[3])CustomData_add_layer( + &mesh->vdata, layer, CD_SET_DEFAULT, nullptr, mesh->totvert); } memcpy(layerorco, orco, sizeof(float[3]) * totvert); @@ -924,13 +927,16 @@ static void mesh_calc_modifiers(struct Depsgraph *depsgraph, /* Not worth parallelizing this, * gives less than 0.1% overall speedup in best of best cases... */ - range_vn_i((int *)CustomData_get_layer(&mesh_final->vdata, CD_ORIGINDEX), + range_vn_i((int *)CustomData_get_layer_for_write( + &mesh_final->vdata, CD_ORIGINDEX, mesh_final->totvert), mesh_final->totvert, 0); - range_vn_i((int *)CustomData_get_layer(&mesh_final->edata, CD_ORIGINDEX), + range_vn_i((int *)CustomData_get_layer_for_write( + &mesh_final->edata, CD_ORIGINDEX, mesh_final->totedge), mesh_final->totedge, 0); - range_vn_i((int *)CustomData_get_layer(&mesh_final->pdata, CD_ORIGINDEX), + range_vn_i((int *)CustomData_get_layer_for_write( + &mesh_final->pdata, CD_ORIGINDEX, mesh_final->totpoly), mesh_final->totpoly, 0); } @@ -1932,8 +1938,8 @@ static void mesh_init_origspace(Mesh *mesh) { const float default_osf[4][2] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; - OrigSpaceLoop *lof_array = (OrigSpaceLoop *)CustomData_get_layer(&mesh->ldata, - CD_ORIGSPACE_MLOOP); + OrigSpaceLoop *lof_array = (OrigSpaceLoop *)CustomData_get_layer_for_write( + &mesh->ldata, CD_ORIGSPACE_MLOOP, mesh->totloop); const int numpoly = mesh->totpoly; // const int numloop = mesh->totloop; const Span positions = mesh->vert_positions(); diff --git a/source/blender/blenkernel/intern/attribute_access.cc b/source/blender/blenkernel/intern/attribute_access.cc index d95c80cf1eb..92bb7247673 100644 --- a/source/blender/blenkernel/intern/attribute_access.cc +++ b/source/blender/blenkernel/intern/attribute_access.cc @@ -340,11 +340,11 @@ GAttributeWriter BuiltinCustomDataLayerProvider::try_get_for_write(void *owner) void *data = nullptr; if (stored_as_named_attribute_) { - data = CustomData_duplicate_referenced_layer_named( + data = CustomData_get_layer_named_for_write( custom_data, stored_type_, name_.c_str(), element_num); } else { - data = CustomData_duplicate_referenced_layer(custom_data, stored_type_, element_num); + data = CustomData_get_layer_for_write(custom_data, stored_type_, element_num); } if (data == nullptr) { return {}; @@ -461,7 +461,7 @@ GAttributeWriter CustomDataAttributeProvider::try_get_for_write( if (!custom_data_layer_matches_attribute_id(layer, attribute_id)) { continue; } - CustomData_duplicate_referenced_layer_named(custom_data, layer.type, layer.name, element_num); + CustomData_get_layer_named_for_write(custom_data, layer.type, layer.name, element_num); const CPPType *type = custom_data_type_to_cpp_type((eCustomDataType)layer.type); if (type == nullptr) { diff --git a/source/blender/blenkernel/intern/cdderivedmesh.c b/source/blender/blenkernel/intern/cdderivedmesh.c index 7370ee6970a..a5d179fb2cb 100644 --- a/source/blender/blenkernel/intern/cdderivedmesh.c +++ b/source/blender/blenkernel/intern/cdderivedmesh.c @@ -221,13 +221,14 @@ static DerivedMesh *cdDM_from_mesh_ex(Mesh *mesh, CustomData_merge(&mesh->ldata, &dm->loopData, cddata_masks.lmask, alloctype, mesh->totloop); CustomData_merge(&mesh->pdata, &dm->polyData, cddata_masks.pmask, alloctype, mesh->totpoly); - cddm->vert_positions = CustomData_get_layer_named(&dm->vertData, CD_PROP_FLOAT3, "position"); + cddm->vert_positions = CustomData_get_layer_named_for_write( + &dm->vertData, CD_PROP_FLOAT3, "position", mesh->totvert); /* Though this may be an unnecessary calculation, simply retrieving the layer may return nothing * or dirty normals. */ cddm->vert_normals = BKE_mesh_vertex_normals_ensure(mesh); - cddm->medge = CustomData_get_layer(&dm->edgeData, CD_MEDGE); - cddm->mloop = CustomData_get_layer(&dm->loopData, CD_MLOOP); - cddm->mpoly = CustomData_get_layer(&dm->polyData, CD_MPOLY); + cddm->medge = CustomData_get_layer_for_write(&dm->edgeData, CD_MEDGE, mesh->totedge); + cddm->mloop = CustomData_get_layer_for_write(&dm->loopData, CD_MLOOP, mesh->totloop); + cddm->mpoly = CustomData_get_layer_for_write(&dm->polyData, CD_MPOLY, mesh->totpoly); #if 0 cddm->mface = CustomData_get_layer(&dm->faceData, CD_MFACE); #else diff --git a/source/blender/blenkernel/intern/curves_geometry.cc b/source/blender/blenkernel/intern/curves_geometry.cc index 835b6728e99..65117ab00bb 100644 --- a/source/blender/blenkernel/intern/curves_geometry.cc +++ b/source/blender/blenkernel/intern/curves_geometry.cc @@ -216,8 +216,7 @@ static MutableSpan get_mutable_attribute(CurvesGeometry &curves, const eCustomDataType type = cpp_type_to_custom_data_type(CPPType::get()); CustomData &custom_data = domain_custom_data(curves, domain); - T *data = (T *)CustomData_duplicate_referenced_layer_named( - &custom_data, type, name.c_str(), num); + T *data = (T *)CustomData_get_layer_named_for_write(&custom_data, type, name.c_str(), num); if (data != nullptr) { return {data, num}; } diff --git a/source/blender/blenkernel/intern/customdata.cc b/source/blender/blenkernel/intern/customdata.cc index 1d970d35148..a8b8782062e 100644 --- a/source/blender/blenkernel/intern/customdata.cc +++ b/source/blender/blenkernel/intern/customdata.cc @@ -3052,35 +3052,6 @@ static void *customData_duplicate_referenced_layer_index(CustomData *data, return layer->data; } -void *CustomData_duplicate_referenced_layer(CustomData *data, const int type, const int totelem) -{ - int layer_index = CustomData_get_active_layer_index(data, type); - - return customData_duplicate_referenced_layer_index(data, layer_index, totelem); -} - -void *CustomData_duplicate_referenced_layer_n(CustomData *data, - const int type, - const int n, - const int totelem) -{ - /* get the layer index of the desired layer */ - int layer_index = CustomData_get_layer_index_n(data, type, n); - - return customData_duplicate_referenced_layer_index(data, layer_index, totelem); -} - -void *CustomData_duplicate_referenced_layer_named(CustomData *data, - const int type, - const char *name, - const int totelem) -{ - /* get the layer index of the desired layer */ - int layer_index = CustomData_get_named_layer_index(data, type, name); - - return customData_duplicate_referenced_layer_index(data, layer_index, totelem); -} - void CustomData_duplicate_referenced_layers(CustomData *data, const int totelem) { for (int i = 0; i < data->totlayer; i++) { @@ -3089,18 +3060,6 @@ void CustomData_duplicate_referenced_layers(CustomData *data, const int totelem) } } -bool CustomData_is_referenced_layer(CustomData *data, const int type) -{ - int layer_index = CustomData_get_active_layer_index(data, type); - if (layer_index == -1) { - return false; - } - - CustomDataLayer *layer = &data->layers[layer_index]; - - return (layer->flag & CD_FLAG_NOFREE) != 0; -} - void CustomData_free_temporary(CustomData *data, const int totelem) { int i, j; @@ -3415,20 +3374,21 @@ void CustomData_swap(CustomData *data, const int index_a, const int index_b) } } -void *CustomData_get(const CustomData *data, const int index, const int type) +void *CustomData_get_for_write(CustomData *data, const int index, const int type, int totelem) { BLI_assert(index >= 0); - void *layer_data = CustomData_get_layer(data, type); + void *layer_data = CustomData_get_layer_for_write(data, type, totelem); if (!layer_data) { return nullptr; } return POINTER_OFFSET(layer_data, size_t(index) * layerType_getInfo(type)->size); } -void *CustomData_get_n(const CustomData *data, const int type, const int index, const int n) +void *CustomData_get_n_for_write( + CustomData *data, const int type, const int index, const int n, int totelem) { BLI_assert(index >= 0); - void *layer_data = CustomData_get_layer_n(data, type, n); + void *layer_data = CustomData_get_layer_n_for_write(data, type, n, totelem); if (!layer_data) { return nullptr; } @@ -3436,7 +3396,7 @@ void *CustomData_get_n(const CustomData *data, const int type, const int index, return POINTER_OFFSET(layer_data, size_t(index) * layerType_getInfo(type)->size); } -void *CustomData_get_layer(const CustomData *data, const int type) +const void *CustomData_get_layer(const CustomData *data, const int type) { int layer_index = CustomData_get_active_layer_index(data, type); if (layer_index == -1) { @@ -3446,7 +3406,13 @@ void *CustomData_get_layer(const CustomData *data, const int type) return data->layers[layer_index].data; } -void *CustomData_get_layer_n(const CustomData *data, const int type, const int n) +void *CustomData_get_layer_for_write(CustomData *data, const int type, const int totelem) +{ + const int layer_index = CustomData_get_active_layer_index(data, type); + return customData_duplicate_referenced_layer_index(data, layer_index, totelem); +} + +const void *CustomData_get_layer_n(const CustomData *data, const int type, const int n) { int layer_index = CustomData_get_layer_index_n(data, type, n); if (layer_index == -1) { @@ -3456,7 +3422,16 @@ void *CustomData_get_layer_n(const CustomData *data, const int type, const int n return data->layers[layer_index].data; } -void *CustomData_get_layer_named(const CustomData *data, const int type, const char *name) +void *CustomData_get_layer_n_for_write(CustomData *data, + const int type, + const int n, + const int totelem) +{ + const int layer_index = CustomData_get_layer_index_n(data, type, n); + return customData_duplicate_referenced_layer_index(data, layer_index, totelem); +} + +const void *CustomData_get_layer_named(const CustomData *data, const int type, const char *name) { int layer_index = CustomData_get_named_layer_index(data, type, name); if (layer_index == -1) { @@ -3466,6 +3441,15 @@ void *CustomData_get_layer_named(const CustomData *data, const int type, const c return data->layers[layer_index].data; } +void *CustomData_get_layer_named_for_write(CustomData *data, + const int type, + const char *name, + const int totelem) +{ + const int layer_index = CustomData_get_named_layer_index(data, type, name); + return customData_duplicate_referenced_layer_index(data, layer_index, totelem); +} + int CustomData_get_offset(const CustomData *data, const int type) { int layer_index = CustomData_get_active_layer_index(data, type); diff --git a/source/blender/blenkernel/intern/data_transfer.cc b/source/blender/blenkernel/intern/data_transfer.cc index 48dd0c84a54..39d6cde31e7 100644 --- a/source/blender/blenkernel/intern/data_transfer.cc +++ b/source/blender/blenkernel/intern/data_transfer.cc @@ -277,10 +277,11 @@ static void data_transfer_dtdata_type_preprocess(Mesh *me_src, float(*loop_nors_dst)[3]; short(*custom_nors_dst)[2] = static_cast( - CustomData_get_layer(ldata_dst, CD_CUSTOMLOOPNORMAL)); + CustomData_get_layer_for_write(ldata_dst, CD_CUSTOMLOOPNORMAL, me_dst->totloop)); /* Cache loop nors into a temp CDLayer. */ - loop_nors_dst = static_cast(CustomData_get_layer(ldata_dst, CD_NORMAL)); + loop_nors_dst = static_cast( + CustomData_get_layer_for_write(ldata_dst, CD_NORMAL, me_dst->totloop)); const bool do_loop_nors_dst = (loop_nors_dst == nullptr); if (do_loop_nors_dst) { loop_nors_dst = static_cast( @@ -337,9 +338,9 @@ static void data_transfer_dtdata_type_postprocess(Object * /*ob_src*/, const float(*poly_nors_dst)[3] = BKE_mesh_poly_normals_ensure(me_dst); float(*loop_nors_dst)[3] = static_cast( - CustomData_get_layer(ldata_dst, CD_NORMAL)); + CustomData_get_layer_for_write(ldata_dst, CD_NORMAL, me_dst->totloop)); short(*custom_nors_dst)[2] = static_cast( - CustomData_get_layer(ldata_dst, CD_CUSTOMLOOPNORMAL)); + CustomData_get_layer_for_write(ldata_dst, CD_CUSTOMLOOPNORMAL, me_dst->totloop)); if (!custom_nors_dst) { custom_nors_dst = static_cast(CustomData_add_layer( @@ -514,7 +515,6 @@ static bool data_transfer_layersmapping_cdlayers_multisrc_to_dst(ListBase *r_map const bool use_delete, const CustomData *cd_src, CustomData *cd_dst, - const bool use_dupref_dst, const int tolayers, const bool *use_layers_src, const int num_layers_src, @@ -571,15 +571,7 @@ static bool data_transfer_layersmapping_cdlayers_multisrc_to_dst(ListBase *r_map continue; } data_src = CustomData_get_layer_n(cd_src, cddata_type, idx_src); - /* If dest is a evaluated mesh (from modifier), - * we do not want to overwrite cdlayers of orig mesh! */ - if (use_dupref_dst) { - data_dst = CustomData_duplicate_referenced_layer_n( - cd_dst, cddata_type, idx_src, num_elem_dst); - } - else { - data_dst = CustomData_get_layer_n(cd_dst, cddata_type, idx_src); - } + data_dst = CustomData_get_layer_n_for_write(cd_dst, cddata_type, idx_src, num_elem_dst); data_transfer_layersmapping_add_item_cd(r_map, cddata_type, mix_mode, @@ -627,15 +619,7 @@ static bool data_transfer_layersmapping_cdlayers_multisrc_to_dst(ListBase *r_map data_dst_to_delete[idx_dst] = false; } if (r_map) { - /* If dest is a evaluated mesh (from modifier), - * we do not want to overwrite cdlayers of orig mesh! */ - if (use_dupref_dst) { - data_dst = CustomData_duplicate_referenced_layer_n( - cd_dst, cddata_type, idx_dst, num_elem_dst); - } - else { - data_dst = CustomData_get_layer_n(cd_dst, cddata_type, idx_dst); - } + data_dst = CustomData_get_layer_n_for_write(cd_dst, cddata_type, idx_dst, num_elem_dst); data_transfer_layersmapping_add_item_cd(r_map, cddata_type, mix_mode, @@ -677,9 +661,8 @@ static bool data_transfer_layersmapping_cdlayers(ListBase *r_map, const int num_elem_dst, const bool use_create, const bool use_delete, - CustomData *cd_src, + const CustomData *cd_src, CustomData *cd_dst, - const bool use_dupref_dst, const int fromlayers, const int tolayers, cd_datatransfer_interp interp, @@ -697,18 +680,13 @@ static bool data_transfer_layersmapping_cdlayers(ListBase *r_map, return true; } - data_dst = CustomData_get_layer(cd_dst, cddata_type); + data_dst = CustomData_get_layer_for_write(cd_dst, cddata_type, num_elem_dst); if (!data_dst) { if (!use_create) { return true; } data_dst = CustomData_add_layer(cd_dst, cddata_type, CD_SET_DEFAULT, nullptr, num_elem_dst); } - else if (use_dupref_dst && r_map) { - /* If dest is a evaluated mesh (from modifier), - * we do not want to overwrite cdlayers of orig mesh! */ - data_dst = CustomData_duplicate_referenced_layer(cd_dst, cddata_type, num_elem_dst); - } if (r_map) { data_transfer_layersmapping_add_item_cd(r_map, @@ -740,15 +718,7 @@ static bool data_transfer_layersmapping_cdlayers(ListBase *r_map, if (tolayers >= 0) { /* Real-layer index */ idx_dst = tolayers; - /* If dest is a evaluated mesh (from modifier), - * we do not want to overwrite cdlayers of orig mesh! */ - if (use_dupref_dst && r_map) { - data_dst = CustomData_duplicate_referenced_layer_n( - cd_dst, cddata_type, idx_dst, num_elem_dst); - } - else { - data_dst = CustomData_get_layer_n(cd_dst, cddata_type, idx_dst); - } + data_dst = CustomData_get_layer_n_for_write(cd_dst, cddata_type, idx_dst, num_elem_dst); } else if (tolayers == DT_LAYERS_ACTIVE_DST) { if ((idx_dst = CustomData_get_active_layer(cd_dst, cddata_type)) == -1) { @@ -759,15 +729,7 @@ static bool data_transfer_layersmapping_cdlayers(ListBase *r_map, cd_dst, cddata_type, CD_SET_DEFAULT, nullptr, num_elem_dst); } else { - /* If dest is a evaluated mesh (from modifier), - * we do not want to overwrite cdlayers of orig mesh! */ - if (use_dupref_dst && r_map) { - data_dst = CustomData_duplicate_referenced_layer_n( - cd_dst, cddata_type, idx_dst, num_elem_dst); - } - else { - data_dst = CustomData_get_layer_n(cd_dst, cddata_type, idx_dst); - } + data_dst = CustomData_get_layer_n_for_write(cd_dst, cddata_type, idx_dst, num_elem_dst); } } else if (tolayers == DT_LAYERS_INDEX_DST) { @@ -782,15 +744,7 @@ static bool data_transfer_layersmapping_cdlayers(ListBase *r_map, CustomData_add_layer(cd_dst, cddata_type, CD_SET_DEFAULT, nullptr, num_elem_dst); } } - /* If dest is a evaluated mesh (from modifier), - * we do not want to overwrite cdlayers of orig mesh! */ - if (use_dupref_dst && r_map) { - data_dst = CustomData_duplicate_referenced_layer_n( - cd_dst, cddata_type, idx_dst, num_elem_dst); - } - else { - data_dst = CustomData_get_layer_n(cd_dst, cddata_type, idx_dst); - } + data_dst = CustomData_get_layer_n_for_write(cd_dst, cddata_type, idx_dst, num_elem_dst); } else if (tolayers == DT_LAYERS_NAME_DST) { const char *name = CustomData_get_layer_name(cd_src, cddata_type, idx_src); @@ -802,15 +756,7 @@ static bool data_transfer_layersmapping_cdlayers(ListBase *r_map, cd_dst, cddata_type, CD_SET_DEFAULT, nullptr, num_elem_dst, name); idx_dst = CustomData_get_named_layer(cd_dst, cddata_type, name); } - /* If dest is a evaluated mesh (from modifier), - * we do not want to overwrite cdlayers of orig mesh! */ - if (use_dupref_dst && r_map) { - data_dst = CustomData_duplicate_referenced_layer_n( - cd_dst, cddata_type, idx_dst, num_elem_dst); - } - else { - data_dst = CustomData_get_layer_n(cd_dst, cddata_type, idx_dst); - } + data_dst = CustomData_get_layer_n_for_write(cd_dst, cddata_type, idx_dst, num_elem_dst); } else { return false; @@ -853,7 +799,6 @@ static bool data_transfer_layersmapping_cdlayers(ListBase *r_map, use_delete, cd_src, cd_dst, - use_dupref_dst, tolayers, use_layers_src, num_src, @@ -909,7 +854,6 @@ static bool data_transfer_layersmapping_generate(ListBase *r_map, use_delete, cd_src, cd_dst, - me_dst != ob_dst->data, fromlayers, tolayers, interp, @@ -962,7 +906,6 @@ static bool data_transfer_layersmapping_generate(ListBase *r_map, use_delete, cd_src, cd_dst, - me_dst != ob_dst->data, fromlayers, tolayers, interp, @@ -1007,7 +950,8 @@ static bool data_transfer_layersmapping_generate(ListBase *r_map, mix_factor, mix_weights, CustomData_get_layer_named(&me_src->edata, CD_PROP_BOOL, "sharp_edge"), - CustomData_get_layer_named(&me_dst->edata, CD_PROP_BOOL, "sharp_edge"), + CustomData_get_layer_named_for_write( + &me_dst->edata, CD_PROP_BOOL, "sharp_edge", me_dst->totedge), interp, interp_data); return true; @@ -1040,7 +984,6 @@ static bool data_transfer_layersmapping_generate(ListBase *r_map, use_delete, cd_src, cd_dst, - me_dst != ob_dst->data, fromlayers, tolayers, interp, @@ -1072,7 +1015,6 @@ static bool data_transfer_layersmapping_generate(ListBase *r_map, use_delete, cd_src, cd_dst, - me_dst != ob_dst->data, fromlayers, tolayers, interp, diff --git a/source/blender/blenkernel/intern/deform.c b/source/blender/blenkernel/intern/deform.c index c2e5434f0e1..8bc98270054 100644 --- a/source/blender/blenkernel/intern/deform.c +++ b/source/blender/blenkernel/intern/deform.c @@ -1395,10 +1395,10 @@ bool data_transfer_layersmapping_vgroups(ListBase *r_map, const MDeformVert *data_src = CustomData_get_layer(cd_src, CD_MDEFORMVERT); - MDeformVert *data_dst = CustomData_get_layer(cd_dst, CD_MDEFORMVERT); + MDeformVert *data_dst = CustomData_get_layer_for_write(cd_dst, CD_MDEFORMVERT, num_elem_dst); if (data_dst && use_dupref_dst && r_map) { /* If dest is a derivedmesh, we do not want to overwrite cdlayers of org mesh! */ - data_dst = CustomData_duplicate_referenced_layer(cd_dst, CD_MDEFORMVERT, num_elem_dst); + data_dst = CustomData_get_layer_for_write(cd_dst, CD_MDEFORMVERT, num_elem_dst); } if (fromlayers == DT_LAYERS_ACTIVE_SRC || fromlayers >= 0) { diff --git a/source/blender/blenkernel/intern/dynamicpaint.c b/source/blender/blenkernel/intern/dynamicpaint.c index 21614b53eae..4f24a503a4d 100644 --- a/source/blender/blenkernel/intern/dynamicpaint.c +++ b/source/blender/blenkernel/intern/dynamicpaint.c @@ -1934,8 +1934,8 @@ static Mesh *dynamicPaint_Modifier_apply(DynamicPaintModifierData *pmd, Object * } /* paint layer */ - MLoopCol *mloopcol = CustomData_get_layer_named( - &result->ldata, CD_PROP_BYTE_COLOR, surface->output_name); + MLoopCol *mloopcol = CustomData_get_layer_named_for_write( + &result->ldata, CD_PROP_BYTE_COLOR, surface->output_name, result->totloop); /* if output layer is lost from a constructive modifier, re-add it */ if (!mloopcol && dynamicPaint_outputLayerExists(surface, ob, 0)) { mloopcol = CustomData_add_layer_named(&result->ldata, @@ -1947,8 +1947,8 @@ static Mesh *dynamicPaint_Modifier_apply(DynamicPaintModifierData *pmd, Object * } /* wet layer */ - MLoopCol *mloopcol_wet = CustomData_get_layer_named( - &result->ldata, CD_PROP_BYTE_COLOR, surface->output_name2); + MLoopCol *mloopcol_wet = CustomData_get_layer_named_for_write( + &result->ldata, CD_PROP_BYTE_COLOR, surface->output_name2, result->totloop); /* if output layer is lost from a constructive modifier, re-add it */ if (!mloopcol_wet && dynamicPaint_outputLayerExists(surface, ob, 1)) { mloopcol_wet = CustomData_add_layer_named(&result->ldata, @@ -1978,7 +1978,8 @@ static Mesh *dynamicPaint_Modifier_apply(DynamicPaintModifierData *pmd, Object * /* vertex group paint */ else if (surface->type == MOD_DPAINT_SURFACE_T_WEIGHT) { int defgrp_index = BKE_object_defgroup_name_index(ob, surface->output_name); - MDeformVert *dvert = CustomData_get_layer(&result->vdata, CD_MDEFORMVERT); + MDeformVert *dvert = CustomData_get_layer_for_write( + &result->vdata, CD_MDEFORMVERT, result->totvert); float *weight = (float *)sData->type_data; /* apply weights into a vertex group, if doesn't exists add a new layer */ diff --git a/source/blender/blenkernel/intern/key.cc b/source/blender/blenkernel/intern/key.cc index 0dfb6d2c093..4b3a42e4c8a 100644 --- a/source/blender/blenkernel/intern/key.cc +++ b/source/blender/blenkernel/intern/key.cc @@ -2222,7 +2222,7 @@ void BKE_keyblock_convert_to_mesh(const KeyBlock *kb, } void BKE_keyblock_mesh_calc_normals(const KeyBlock *kb, - const Mesh *mesh, + Mesh *mesh, float (*r_vert_normals)[3], float (*r_poly_normals)[3], float (*r_loop_normals)[3]) @@ -2272,8 +2272,8 @@ void BKE_keyblock_mesh_calc_normals(const KeyBlock *kb, vert_normals); } if (loop_normals_needed) { - short(*clnors)[2] = static_cast( - CustomData_get_layer(&mesh->ldata, CD_CUSTOMLOOPNORMAL)); /* May be nullptr. */ + short(*clnors)[2] = static_cast(CustomData_get_layer_for_write( + &mesh->ldata, CD_CUSTOMLOOPNORMAL, mesh->totloop)); /* May be nullptr. */ const bool *sharp_edges = static_cast( CustomData_get_layer_named(&mesh->edata, CD_PROP_BOOL, "sharp_edge")); BKE_mesh_normals_loop_split(positions, diff --git a/source/blender/blenkernel/intern/mesh.cc b/source/blender/blenkernel/intern/mesh.cc index 47832e669ed..23276b524b4 100644 --- a/source/blender/blenkernel/intern/mesh.cc +++ b/source/blender/blenkernel/intern/mesh.cc @@ -1601,7 +1601,7 @@ void BKE_mesh_transform(Mesh *me, const float mat[4][4], bool do_keys) /* don't update normals, caller can do this explicitly. * We do update loop normals though, those may not be auto-generated * (see e.g. STL import script)! */ - float(*lnors)[3] = (float(*)[3])CustomData_duplicate_referenced_layer( + float(*lnors)[3] = (float(*)[3])CustomData_get_layer_for_write( &me->ldata, CD_NORMAL, me->totloop); if (lnors) { float m3[3][3]; @@ -1815,7 +1815,8 @@ static float (*ensure_corner_normal_layer(Mesh &mesh))[3] { float(*r_loop_normals)[3]; if (CustomData_has_layer(&mesh.ldata, CD_NORMAL)) { - r_loop_normals = (float(*)[3])CustomData_get_layer(&mesh.ldata, CD_NORMAL); + r_loop_normals = (float(*)[3])CustomData_get_layer_for_write( + &mesh.ldata, CD_NORMAL, mesh.totloop); memset(r_loop_normals, 0, sizeof(float[3]) * mesh.totloop); } else { @@ -1838,7 +1839,8 @@ void BKE_mesh_calc_normals_split_ex(Mesh *mesh, const float split_angle = (mesh->flag & ME_AUTOSMOOTH) != 0 ? mesh->smoothresh : float(M_PI); /* may be nullptr */ - short(*clnors)[2] = (short(*)[2])CustomData_get_layer(&mesh->ldata, CD_CUSTOMLOOPNORMAL); + short(*clnors)[2] = (short(*)[2])CustomData_get_layer_for_write( + &mesh->ldata, CD_CUSTOMLOOPNORMAL, mesh->totloop); const bool *sharp_edges = static_cast( CustomData_get_layer_named(&mesh->edata, CD_PROP_BOOL, "sharp_edge")); diff --git a/source/blender/blenkernel/intern/mesh_boolean_convert.cc b/source/blender/blenkernel/intern/mesh_boolean_convert.cc index 8ae08972cad..74033704565 100644 --- a/source/blender/blenkernel/intern/mesh_boolean_convert.cc +++ b/source/blender/blenkernel/intern/mesh_boolean_convert.cc @@ -641,11 +641,14 @@ static void copy_or_interp_loop_attributes(Mesh *dest_mesh, } int source_layer_type_index = source_layer_i - source_cd->typemap[ty]; BLI_assert(target_layer_type_index != -1 && source_layer_type_index >= 0); + const int size = CustomData_sizeof(ty); for (int j = 0; j < orig_mp->totloop; ++j) { - src_blocks_ofs[j] = CustomData_get_n( - source_cd, ty, orig_mp->loopstart + j, source_layer_type_index); + const void *layer = CustomData_get_layer_n(source_cd, ty, source_layer_type_index); + src_blocks_ofs[j] = POINTER_OFFSET(layer, size * (orig_mp->loopstart + j)); } - void *dst_block_ofs = CustomData_get_n(target_cd, ty, loop_index, target_layer_type_index); + void *dst_layer = CustomData_get_layer_n_for_write( + target_cd, ty, target_layer_type_index, dest_mesh->totloop); + void *dst_block_ofs = POINTER_OFFSET(dst_layer, size * loop_index); CustomData_bmesh_interp_n(target_cd, src_blocks_ofs.data(), weights.data(), diff --git a/source/blender/blenkernel/intern/mesh_evaluate.cc b/source/blender/blenkernel/intern/mesh_evaluate.cc index 629d9e89ed6..3c25cd2e07e 100644 --- a/source/blender/blenkernel/intern/mesh_evaluate.cc +++ b/source/blender/blenkernel/intern/mesh_evaluate.cc @@ -595,15 +595,15 @@ void BKE_mesh_polygon_flip_ex(const MPoly *mpoly, } } -void BKE_mesh_polygon_flip(const MPoly *mpoly, MLoop *mloop, CustomData *ldata) +void BKE_mesh_polygon_flip(const MPoly *mpoly, MLoop *mloop, CustomData *ldata, const int totloop) { - MDisps *mdisp = (MDisps *)CustomData_get_layer(ldata, CD_MDISPS); + MDisps *mdisp = (MDisps *)CustomData_get_layer_for_write(ldata, CD_MDISPS, totloop); BKE_mesh_polygon_flip_ex(mpoly, mloop, ldata, nullptr, mdisp, true); } void BKE_mesh_polys_flip(const MPoly *mpoly, MLoop *mloop, CustomData *ldata, int totpoly) { - MDisps *mdisp = (MDisps *)CustomData_get_layer(ldata, CD_MDISPS); + MDisps *mdisp = (MDisps *)CustomData_get_layer_for_write(ldata, CD_MDISPS, totpoly); const MPoly *mp; int i; diff --git a/source/blender/blenkernel/intern/mesh_legacy_convert.cc b/source/blender/blenkernel/intern/mesh_legacy_convert.cc index 07fe17794ff..0f95158e430 100644 --- a/source/blender/blenkernel/intern/mesh_legacy_convert.cc +++ b/source/blender/blenkernel/intern/mesh_legacy_convert.cc @@ -289,6 +289,7 @@ void BKE_mesh_do_versions_cd_flag_init(Mesh *mesh) static void bm_corners_to_loops_ex(ID *id, CustomData *fdata, + const int totface, CustomData *ldata, MFace *mface, int totloop, @@ -300,10 +301,11 @@ static void bm_corners_to_loops_ex(ID *id, MFace *mf = mface + findex; for (int i = 0; i < numTex; i++) { - const MTFace *texface = (const MTFace *)CustomData_get_n(fdata, CD_MTFACE, findex, i); + const MTFace *texface = (const MTFace *)CustomData_get_n_for_write( + fdata, CD_MTFACE, findex, i, totface); blender::float2 *uv = static_cast( - CustomData_get_n(ldata, CD_PROP_FLOAT2, loopstart, i)); + CustomData_get_n_for_write(ldata, CD_PROP_FLOAT2, loopstart, i, totloop)); copy_v2_v2(*uv, texface->uv[0]); uv++; copy_v2_v2(*uv, texface->uv[1]); @@ -318,8 +320,10 @@ static void bm_corners_to_loops_ex(ID *id, } for (int i = 0; i < numCol; i++) { - MLoopCol *mloopcol = (MLoopCol *)CustomData_get_n(ldata, CD_PROP_BYTE_COLOR, loopstart, i); - const MCol *mcol = (const MCol *)CustomData_get_n(fdata, CD_MCOL, findex, i); + MLoopCol *mloopcol = (MLoopCol *)CustomData_get_n_for_write( + ldata, CD_PROP_BYTE_COLOR, loopstart, i, totloop); + const MCol *mcol = (const MCol *)CustomData_get_n_for_write( + fdata, CD_MCOL, findex, i, totface); MESH_MLOOPCOL_FROM_MCOL(mloopcol, &mcol[0]); mloopcol++; @@ -334,9 +338,10 @@ static void bm_corners_to_loops_ex(ID *id, } if (CustomData_has_layer(fdata, CD_TESSLOOPNORMAL)) { - float(*loop_normals)[3] = (float(*)[3])CustomData_get(ldata, loopstart, CD_NORMAL); - const short(*tessloop_normals)[3] = (short(*)[3])CustomData_get( - fdata, findex, CD_TESSLOOPNORMAL); + float(*loop_normals)[3] = (float(*)[3])CustomData_get_for_write( + ldata, loopstart, CD_NORMAL, totloop); + const short(*tessloop_normals)[3] = (short(*)[3])CustomData_get_for_write( + fdata, findex, CD_TESSLOOPNORMAL, totface); const int max = mf->v4 ? 4 : 3; for (int i = 0; i < max; i++, loop_normals++, tessloop_normals++) { @@ -345,8 +350,8 @@ static void bm_corners_to_loops_ex(ID *id, } if (CustomData_has_layer(fdata, CD_MDISPS)) { - MDisps *ld = (MDisps *)CustomData_get(ldata, loopstart, CD_MDISPS); - const MDisps *fd = (const MDisps *)CustomData_get(fdata, findex, CD_MDISPS); + MDisps *ld = (MDisps *)CustomData_get_for_write(ldata, loopstart, CD_MDISPS, totloop); + const MDisps *fd = (const MDisps *)CustomData_get_for_write(fdata, findex, CD_MDISPS, totface); const float(*disps)[3] = fd->disps; int tot = mf->v4 ? 4 : 3; int corners; @@ -443,7 +448,7 @@ static void convert_mfaces_to_mpolys(ID *id, totpoly = totface_i; mpoly = (MPoly *)CustomData_add_layer(pdata, CD_MPOLY, CD_SET_DEFAULT, nullptr, totpoly); int *material_indices = static_cast( - CustomData_get_layer_named(pdata, CD_PROP_INT32, "material_index")); + CustomData_get_layer_named_for_write(pdata, CD_PROP_INT32, "material_index", totpoly)); if (material_indices == nullptr) { material_indices = static_cast(CustomData_add_layer_named( pdata, CD_PROP_INT32, CD_SET_DEFAULT, nullptr, totpoly, "material_index")); @@ -515,7 +520,8 @@ static void convert_mfaces_to_mpolys(ID *id, #undef ML - bm_corners_to_loops_ex(id, fdata, ldata, mface, totloop, i, mp->loopstart, numTex, numCol); + bm_corners_to_loops_ex( + id, fdata, totface_i, ldata, mface, totloop, i, mp->loopstart, numTex, numCol); if (polyindex) { *polyindex = i; @@ -804,7 +810,7 @@ static void mesh_loops_to_tessdata(CustomData *fdata, uint(*lidx)[4]; for (i = 0; i < numUV; i++) { - MTFace *texface = (MTFace *)CustomData_get_layer_n(fdata, CD_MTFACE, i); + MTFace *texface = (MTFace *)CustomData_get_layer_n_for_write(fdata, CD_MTFACE, i, num_faces); const blender::float2 *uv = static_cast( CustomData_get_layer_n(ldata, CD_PROP_FLOAT2, i)); @@ -817,7 +823,7 @@ static void mesh_loops_to_tessdata(CustomData *fdata, } for (i = 0; i < numCol; i++) { - MCol(*mcol)[4] = (MCol(*)[4])CustomData_get_layer_n(fdata, CD_MCOL, i); + MCol(*mcol)[4] = (MCol(*)[4])CustomData_get_layer_n_for_write(fdata, CD_MCOL, i, num_faces); const MLoopCol *mloopcol = (const MLoopCol *)CustomData_get_layer_n( ldata, CD_PROP_BYTE_COLOR, i); @@ -1835,7 +1841,7 @@ void BKE_mesh_legacy_convert_verts_to_positions(Mesh *mesh) using namespace blender; using namespace blender::bke; - const Span verts(static_cast(CustomData_get_layer(&mesh->vdata, CD_MVERT)), + const Span verts(static_cast(CustomData_get_layer(&mesh->vdata, CD_MVERT)), mesh->totvert); MutableSpan positions( static_cast(CustomData_add_layer_named( diff --git a/source/blender/blenkernel/intern/mesh_merge_customdata.cc b/source/blender/blenkernel/intern/mesh_merge_customdata.cc index f219ad3ae19..99a45b137a4 100644 --- a/source/blender/blenkernel/intern/mesh_merge_customdata.cc +++ b/source/blender/blenkernel/intern/mesh_merge_customdata.cc @@ -125,7 +125,8 @@ void BKE_mesh_merge_customdata_for_apply_modifier(Mesh *me) Vector mloopuv_layers; mloopuv_layers.reserve(mloopuv_layers_num); for (int a = 0; a < mloopuv_layers_num; a++) { - float2 *mloopuv = static_cast(CustomData_get_layer_n(&me->ldata, CD_PROP_FLOAT2, a)); + float2 *mloopuv = static_cast( + CustomData_get_layer_n_for_write(&me->ldata, CD_PROP_FLOAT2, a, me->totloop)); mloopuv_layers.append_unchecked(mloopuv); } diff --git a/source/blender/blenkernel/intern/mesh_mirror.cc b/source/blender/blenkernel/intern/mesh_mirror.cc index 3f5d18e86b9..21b8ab83ebd 100644 --- a/source/blender/blenkernel/intern/mesh_mirror.cc +++ b/source/blender/blenkernel/intern/mesh_mirror.cc @@ -298,7 +298,7 @@ Mesh *BKE_mesh_mirror_apply_mirror_on_axis_for_modifier(MirrorModifierData *mmd, totshape = CustomData_number_of_layers(&result->vdata, CD_SHAPEKEY); for (a = 0; a < totshape; a++) { float(*cos)[3] = static_cast( - CustomData_get_layer_n(&result->vdata, CD_SHAPEKEY, a)); + CustomData_get_layer_n_for_write(&result->vdata, CD_SHAPEKEY, a, result->totvert)); for (i = maxVerts; i < result->totvert; i++) { mul_m4_v3(mtx, cos[i]); } @@ -361,7 +361,7 @@ Mesh *BKE_mesh_mirror_apply_mirror_on_axis_for_modifier(MirrorModifierData *mmd, for (a = 0; a < totuv; a++) { float(*dmloopuv)[2] = static_cast( - CustomData_get_layer_n(&result->ldata, CD_PROP_FLOAT2, a)); + CustomData_get_layer_n_for_write(&result->ldata, CD_PROP_FLOAT2, a, result->totloop)); int j = maxLoops; dmloopuv += j; /* second set of loops only */ for (; j-- > 0; dmloopuv++) { @@ -397,7 +397,8 @@ Mesh *BKE_mesh_mirror_apply_mirror_on_axis_for_modifier(MirrorModifierData *mmd, float(*loop_normals)[3] = static_cast( MEM_calloc_arrayN(size_t(totloop), sizeof(*loop_normals), __func__)); CustomData *ldata = &result->ldata; - short(*clnors)[2] = static_cast(CustomData_get_layer(ldata, CD_CUSTOMLOOPNORMAL)); + short(*clnors)[2] = static_cast( + CustomData_get_layer_for_write(ldata, CD_CUSTOMLOOPNORMAL, totloop)); MLoopNorSpaceArray lnors_spacearr = {nullptr}; /* The transform matrix of a normal must be diff --git a/source/blender/blenkernel/intern/mesh_normals.cc b/source/blender/blenkernel/intern/mesh_normals.cc index aa5b2d761f7..db700ec3e12 100644 --- a/source/blender/blenkernel/intern/mesh_normals.cc +++ b/source/blender/blenkernel/intern/mesh_normals.cc @@ -1913,7 +1913,8 @@ static void mesh_set_custom_normals(Mesh *mesh, float (*r_custom_nors)[3], const short(*clnors)[2]; const int numloops = mesh->totloop; - clnors = (short(*)[2])CustomData_get_layer(&mesh->ldata, CD_CUSTOMLOOPNORMAL); + clnors = (short(*)[2])CustomData_get_layer_for_write( + &mesh->ldata, CD_CUSTOMLOOPNORMAL, mesh->totloop); if (clnors != nullptr) { memset(clnors, 0, sizeof(*clnors) * size_t(numloops)); } diff --git a/source/blender/blenkernel/intern/mesh_remap.cc b/source/blender/blenkernel/intern/mesh_remap.cc index 0b073e8d77f..c36fbfb3274 100644 --- a/source/blender/blenkernel/intern/mesh_remap.cc +++ b/source/blender/blenkernel/intern/mesh_remap.cc @@ -1357,10 +1357,11 @@ void BKE_mesh_remap_calc_loops_from_mesh(const int mode, } if (need_lnors_dst) { short(*custom_nors_dst)[2] = static_cast( - CustomData_get_layer(ldata_dst, CD_CUSTOMLOOPNORMAL)); + CustomData_get_layer_for_write(ldata_dst, CD_CUSTOMLOOPNORMAL, numloops_dst)); /* Cache loop normals into a temporary custom data layer. */ - loop_nors_dst = static_cast(CustomData_get_layer(ldata_dst, CD_NORMAL)); + loop_nors_dst = static_cast( + CustomData_get_layer_for_write(ldata_dst, CD_NORMAL, numloops_dst)); const bool do_loop_nors_dst = (loop_nors_dst == nullptr); if (!loop_nors_dst) { loop_nors_dst = static_cast( diff --git a/source/blender/blenkernel/intern/mesh_validate.cc b/source/blender/blenkernel/intern/mesh_validate.cc index e834ef70b4e..478c429e7aa 100644 --- a/source/blender/blenkernel/intern/mesh_validate.cc +++ b/source/blender/blenkernel/intern/mesh_validate.cc @@ -1071,21 +1071,22 @@ bool BKE_mesh_validate(Mesh *me, const bool do_verbose, const bool cddata_check_ MutableSpan polys = me->polys_for_write(); MutableSpan loops = me->loops_for_write(); - BKE_mesh_validate_arrays(me, - reinterpret_cast(positions.data()), - positions.size(), - edges.data(), - edges.size(), - (MFace *)CustomData_get_layer(&me->fdata, CD_MFACE), - me->totface, - loops.data(), - loops.size(), - polys.data(), - polys.size(), - me->deform_verts_for_write().data(), - do_verbose, - true, - &changed); + BKE_mesh_validate_arrays( + me, + reinterpret_cast(positions.data()), + positions.size(), + edges.data(), + edges.size(), + (MFace *)CustomData_get_layer_for_write(&me->fdata, CD_MFACE, me->totface), + me->totface, + loops.data(), + loops.size(), + polys.data(), + polys.size(), + me->deform_verts_for_write().data(), + do_verbose, + true, + &changed); if (changed) { DEG_id_tag_update(&me->id, ID_RECALC_GEOMETRY_ALL_MODES); @@ -1122,21 +1123,22 @@ bool BKE_mesh_is_valid(Mesh *me) MutableSpan polys = me->polys_for_write(); MutableSpan loops = me->loops_for_write(); - is_valid &= BKE_mesh_validate_arrays(me, - reinterpret_cast(positions.data()), - positions.size(), - edges.data(), - edges.size(), - (MFace *)CustomData_get_layer(&me->fdata, CD_MFACE), - me->totface, - loops.data(), - loops.size(), - polys.data(), - polys.size(), - me->deform_verts_for_write().data(), - do_verbose, - do_fixes, - &changed); + is_valid &= BKE_mesh_validate_arrays( + me, + reinterpret_cast(positions.data()), + positions.size(), + edges.data(), + edges.size(), + (MFace *)CustomData_get_layer_for_write(&me->fdata, CD_MFACE, me->totface), + me->totface, + loops.data(), + loops.size(), + polys.data(), + polys.size(), + me->deform_verts_for_write().data(), + do_verbose, + do_fixes, + &changed); if (!me->runtime->vert_normals_dirty) { BLI_assert(me->runtime->vert_normals || me->totvert == 0); @@ -1186,7 +1188,7 @@ void BKE_mesh_strip_loose_faces(Mesh *me) /* NOTE: We need to keep this for edge creation (for now?), and some old `readfile.c` code. */ MFace *f; int a, b; - MFace *mfaces = (MFace *)CustomData_get_layer(&me->fdata, CD_MFACE); + MFace *mfaces = (MFace *)CustomData_get_layer_for_write(&me->fdata, CD_MFACE, me->totface); for (a = b = 0, f = mfaces; a < me->totface; a++, f++) { if (f->v3) { @@ -1323,7 +1325,7 @@ void BKE_mesh_calc_edges_tessface(Mesh *mesh) { const int numFaces = mesh->totface; EdgeSet *eh = BLI_edgeset_new_ex(__func__, BLI_EDGEHASH_SIZE_GUESS_FROM_POLYS(numFaces)); - MFace *mfaces = (MFace *)CustomData_get_layer(&mesh->fdata, CD_MFACE); + MFace *mfaces = (MFace *)CustomData_get_layer_for_write(&mesh->fdata, CD_MFACE, mesh->totface); MFace *mf = mfaces; for (int i = 0; i < numFaces; i++, mf++) { @@ -1347,8 +1349,8 @@ void BKE_mesh_calc_edges_tessface(Mesh *mesh) CustomData_add_layer(&edgeData, CD_MEDGE, CD_SET_DEFAULT, nullptr, numEdges); CustomData_add_layer(&edgeData, CD_ORIGINDEX, CD_SET_DEFAULT, nullptr, numEdges); - MEdge *med = (MEdge *)CustomData_get_layer(&edgeData, CD_MEDGE); - int *index = (int *)CustomData_get_layer(&edgeData, CD_ORIGINDEX); + MEdge *med = (MEdge *)CustomData_get_layer_for_write(&edgeData, CD_MEDGE, mesh->totedge); + int *index = (int *)CustomData_get_layer_for_write(&edgeData, CD_ORIGINDEX, mesh->totedge); EdgeSetIterator *ehi = BLI_edgesetIterator_new(eh); for (int i = 0; BLI_edgesetIterator_isDone(ehi) == false; diff --git a/source/blender/blenkernel/intern/mesh_wrapper.cc b/source/blender/blenkernel/intern/mesh_wrapper.cc index 33c67e606b9..9c8c36cacb8 100644 --- a/source/blender/blenkernel/intern/mesh_wrapper.cc +++ b/source/blender/blenkernel/intern/mesh_wrapper.cc @@ -340,7 +340,7 @@ static Mesh *mesh_wrapper_ensure_subdivision(Mesh *me) if (use_clnors) { float(*lnors)[3] = static_cast( - CustomData_get_layer(&subdiv_mesh->ldata, CD_NORMAL)); + CustomData_get_layer_for_write(&subdiv_mesh->ldata, CD_NORMAL, subdiv_mesh->totloop)); BLI_assert(lnors != nullptr); BKE_mesh_set_custom_normals(subdiv_mesh, lnors); CustomData_set_layer_flag(&me->ldata, CD_NORMAL, CD_FLAG_TEMPORARY); diff --git a/source/blender/blenkernel/intern/multires.cc b/source/blender/blenkernel/intern/multires.cc index 890a1379e4c..9b04435b91c 100644 --- a/source/blender/blenkernel/intern/multires.cc +++ b/source/blender/blenkernel/intern/multires.cc @@ -544,7 +544,8 @@ void multiresModifier_set_levels_from_disps(MultiresModifierData *mmd, Object *o static void multires_set_tot_mdisps(Mesh *me, int lvl) { - MDisps *mdisps = static_cast(CustomData_get_layer(&me->ldata, CD_MDISPS)); + MDisps *mdisps = static_cast( + CustomData_get_layer_for_write(&me->ldata, CD_MDISPS, me->totloop)); int i; if (mdisps) { @@ -663,8 +664,10 @@ static void multires_del_higher(MultiresModifierData *mmd, Object *ob, int lvl) multires_set_tot_mdisps(me, mmd->totlvl); multiresModifier_ensure_external_read(me, mmd); - mdisps = static_cast(CustomData_get_layer(&me->ldata, CD_MDISPS)); - gpm = static_cast(CustomData_get_layer(&me->ldata, CD_GRID_PAINT_MASK)); + mdisps = static_cast( + CustomData_get_layer_for_write(&me->ldata, CD_MDISPS, me->totloop)); + gpm = static_cast( + CustomData_get_layer_for_write(&me->ldata, CD_GRID_PAINT_MASK, me->totloop)); multires_force_sculpt_rebuild(ob); @@ -728,7 +731,8 @@ void multiresModifier_del_levels(MultiresModifierData *mmd, multires_set_tot_mdisps(me, mmd->totlvl); multiresModifier_ensure_external_read(me, mmd); - MDisps *mdisps = static_cast(CustomData_get_layer(&me->ldata, CD_MDISPS)); + MDisps *mdisps = static_cast( + CustomData_get_layer_for_write(&me->ldata, CD_MDISPS, me->totloop)); multires_force_sculpt_rebuild(ob); @@ -965,7 +969,8 @@ static void multiresModifier_disp_run( CCGElem **gridData, **subGridData; CCGKey key; const MPoly *mpoly = BKE_mesh_polys(me); - MDisps *mdisps = static_cast(CustomData_get_layer(&me->ldata, CD_MDISPS)); + MDisps *mdisps = static_cast( + CustomData_get_layer_for_write(&me->ldata, CD_MDISPS, me->totloop)); GridPaintMask *grid_paint_mask = nullptr; int *gridOffset; int i, gridSize, dGridSize, dSkip; @@ -973,8 +978,10 @@ static void multiresModifier_disp_run( /* this happens in the dm made by bmesh_mdisps_space_set */ if (dm2 && CustomData_has_layer(&dm2->loopData, CD_MDISPS)) { - mpoly = static_cast(CustomData_get_layer(&dm2->polyData, CD_MPOLY)); - mdisps = static_cast(CustomData_get_layer(&dm2->loopData, CD_MDISPS)); + mpoly = static_cast( + CustomData_get_layer_for_write(&dm2->polyData, CD_MPOLY, dm2->getNumPolys(dm))); + mdisps = static_cast( + CustomData_get_layer_for_write(&dm2->loopData, CD_MDISPS, dm2->getNumLoops(dm))); totloop = dm2->numLoopData; totpoly = dm2->numPolyData; } @@ -1006,7 +1013,7 @@ static void multiresModifier_disp_run( /* multires paint masks */ if (key.has_mask) { grid_paint_mask = static_cast( - CustomData_get_layer(&me->ldata, CD_GRID_PAINT_MASK)); + CustomData_get_layer_for_write(&me->ldata, CD_GRID_PAINT_MASK, me->totloop)); } /* when adding new faces in edit mode, need to allocate disps */ @@ -1176,7 +1183,8 @@ void multires_modifier_update_hidden(DerivedMesh *dm) CCGDerivedMesh *ccgdm = (CCGDerivedMesh *)dm; BLI_bitmap **grid_hidden = ccgdm->gridHidden; Mesh *me = static_cast(ccgdm->multires.ob->data); - MDisps *mdisps = static_cast(CustomData_get_layer(&me->ldata, CD_MDISPS)); + MDisps *mdisps = static_cast( + CustomData_get_layer_for_write(&me->ldata, CD_MDISPS, me->totloop)); int totlvl = ccgdm->multires.totlvl; int lvl = ccgdm->multires.lvl; @@ -1395,7 +1403,8 @@ static void multires_sync_levels(Scene *scene, Object *ob_src, Object *ob_dst) static void multires_apply_uniform_scale(Object *object, const float scale) { Mesh *mesh = (Mesh *)object->data; - MDisps *mdisps = static_cast(CustomData_get_layer(&mesh->ldata, CD_MDISPS)); + MDisps *mdisps = static_cast( + CustomData_get_layer_for_write(&mesh->ldata, CD_MDISPS, mesh->totloop)); for (int i = 0; i < mesh->totloop; i++) { MDisps *grid = &mdisps[i]; for (int j = 0; j < grid->totdisp; j++) { @@ -1479,7 +1488,8 @@ void multires_topology_changed(Mesh *me) int i, grid = 0; CustomData_external_read(&me->ldata, &me->id, CD_MASK_MDISPS, me->totloop); - mdisp = static_cast(CustomData_get_layer(&me->ldata, CD_MDISPS)); + mdisp = static_cast( + CustomData_get_layer_for_write(&me->ldata, CD_MDISPS, me->totloop)); if (!mdisp) { return; @@ -1514,7 +1524,8 @@ void multires_ensure_external_read(struct Mesh *mesh, int top_level) return; } - MDisps *mdisps = static_cast(CustomData_get_layer(&mesh->ldata, CD_MDISPS)); + MDisps *mdisps = static_cast( + CustomData_get_layer_for_write(&mesh->ldata, CD_MDISPS, mesh->totloop)); if (mdisps == nullptr) { mdisps = static_cast( CustomData_add_layer(&mesh->ldata, CD_MDISPS, CD_SET_DEFAULT, nullptr, mesh->totloop)); diff --git a/source/blender/blenkernel/intern/multires_reshape_subdivide.c b/source/blender/blenkernel/intern/multires_reshape_subdivide.c index e3cb6f52a41..4e74835fd9a 100644 --- a/source/blender/blenkernel/intern/multires_reshape_subdivide.c +++ b/source/blender/blenkernel/intern/multires_reshape_subdivide.c @@ -32,7 +32,7 @@ static void multires_subdivide_create_object_space_linear_grids(Mesh *mesh) const MPoly *polys = BKE_mesh_polys(mesh); const MLoop *loops = BKE_mesh_loops(mesh); - MDisps *mdisps = CustomData_get_layer(&mesh->ldata, CD_MDISPS); + MDisps *mdisps = CustomData_get_layer_for_write(&mesh->ldata, CD_MDISPS, mesh->totloop); const int totpoly = mesh->totpoly; for (int p = 0; p < totpoly; p++) { const MPoly *poly = &polys[p]; diff --git a/source/blender/blenkernel/intern/multires_reshape_util.c b/source/blender/blenkernel/intern/multires_reshape_util.c index 1c33fbb6dd8..dfc9fcfe0c9 100644 --- a/source/blender/blenkernel/intern/multires_reshape_util.c +++ b/source/blender/blenkernel/intern/multires_reshape_util.c @@ -103,8 +103,10 @@ static void context_init_lookup(MultiresReshapeContext *reshape_context) static void context_init_grid_pointers(MultiresReshapeContext *reshape_context) { Mesh *base_mesh = reshape_context->base_mesh; - reshape_context->mdisps = CustomData_get_layer(&base_mesh->ldata, CD_MDISPS); - reshape_context->grid_paint_masks = CustomData_get_layer(&base_mesh->ldata, CD_GRID_PAINT_MASK); + reshape_context->mdisps = CustomData_get_layer_for_write( + &base_mesh->ldata, CD_MDISPS, base_mesh->totloop); + reshape_context->grid_paint_masks = CustomData_get_layer_for_write( + &base_mesh->ldata, CD_GRID_PAINT_MASK, base_mesh->totloop); } static void context_init_commoon(MultiresReshapeContext *reshape_context) @@ -559,7 +561,7 @@ static void ensure_displacement_grid(MDisps *displacement_grid, const int level) static void ensure_displacement_grids(Mesh *mesh, const int grid_level) { const int num_grids = mesh->totloop; - MDisps *mdisps = CustomData_get_layer(&mesh->ldata, CD_MDISPS); + MDisps *mdisps = CustomData_get_layer_for_write(&mesh->ldata, CD_MDISPS, mesh->totloop); for (int grid_index = 0; grid_index < num_grids; grid_index++) { ensure_displacement_grid(&mdisps[grid_index], grid_level); } @@ -567,7 +569,8 @@ static void ensure_displacement_grids(Mesh *mesh, const int grid_level) static void ensure_mask_grids(Mesh *mesh, const int level) { - GridPaintMask *grid_paint_masks = CustomData_get_layer(&mesh->ldata, CD_GRID_PAINT_MASK); + GridPaintMask *grid_paint_masks = CustomData_get_layer_for_write( + &mesh->ldata, CD_GRID_PAINT_MASK, mesh->totloop); if (grid_paint_masks == NULL) { return; } diff --git a/source/blender/blenkernel/intern/multires_unsubdivide.c b/source/blender/blenkernel/intern/multires_unsubdivide.c index 28bd199a967..2ef074cd24d 100644 --- a/source/blender/blenkernel/intern/multires_unsubdivide.c +++ b/source/blender/blenkernel/intern/multires_unsubdivide.c @@ -938,7 +938,8 @@ static void multires_unsubdivide_prepare_original_bmesh_for_extract( bm_original_mesh, BM_VERT | BM_EDGE | BM_FACE, BM_ELEM_SELECT, false); /* Get the mapping data-layer. */ - context->base_to_orig_vmap = CustomData_get_layer_named(&base_mesh->vdata, CD_PROP_INT32, vname); + context->base_to_orig_vmap = CustomData_get_layer_named_for_write( + &base_mesh->vdata, CD_PROP_INT32, vname, base_mesh->totvert); /* Tag the base mesh vertices in the original mesh. */ for (int i = 0; i < base_mesh->totvert; i++) { @@ -1001,7 +1002,8 @@ static void multires_unsubdivide_extract_grids(MultiresUnsubdivideContext *conte int *orig_to_base_vmap = MEM_calloc_arrayN(bm_original_mesh->totvert, sizeof(int), "orig vmap"); int *base_to_orig_vmap = MEM_calloc_arrayN(base_mesh->totvert, sizeof(int), "base vmap"); - context->base_to_orig_vmap = CustomData_get_layer_named(&base_mesh->vdata, CD_PROP_INT32, vname); + context->base_to_orig_vmap = CustomData_get_layer_named_for_write( + &base_mesh->vdata, CD_PROP_INT32, vname, base_mesh->totvert); for (int i = 0; i < base_mesh->totvert; i++) { base_to_orig_vmap[i] = context->base_to_orig_vmap[i]; } diff --git a/source/blender/blenkernel/intern/object_facemap.c b/source/blender/blenkernel/intern/object_facemap.c index 42c57c706a4..de3bb5d4f41 100644 --- a/source/blender/blenkernel/intern/object_facemap.c +++ b/source/blender/blenkernel/intern/object_facemap.c @@ -176,7 +176,7 @@ static void object_fmap_remove_object_mode(Object *ob, bFaceMap *fmap, bool purg Mesh *me = ob->data; if (CustomData_has_layer(&me->pdata, CD_FACEMAP)) { - int *map = CustomData_get_layer(&me->pdata, CD_FACEMAP); + int *map = CustomData_get_layer_for_write(&me->pdata, CD_FACEMAP, me->totpoly); int i; if (map) { diff --git a/source/blender/blenkernel/intern/paint.cc b/source/blender/blenkernel/intern/paint.cc index 75d9f0d5b89..6628bc782f0 100644 --- a/source/blender/blenkernel/intern/paint.cc +++ b/source/blender/blenkernel/intern/paint.cc @@ -1709,7 +1709,8 @@ static void sculpt_update_object( ss->multires.active = false; ss->multires.modifier = nullptr; ss->multires.level = 0; - ss->vmask = static_cast(CustomData_get_layer(&me->vdata, CD_PAINT_MASK)); + ss->vmask = static_cast( + CustomData_get_layer_for_write(&me->vdata, CD_PAINT_MASK, me->totvert)); CustomDataLayer *layer; eAttrDomain domain; @@ -1736,14 +1737,15 @@ static void sculpt_update_object( /* Sculpt Face Sets. */ if (use_face_sets) { - ss->face_sets = static_cast( - CustomData_get_layer_named(&me->pdata, CD_PROP_INT32, ".sculpt_face_set")); + ss->face_sets = static_cast(CustomData_get_layer_named_for_write( + &me->pdata, CD_PROP_INT32, ".sculpt_face_set", me->totpoly)); } else { ss->face_sets = nullptr; } - ss->hide_poly = (bool *)CustomData_get_layer_named(&me->pdata, CD_PROP_BOOL, ".hide_poly"); + ss->hide_poly = (bool *)CustomData_get_layer_named_for_write( + &me->pdata, CD_PROP_BOOL, ".hide_poly", me->totpoly); ss->subdiv_ccg = me_eval->runtime->subdiv_ccg; @@ -1962,14 +1964,14 @@ int *BKE_sculpt_face_sets_ensure(Mesh *mesh) face_sets.finish(); } - return static_cast( - CustomData_get_layer_named(&mesh->pdata, CD_PROP_INT32, ".sculpt_face_set")); + return static_cast(CustomData_get_layer_named_for_write( + &mesh->pdata, CD_PROP_INT32, ".sculpt_face_set", mesh->totpoly)); } bool *BKE_sculpt_hide_poly_ensure(Mesh *mesh) { - bool *hide_poly = static_cast( - CustomData_get_layer_named(&mesh->pdata, CD_PROP_BOOL, ".hide_poly")); + bool *hide_poly = static_cast(CustomData_get_layer_named_for_write( + &mesh->pdata, CD_PROP_BOOL, ".hide_poly", mesh->totpoly)); if (hide_poly != nullptr) { return hide_poly; } diff --git a/source/blender/blenkernel/intern/particle.cc b/source/blender/blenkernel/intern/particle.cc index f871aa324be..83b665239fc 100644 --- a/source/blender/blenkernel/intern/particle.cc +++ b/source/blender/blenkernel/intern/particle.cc @@ -1888,7 +1888,8 @@ static float psys_interpolate_value_from_verts( return values[index]; case PART_FROM_FACE: case PART_FROM_VOLUME: { - MFace *mfaces = static_cast(CustomData_get_layer(&mesh->fdata, CD_MFACE)); + MFace *mfaces = static_cast( + CustomData_get_layer_for_write(&mesh->fdata, CD_MFACE, mesh->totface)); MFace *mf = &mfaces[index]; return interpolate_particle_value( values[mf->v1], values[mf->v2], values[mf->v3], values[mf->v4], fw, mf->v4); @@ -1981,9 +1982,10 @@ int psys_particle_dm_face_lookup(Mesh *mesh_final, index_mf_to_mpoly_deformed = nullptr; - mtessface_final = static_cast(CustomData_get_layer(&mesh_final->fdata, CD_MFACE)); + mtessface_final = static_cast( + CustomData_get_layer_for_write(&mesh_final->fdata, CD_MFACE, mesh_final->totface)); osface_final = static_cast( - CustomData_get_layer(&mesh_final->fdata, CD_ORIGSPACE)); + CustomData_get_layer_for_write(&mesh_final->fdata, CD_ORIGSPACE, mesh_final->totface)); if (osface_final == nullptr) { /* Assume we don't need osface_final data, and we get a direct 1-1 mapping... */ @@ -2102,7 +2104,7 @@ static int psys_map_index_on_dm(Mesh *mesh, /* modify the original weights to become * weights for the derived mesh face */ OrigSpaceFace *osface = static_cast( - CustomData_get_layer(&mesh->fdata, CD_ORIGSPACE)); + CustomData_get_layer_for_write(&mesh->fdata, CD_ORIGSPACE, mesh->totface)); const MFace *mfaces = static_cast( CustomData_get_layer(&mesh->fdata, CD_MFACE)); const MFace *mface = &mfaces[i]; @@ -2187,10 +2189,12 @@ void psys_particle_on_dm(Mesh *mesh_final, MFace *mface; MTFace *mtface; - MFace *mfaces = static_cast(CustomData_get_layer(&mesh_final->fdata, CD_MFACE)); + MFace *mfaces = static_cast( + CustomData_get_layer_for_write(&mesh_final->fdata, CD_MFACE, mesh_final->totface)); mface = &mfaces[mapindex]; const float(*vert_positions)[3] = BKE_mesh_vert_positions(mesh_final); - mtface = static_cast(CustomData_get_layer(&mesh_final->fdata, CD_MTFACE)); + mtface = static_cast( + CustomData_get_layer_for_write(&mesh_final->fdata, CD_MTFACE, mesh_final->totface)); if (mtface) { mtface += mapindex; @@ -3893,10 +3897,11 @@ static void psys_face_mat(Object *ob, Mesh *mesh, ParticleData *pa, float mat[4] return; } - MFace *mfaces = static_cast(CustomData_get_layer(&mesh->fdata, CD_MFACE)); + MFace *mfaces = static_cast( + CustomData_get_layer_for_write(&mesh->fdata, CD_MFACE, mesh->totface)); mface = &mfaces[i]; const OrigSpaceFace *osface = static_cast( - CustomData_get(&mesh->fdata, i, CD_ORIGSPACE)); + CustomData_get_for_write(&mesh->fdata, i, CD_ORIGSPACE, mesh->totface)); if (orco && (orcodata = static_cast(CustomData_get_layer(&mesh->vdata, CD_ORCO)))) { @@ -4204,7 +4209,7 @@ static int get_particle_uv(Mesh *mesh, float *texco, bool from_vert) { - MFace *mfaces = (MFace *)CustomData_get_layer(&mesh->fdata, CD_MFACE); + MFace *mfaces = (MFace *)CustomData_get_layer_for_write(&mesh->fdata, CD_MFACE, mesh->totface); MFace *mf; const MTFace *tf; int i; @@ -5108,8 +5113,8 @@ void psys_get_dupli_texture(ParticleSystem *psys, const MTFace *mtface = static_cast( CustomData_get_layer_n(mtf_data, CD_MTFACE, uv_idx)); if (mtface != nullptr) { - const MFace *mface = static_cast( - CustomData_get(&psmd->mesh_final->fdata, cpa->num, CD_MFACE)); + const MFace *mface = static_cast(CustomData_get_for_write( + &psmd->mesh_final->fdata, cpa->num, CD_MFACE, psmd->mesh_final->totface)); mtface += cpa->num; psys_interpolate_uvs(mtface, mface->v4, cpa->fuv, uv); } @@ -5153,8 +5158,8 @@ void psys_get_dupli_texture(ParticleSystem *psys, if (uv_idx >= 0) { const MTFace *mtface = static_cast( CustomData_get_layer_n(mtf_data, CD_MTFACE, uv_idx)); - const MFace *mface = static_cast( - CustomData_get(&psmd->mesh_final->fdata, num, CD_MFACE)); + const MFace *mface = static_cast(CustomData_get_for_write( + &psmd->mesh_final->fdata, num, CD_MFACE, psmd->mesh_final->totface)); mtface += num; psys_interpolate_uvs(mtface, mface->v4, pa->fuv, uv); } diff --git a/source/blender/blenkernel/intern/particle_distribute.c b/source/blender/blenkernel/intern/particle_distribute.c index b7e53fac15f..7cec485e6dd 100644 --- a/source/blender/blenkernel/intern/particle_distribute.c +++ b/source/blender/blenkernel/intern/particle_distribute.c @@ -179,7 +179,7 @@ static void distribute_grid(Mesh *mesh, ParticleSystem *psys) int amax = from == PART_FROM_FACE ? 3 : 1; totface = mesh->totface; - mface = mface_array = CustomData_get_layer(&mesh->fdata, CD_MFACE); + mface = mface_array = CustomData_get_layer_for_write(&mesh->fdata, CD_MFACE, mesh->totface); for (a = 0; a < amax; a++) { if (a == 0) { @@ -462,7 +462,7 @@ static void distribute_from_verts_exec(ParticleTask *thread, ParticleData *pa, i ParticleThreadContext *ctx = thread->ctx; MFace *mface; - mface = CustomData_get_layer(&ctx->mesh->fdata, CD_MFACE); + mface = CustomData_get_layer_for_write(&ctx->mesh->fdata, CD_MFACE, ctx->mesh->totface); int rng_skip_tot = PSYS_RND_DIST_SKIP; /* count how many rng_* calls won't need skipping */ @@ -523,7 +523,7 @@ static void distribute_from_faces_exec(ParticleTask *thread, ParticleData *pa, i int i; int rng_skip_tot = PSYS_RND_DIST_SKIP; /* count how many rng_* calls won't need skipping */ - MFace *mfaces = (MFace *)CustomData_get_layer(&mesh->fdata, CD_MFACE); + MFace *mfaces = (MFace *)CustomData_get_layer_for_write(&mesh->fdata, CD_MFACE, mesh->totface); MFace *mface; pa->num = i = ctx->index[p]; @@ -578,7 +578,7 @@ static void distribute_from_volume_exec(ParticleTask *thread, ParticleData *pa, const float(*positions)[3] = BKE_mesh_vert_positions(mesh); pa->num = i = ctx->index[p]; - MFace *mfaces = (MFace *)CustomData_get_layer(&mesh->fdata, CD_MFACE); + MFace *mfaces = (MFace *)CustomData_get_layer_for_write(&mesh->fdata, CD_MFACE, mesh->totface); mface = &mfaces[i]; switch (distr) { @@ -630,7 +630,7 @@ static void distribute_from_volume_exec(ParticleTask *thread, ParticleData *pa, min_d = FLT_MAX; intersect = 0; - mface = CustomData_get_layer(&mesh->fdata, CD_MFACE); + mface = CustomData_get_layer_for_write(&mesh->fdata, CD_MFACE, mesh->totface); for (i = 0; i < tot; i++, mface++) { if (i == pa->num) { continue; @@ -700,7 +700,7 @@ static void distribute_children_exec(ParticleTask *thread, ChildParticle *cpa, i return; } - MFace *mfaces = (MFace *)CustomData_get_layer(&mesh->fdata, CD_MFACE); + MFace *mfaces = (MFace *)CustomData_get_layer_for_write(&mesh->fdata, CD_MFACE, mesh->totface); mf = &mfaces[ctx->index[p]]; randu = BLI_rng_get_float(thread->rng); @@ -1053,7 +1053,7 @@ static int psys_thread_context_init_distribute(ParticleThreadContext *ctx, orcodata = CustomData_get_layer(&mesh->vdata, CD_ORCO); - MFace *mfaces = (MFace *)CustomData_get_layer(&mesh->fdata, CD_MFACE); + MFace *mfaces = (MFace *)CustomData_get_layer_for_write(&mesh->fdata, CD_MFACE, mesh->totface); for (i = 0; i < totelem; i++) { MFace *mf = &mfaces[i]; @@ -1114,7 +1114,8 @@ static int psys_thread_context_init_distribute(ParticleThreadContext *ctx, } } else { /* PART_FROM_FACE / PART_FROM_VOLUME */ - MFace *mfaces = (MFace *)CustomData_get_layer(&mesh->fdata, CD_MFACE); + MFace *mfaces = (MFace *)CustomData_get_layer_for_write( + &mesh->fdata, CD_MFACE, mesh->totface); for (i = 0; i < totelem; i++) { MFace *mf = &mfaces[i]; tweight = vweight[mf->v1] + vweight[mf->v2] + vweight[mf->v3]; diff --git a/source/blender/blenkernel/intern/pbvh.c b/source/blender/blenkernel/intern/pbvh.c index 2a8683ecaaa..d18ef62c20c 100644 --- a/source/blender/blenkernel/intern/pbvh.c +++ b/source/blender/blenkernel/intern/pbvh.c @@ -825,7 +825,8 @@ void BKE_pbvh_build_mesh(PBVH *pbvh, pbvh->mesh = mesh; pbvh->header.type = PBVH_FACES; pbvh->mpoly = mpoly; - pbvh->hide_poly = (bool *)CustomData_get_layer_named(&mesh->pdata, CD_PROP_BOOL, ".hide_poly"); + pbvh->hide_poly = (bool *)CustomData_get_layer_named_for_write( + &mesh->pdata, CD_PROP_BOOL, ".hide_poly", mesh->totpoly); pbvh->material_indices = (const int *)CustomData_get_layer_named( &mesh->pdata, CD_PROP_INT32, "material_index"); pbvh->mloop = mloop; @@ -833,7 +834,8 @@ void BKE_pbvh_build_mesh(PBVH *pbvh, pbvh->vert_positions = vert_positions; BKE_mesh_vertex_normals_ensure(mesh); pbvh->vert_normals = BKE_mesh_vertex_normals_for_write(mesh); - pbvh->hide_vert = (bool *)CustomData_get_layer_named(&mesh->vdata, CD_PROP_BOOL, ".hide_vert"); + pbvh->hide_vert = (bool *)CustomData_get_layer_named_for_write( + &mesh->vdata, CD_PROP_BOOL, ".hide_vert", mesh->totvert); pbvh->vert_bitmap = MEM_calloc_arrayN(totvert, sizeof(bool), "bvh->vert_bitmap"); pbvh->totvert = totvert; @@ -3369,7 +3371,7 @@ void pbvh_vertex_iter_init(PBVH *pbvh, PBVHNode *node, PBVHVertexIter *vi, int m vi->vert_normals = pbvh->vert_normals; vi->hide_vert = pbvh->hide_vert; - vi->vmask = CustomData_get_layer(pbvh->vdata, CD_PAINT_MASK); + vi->vmask = CustomData_get_layer_for_write(pbvh->vdata, CD_PAINT_MASK, pbvh->mesh->totvert); } } @@ -3456,7 +3458,8 @@ bool *BKE_pbvh_get_vert_hide_for_write(PBVH *pbvh) if (pbvh->hide_vert) { return pbvh->hide_vert; } - pbvh->hide_vert = CustomData_get_layer_named(&pbvh->mesh->vdata, CD_PROP_BOOL, ".hide_vert"); + pbvh->hide_vert = CustomData_get_layer_named_for_write( + &pbvh->mesh->vdata, CD_PROP_BOOL, ".hide_vert", pbvh->mesh->totvert); if (pbvh->hide_vert) { return pbvh->hide_vert; } @@ -3478,8 +3481,10 @@ void BKE_pbvh_face_sets_set(PBVH *pbvh, int *face_sets) void BKE_pbvh_update_hide_attributes_from_mesh(PBVH *pbvh) { if (pbvh->header.type == PBVH_FACES) { - pbvh->hide_vert = CustomData_get_layer_named(&pbvh->mesh->vdata, CD_PROP_BOOL, ".hide_vert"); - pbvh->hide_poly = CustomData_get_layer_named(&pbvh->mesh->pdata, CD_PROP_BOOL, ".hide_poly"); + pbvh->hide_vert = CustomData_get_layer_named_for_write( + &pbvh->mesh->vdata, CD_PROP_BOOL, ".hide_vert", pbvh->mesh->totvert); + pbvh->hide_poly = CustomData_get_layer_named_for_write( + &pbvh->mesh->pdata, CD_PROP_BOOL, ".hide_poly", pbvh->mesh->totpoly); } } @@ -3799,8 +3804,8 @@ void BKE_pbvh_sync_visibility_from_verts(PBVH *pbvh, Mesh *mesh) const MPoly *mp = BKE_mesh_polys(mesh); CCGKey key = pbvh->gridkey; - bool *hide_poly = (bool *)CustomData_get_layer_named( - &mesh->pdata, CD_PROP_BOOL, ".hide_poly"); + bool *hide_poly = (bool *)CustomData_get_layer_named_for_write( + &mesh->pdata, CD_PROP_BOOL, ".hide_poly", mesh->totpoly); bool delete_hide_poly = true; for (int face_index = 0; face_index < mesh->totpoly; face_index++, mp++) { @@ -3818,14 +3823,15 @@ void BKE_pbvh_sync_visibility_from_verts(PBVH *pbvh, Mesh *mesh) } if (hidden && !hide_poly) { - hide_poly = (bool *)CustomData_get_layer_named(&mesh->pdata, CD_PROP_BOOL, ".hide_poly"); + hide_poly = (bool *)CustomData_get_layer_named_for_write( + &mesh->pdata, CD_PROP_BOOL, ".hide_poly", mesh->totpoly); if (!hide_poly) { CustomData_add_layer_named( &mesh->pdata, CD_PROP_BOOL, CD_CONSTRUCT, NULL, mesh->totpoly, ".hide_poly"); - hide_poly = (bool *)CustomData_get_layer_named( - &mesh->pdata, CD_PROP_BOOL, ".hide_poly"); + hide_poly = (bool *)CustomData_get_layer_named_for_write( + &mesh->pdata, CD_PROP_BOOL, ".hide_poly", mesh->totpoly); } } diff --git a/source/blender/blenkernel/intern/subdiv_mesh.cc b/source/blender/blenkernel/intern/subdiv_mesh.cc index 2d1b6dd3f0e..dbb58fd73de 100644 --- a/source/blender/blenkernel/intern/subdiv_mesh.cc +++ b/source/blender/blenkernel/intern/subdiv_mesh.cc @@ -79,8 +79,8 @@ static void subdiv_mesh_ctx_cache_uv_layers(SubdivMeshContext *ctx) Mesh *subdiv_mesh = ctx->subdiv_mesh; ctx->num_uv_layers = CustomData_number_of_layers(&subdiv_mesh->ldata, CD_PROP_FLOAT2); for (int layer_index = 0; layer_index < ctx->num_uv_layers; layer_index++) { - ctx->uv_layers[layer_index] = static_cast( - CustomData_get_layer_n(&subdiv_mesh->ldata, CD_PROP_FLOAT2, layer_index)); + ctx->uv_layers[layer_index] = static_cast(CustomData_get_layer_n_for_write( + &subdiv_mesh->ldata, CD_PROP_FLOAT2, layer_index, subdiv_mesh->totloop)); } } @@ -93,19 +93,20 @@ static void subdiv_mesh_ctx_cache_custom_data_layers(SubdivMeshContext *ctx) ctx->subdiv_loops = BKE_mesh_loops_for_write(subdiv_mesh); /* Pointers to original indices layers. */ ctx->vert_origindex = static_cast( - CustomData_get_layer(&subdiv_mesh->vdata, CD_ORIGINDEX)); + CustomData_get_layer_for_write(&subdiv_mesh->vdata, CD_ORIGINDEX, subdiv_mesh->totvert)); ctx->edge_origindex = static_cast( - CustomData_get_layer(&subdiv_mesh->edata, CD_ORIGINDEX)); + CustomData_get_layer_for_write(&subdiv_mesh->edata, CD_ORIGINDEX, subdiv_mesh->totedge)); ctx->loop_origindex = static_cast( - CustomData_get_layer(&subdiv_mesh->ldata, CD_ORIGINDEX)); + CustomData_get_layer_for_write(&subdiv_mesh->ldata, CD_ORIGINDEX, subdiv_mesh->totloop)); ctx->poly_origindex = static_cast( - CustomData_get_layer(&subdiv_mesh->pdata, CD_ORIGINDEX)); + CustomData_get_layer_for_write(&subdiv_mesh->pdata, CD_ORIGINDEX, subdiv_mesh->totpoly)); /* UV layers interpolation. */ subdiv_mesh_ctx_cache_uv_layers(ctx); /* Orco interpolation. */ - ctx->orco = static_cast(CustomData_get_layer(&subdiv_mesh->vdata, CD_ORCO)); + ctx->orco = static_cast( + CustomData_get_layer_for_write(&subdiv_mesh->vdata, CD_ORCO, subdiv_mesh->totvert)); ctx->cloth_orco = static_cast( - CustomData_get_layer(&subdiv_mesh->vdata, CD_CLOTH_ORCO)); + CustomData_get_layer_for_write(&subdiv_mesh->vdata, CD_CLOTH_ORCO, subdiv_mesh->totvert)); } static void subdiv_mesh_prepare_accumulator(SubdivMeshContext *ctx, int num_vertices) diff --git a/source/blender/blenkernel/intern/subsurf_ccg.c b/source/blender/blenkernel/intern/subsurf_ccg.c index 910076d8b43..f5835b7fbb8 100644 --- a/source/blender/blenkernel/intern/subsurf_ccg.c +++ b/source/blender/blenkernel/intern/subsurf_ccg.c @@ -390,8 +390,10 @@ static void set_subsurf_legacy_uv(CCGSubSurf *ss, DerivedMesh *dm, DerivedMesh * const float(*dmloopuv)[2] = CustomData_get_layer_n(&dm->loopData, CD_PROP_FLOAT2, n); /* need to update both CD_MTFACE & CD_PROP_FLOAT2, hrmf, we could get away with * just tface except applying the modifier then looses subsurf UV */ - MTFace *tface = CustomData_get_layer_n(&result->faceData, CD_MTFACE, n); - float(*mloopuv)[2] = CustomData_get_layer_n(&result->loopData, CD_PROP_FLOAT2, n); + MTFace *tface = CustomData_get_layer_n_for_write( + &result->faceData, CD_MTFACE, n, result->numTessFaceData); + float(*mloopuv)[2] = CustomData_get_layer_n_for_write( + &result->loopData, CD_PROP_FLOAT2, n, result->getNumLoops(dm)); if (!dmloopuv || (!tface && !mloopuv)) { return; diff --git a/source/blender/blenloader/intern/versioning_250.c b/source/blender/blenloader/intern/versioning_250.c index 217a971d844..da4783e19ea 100644 --- a/source/blender/blenloader/intern/versioning_250.c +++ b/source/blender/blenloader/intern/versioning_250.c @@ -996,7 +996,7 @@ void blo_do_versions_250(FileData *fd, Library *lib, Main *bmain) if ((key = blo_do_versions_newlibadr(fd, lib, me->key)) && key->refkey) { data = key->refkey->data; tot = MIN2(me->totvert, key->refkey->totelem); - MVert *verts = (MVert *)CustomData_get_layer(&me->vdata, CD_MVERT); + MVert *verts = (MVert *)CustomData_get_layer_for_write(&me->vdata, CD_MVERT, me->totvert); for (a = 0; a < tot; a++, data += 3) { copy_v3_v3(verts[a].co_legacy, data); } diff --git a/source/blender/blenloader/intern/versioning_290.c b/source/blender/blenloader/intern/versioning_290.c index eca1abc2bc9..202b9ea692d 100644 --- a/source/blender/blenloader/intern/versioning_290.c +++ b/source/blender/blenloader/intern/versioning_290.c @@ -821,21 +821,22 @@ void blo_do_versions_290(FileData *fd, Library *UNUSED(lib), Main *bmain) for (const MPoly *mp = BKE_mesh_polys(me), *mp_end = mp + me->totpoly; mp < mp_end; mp++) { if (mp->totloop == 2) { bool changed; - BKE_mesh_validate_arrays(me, - BKE_mesh_vert_positions_for_write(me), - me->totvert, - BKE_mesh_edges_for_write(me), - me->totedge, - (MFace *)CustomData_get_layer(&me->fdata, CD_MFACE), - me->totface, - BKE_mesh_loops_for_write(me), - me->totloop, - BKE_mesh_polys_for_write(me), - me->totpoly, - BKE_mesh_deform_verts_for_write(me), - false, - true, - &changed); + BKE_mesh_validate_arrays( + me, + BKE_mesh_vert_positions_for_write(me), + me->totvert, + BKE_mesh_edges_for_write(me), + me->totedge, + (MFace *)CustomData_get_layer_for_write(&me->fdata, CD_MFACE, me->totface), + me->totface, + BKE_mesh_loops_for_write(me), + me->totloop, + BKE_mesh_polys_for_write(me), + me->totpoly, + BKE_mesh_deform_verts_for_write(me), + false, + true, + &changed); break; } } diff --git a/source/blender/blenloader/intern/versioning_defaults.cc b/source/blender/blenloader/intern/versioning_defaults.cc index d833e964232..87574e90dab 100644 --- a/source/blender/blenloader/intern/versioning_defaults.cc +++ b/source/blender/blenloader/intern/versioning_defaults.cc @@ -359,7 +359,7 @@ static void blo_update_defaults_scene(Main *bmain, Scene *scene) {0.625, 0.75}, {0.375, 0.75}, {0.375, 0.25}, {0.625, 0.25}, {0.625, 0.50}, {0.375, 0.50}, }; float(*mloopuv)[2] = static_cast( - CustomData_get_layer(&me->ldata, CD_PROP_FLOAT2)); + CustomData_get_layer_for_write(&me->ldata, CD_PROP_FLOAT2, me->totloop)); memcpy(mloopuv, uv_values, sizeof(float[2]) * me->totloop); } diff --git a/source/blender/draw/intern/draw_cache_extract_mesh_render_data.cc b/source/blender/draw/intern/draw_cache_extract_mesh_render_data.cc index add0a395d70..0937e9feb54 100644 --- a/source/blender/draw/intern/draw_cache_extract_mesh_render_data.cc +++ b/source/blender/draw/intern/draw_cache_extract_mesh_render_data.cc @@ -367,7 +367,7 @@ void mesh_render_data_update_normals(MeshRenderData *mr, const eMRDataType data_ mr->loop_normals = static_cast( MEM_mallocN(sizeof(*mr->loop_normals) * mr->loop_len, __func__)); short(*clnors)[2] = static_cast( - CustomData_get_layer(&mr->me->ldata, CD_CUSTOMLOOPNORMAL)); + CustomData_get_layer_for_write(&mr->me->ldata, CD_CUSTOMLOOPNORMAL, mr->me->totloop)); const bool *sharp_edges = static_cast( CustomData_get_layer_named(&mr->me->edata, CD_PROP_BOOL, "sharp_edge")); BKE_mesh_normals_loop_split(reinterpret_cast(mr->vert_positions), diff --git a/source/blender/editors/mesh/editmesh_mask_extract.c b/source/blender/editors/mesh/editmesh_mask_extract.c index 9988bdcb367..969a5cf52ed 100644 --- a/source/blender/editors/mesh/editmesh_mask_extract.c +++ b/source/blender/editors/mesh/editmesh_mask_extract.c @@ -573,8 +573,10 @@ static int paint_mask_slice_exec(bContext *C, wmOperator *op) if (ob->mode == OB_MODE_SCULPT) { SculptSession *ss = ob->sculpt; - ss->face_sets = CustomData_get_layer_named( - &((Mesh *)ob->data)->pdata, CD_PROP_INT32, ".sculpt_face_set"); + ss->face_sets = CustomData_get_layer_named_for_write(&((Mesh *)ob->data)->pdata, + CD_PROP_INT32, + ".sculpt_face_set", + ((Mesh *)ob->data)->totpoly); if (ss->face_sets) { /* Assign a new Face Set ID to the new faces created by the slice operation. */ const int next_face_set_id = ED_sculpt_face_sets_find_next_available_id(ob->data); diff --git a/source/blender/editors/mesh/mesh_data.cc b/source/blender/editors/mesh/mesh_data.cc index 8b3ecf6f59f..db60f253cb2 100644 --- a/source/blender/editors/mesh/mesh_data.cc +++ b/source/blender/editors/mesh/mesh_data.cc @@ -232,7 +232,7 @@ void ED_mesh_uv_loop_reset_ex(Mesh *me, const int layernum) /* Collect Mesh UVs */ BLI_assert(CustomData_has_layer(&me->ldata, CD_PROP_FLOAT2)); float2 *mloopuv = static_cast( - CustomData_get_layer_n(&me->ldata, CD_PROP_FLOAT2, layernum)); + CustomData_get_layer_n_for_write(&me->ldata, CD_PROP_FLOAT2, layernum, me->totloop)); const MPoly *polys = BKE_mesh_polys(me); for (int i = 0; i < me->totpoly; i++) { @@ -302,7 +302,8 @@ int ED_mesh_uv_add( CustomData_add_layer_named(&me->ldata, CD_PROP_FLOAT2, CD_DUPLICATE, - CustomData_get_layer(&me->ldata, CD_PROP_FLOAT2), + const_cast(static_cast( + CustomData_get_layer(&me->ldata, CD_PROP_FLOAT2))), me->totloop, unique_name); @@ -364,8 +365,8 @@ const bool *ED_mesh_uv_map_pin_layer_get(const Mesh *mesh, const int uv_index) static bool *ensure_corner_boolean_attribute(Mesh &mesh, const blender::StringRefNull name) { - bool *data = static_cast(CustomData_duplicate_referenced_layer_named( - &mesh.ldata, CD_PROP_BOOL, name.c_str(), mesh.totloop)); + bool *data = static_cast( + CustomData_get_layer_named_for_write(&mesh.ldata, CD_PROP_BOOL, name.c_str(), mesh.totloop)); if (!data) { data = static_cast(CustomData_add_layer_named( &mesh.ldata, CD_PROP_BOOL, CD_SET_DEFAULT, nullptr, mesh.totpoly, name.c_str())); diff --git a/source/blender/editors/mesh/meshtools.cc b/source/blender/editors/mesh/meshtools.cc index a5e61cb8b33..b2cbb8c47ea 100644 --- a/source/blender/editors/mesh/meshtools.cc +++ b/source/blender/editors/mesh/meshtools.cc @@ -108,9 +108,10 @@ static void join_mesh_single(Depsgraph *depsgraph, CustomData_copy_data_named(&me->vdata, vdata, 0, *vertofs, me->totvert); /* vertex groups */ - MDeformVert *dvert = (MDeformVert *)CustomData_get(vdata, *vertofs, CD_MDEFORMVERT); - const MDeformVert *dvert_src = (const MDeformVert *)CustomData_get( - &me->vdata, 0, CD_MDEFORMVERT); + MDeformVert *dvert = (MDeformVert *)CustomData_get_for_write( + vdata, *vertofs, CD_MDEFORMVERT, totvert); + const MDeformVert *dvert_src = (const MDeformVert *)CustomData_get_layer(&me->vdata, + CD_MDEFORMVERT); /* Remap to correct new vgroup indices, if needed. */ if (dvert_src) { @@ -254,7 +255,7 @@ static void join_mesh_single(Depsgraph *depsgraph, /* Apply matmap. In case we don't have material indices yet, create them if more than one * material is the result of joining. */ int *material_indices = static_cast( - CustomData_get_layer_named(pdata, CD_PROP_INT32, "material_index")); + CustomData_get_layer_named_for_write(pdata, CD_PROP_INT32, "material_index", totpoly)); if (!material_indices && totcol > 1) { material_indices = (int *)CustomData_add_layer_named( pdata, CD_PROP_INT32, CD_SET_DEFAULT, nullptr, totpoly, "material_index"); @@ -270,8 +271,9 @@ static void join_mesh_single(Depsgraph *depsgraph, } /* Face maps. */ - int *fmap = (int *)CustomData_get(pdata, *polyofs, CD_FACEMAP); - const int *fmap_src = (const int *)CustomData_get(&me->pdata, 0, CD_FACEMAP); + int *fmap = (int *)CustomData_get_for_write(pdata, *polyofs, CD_FACEMAP, totpoly); + const int *fmap_src = (const int *)CustomData_get_for_write( + &me->pdata, 0, CD_FACEMAP, me->totpoly); /* Remap to correct new face-map indices, if needed. */ if (fmap_src) { @@ -300,14 +302,14 @@ static void join_mesh_single(Depsgraph *depsgraph, /* Face Sets IDs are a sparse sequence, so this function offsets all the IDs by face_set_offset and * updates face_set_offset with the maximum ID value. This way, when used in multiple meshes, all * of them will have different IDs for their Face Sets. */ -static void mesh_join_offset_face_sets_ID(const Mesh *mesh, int *face_set_offset) +static void mesh_join_offset_face_sets_ID(Mesh *mesh, int *face_set_offset) { if (!mesh->totpoly) { return; } - int *face_sets = (int *)CustomData_get_layer_named( - &mesh->pdata, CD_PROP_INT32, ".sculpt_face_set"); + int *face_sets = (int *)CustomData_get_layer_named_for_write( + &mesh->pdata, CD_PROP_INT32, ".sculpt_face_set", mesh->totpoly); if (!face_sets) { return; } diff --git a/source/blender/editors/object/object_facemap_ops.c b/source/blender/editors/object/object_facemap_ops.c index 4364375a4e3..23344c38a12 100644 --- a/source/blender/editors/object/object_facemap_ops.c +++ b/source/blender/editors/object/object_facemap_ops.c @@ -52,7 +52,7 @@ void ED_object_facemap_face_add(Object *ob, bFaceMap *fmap, int facenum) Mesh *me = ob->data; /* if there's is no facemap layer then create one */ - if ((facemap = CustomData_get_layer(&me->pdata, CD_FACEMAP)) == NULL) { + if ((facemap = CustomData_get_layer_for_write(&me->pdata, CD_FACEMAP, me->totpoly)) == NULL) { facemap = CustomData_add_layer(&me->pdata, CD_FACEMAP, CD_SET_DEFAULT, NULL, me->totpoly); } @@ -74,7 +74,7 @@ void ED_object_facemap_face_remove(Object *ob, bFaceMap *fmap, int facenum) int *facemap; Mesh *me = ob->data; - if ((facemap = CustomData_get_layer(&me->pdata, CD_FACEMAP)) == NULL) { + if ((facemap = CustomData_get_layer_for_write(&me->pdata, CD_FACEMAP, me->totpoly)) == NULL) { return; } @@ -117,7 +117,7 @@ static void object_fmap_remap_object_mode(Object *ob, const int *remap) Mesh *me = ob->data; if (CustomData_has_layer(&me->pdata, CD_FACEMAP)) { - int *map = CustomData_get_layer(&me->pdata, CD_FACEMAP); + int *map = CustomData_get_layer_for_write(&me->pdata, CD_FACEMAP, me->totpoly); if (map) { for (int i = 0; i < me->totpoly; i++) { if (map[i] != -1) { diff --git a/source/blender/editors/object/object_modifier.cc b/source/blender/editors/object/object_modifier.cc index 57d62b3a95b..1bfd156b664 100644 --- a/source/blender/editors/object/object_modifier.cc +++ b/source/blender/editors/object/object_modifier.cc @@ -2900,7 +2900,7 @@ static Object *modifier_skin_armature_create(Depsgraph *depsgraph, Main *bmain, arm->edbo = MEM_cnew("edbo armature"); MVertSkin *mvert_skin = static_cast( - CustomData_get_layer(&me->vdata, CD_MVERT_SKIN)); + CustomData_get_layer_for_write(&me->vdata, CD_MVERT_SKIN, me->totvert)); int *emap_mem; MeshElemMap *emap; BKE_mesh_vert_edge_map_create(&emap, &emap_mem, me_edges.data(), me->totvert, me->totedge); diff --git a/source/blender/editors/sculpt_paint/paint_hide.c b/source/blender/editors/sculpt_paint/paint_hide.c index 09fcbef2180..1ec93d3a713 100644 --- a/source/blender/editors/sculpt_paint/paint_hide.c +++ b/source/blender/editors/sculpt_paint/paint_hide.c @@ -77,7 +77,8 @@ static void partialvis_update_mesh(Object *ob, const int *vert_indices = BKE_pbvh_node_get_vert_indices(node); paint_mask = CustomData_get_layer(&me->vdata, CD_PAINT_MASK); - bool *hide_vert = CustomData_get_layer_named(&me->vdata, CD_PROP_BOOL, ".hide_vert"); + bool *hide_vert = CustomData_get_layer_named_for_write( + &me->vdata, CD_PROP_BOOL, ".hide_vert", me->totvert); if (hide_vert == NULL) { hide_vert = CustomData_add_layer_named( &me->vdata, CD_PROP_BOOL, CD_SET_DEFAULT, NULL, me->totvert, ".hide_vert"); diff --git a/source/blender/editors/sculpt_paint/paint_mask.c b/source/blender/editors/sculpt_paint/paint_mask.c index 3ebd5fe799d..c6ffee8ba46 100644 --- a/source/blender/editors/sculpt_paint/paint_mask.c +++ b/source/blender/editors/sculpt_paint/paint_mask.c @@ -1408,7 +1408,8 @@ static void sculpt_gesture_trim_end(bContext *UNUSED(C), SculptGestureContext *s SculptSession *ss = object->sculpt; Mesh *mesh = (Mesh *)object->data; - ss->face_sets = CustomData_get_layer_named(&mesh->pdata, CD_PROP_INT32, ".sculpt_face_set"); + ss->face_sets = CustomData_get_layer_named_for_write( + &mesh->pdata, CD_PROP_INT32, ".sculpt_face_set", mesh->totpoly); if (ss->face_sets) { /* Assign a new Face Set ID to the new faces created by the trim operation. */ const int next_face_set_id = ED_sculpt_face_sets_find_next_available_id(object->data); diff --git a/source/blender/editors/sculpt_paint/sculpt_dyntopo.c b/source/blender/editors/sculpt_paint/sculpt_dyntopo.c index 192a4545e94..56ae10ef791 100644 --- a/source/blender/editors/sculpt_paint/sculpt_dyntopo.c +++ b/source/blender/editors/sculpt_paint/sculpt_dyntopo.c @@ -173,7 +173,8 @@ static void SCULPT_dynamic_topology_disable_ex( me->face_sets_color_default = 1; /* Sync the visibility to vertices manually as the pmap is still not initialized. */ - bool *hide_vert = (bool *)CustomData_get_layer_named(&me->vdata, CD_PROP_BOOL, ".hide_vert"); + bool *hide_vert = (bool *)CustomData_get_layer_named_for_write( + &me->vdata, CD_PROP_BOOL, ".hide_vert", me->totvert); if (hide_vert != NULL) { memset(hide_vert, 0, sizeof(bool) * me->totvert); } diff --git a/source/blender/editors/sculpt_paint/sculpt_face_set.cc b/source/blender/editors/sculpt_paint/sculpt_face_set.cc index d2e20b33f6c..9c2d6a78fab 100644 --- a/source/blender/editors/sculpt_paint/sculpt_face_set.cc +++ b/source/blender/editors/sculpt_paint/sculpt_face_set.cc @@ -74,8 +74,8 @@ int ED_sculpt_face_sets_find_next_available_id(struct Mesh *mesh) void ED_sculpt_face_sets_initialize_none_to_id(struct Mesh *mesh, const int new_id) { - int *face_sets = static_cast( - CustomData_get_layer_named(&mesh->pdata, CD_PROP_INT32, ".sculpt_face_set")); + int *face_sets = static_cast(CustomData_get_layer_named_for_write( + &mesh->pdata, CD_PROP_INT32, ".sculpt_face_set", mesh->totpoly)); if (!face_sets) { return; } diff --git a/source/blender/freestyle/intern/blender_interface/BlenderFileLoader.cpp b/source/blender/freestyle/intern/blender_interface/BlenderFileLoader.cpp index 0752b1a7b91..1529c943963 100644 --- a/source/blender/freestyle/intern/blender_interface/BlenderFileLoader.cpp +++ b/source/blender/freestyle/intern/blender_interface/BlenderFileLoader.cpp @@ -421,12 +421,14 @@ void BlenderFileLoader::insertShapeNode(Object *ob, Mesh *me, int id) const float(*lnors)[3] = nullptr; if (CustomData_has_layer(&me->ldata, CD_NORMAL)) { - lnors = (float(*)[3])CustomData_get_layer(&me->ldata, CD_NORMAL); + lnors = (const float(*)[3])CustomData_get_layer(&me->ldata, CD_NORMAL); } // Get other mesh data - const FreestyleEdge *fed = (FreestyleEdge *)CustomData_get_layer(&me->edata, CD_FREESTYLE_EDGE); - const FreestyleFace *ffa = (FreestyleFace *)CustomData_get_layer(&me->pdata, CD_FREESTYLE_FACE); + const FreestyleEdge *fed = (const FreestyleEdge *)CustomData_get_layer(&me->edata, + CD_FREESTYLE_EDGE); + const FreestyleFace *ffa = (const FreestyleFace *)CustomData_get_layer(&me->pdata, + CD_FREESTYLE_FACE); // Compute view matrix Object *ob_camera_eval = DEG_get_evaluated_object(_depsgraph, RE_GetCamera(_re)); diff --git a/source/blender/geometry/intern/mesh_split_edges.cc b/source/blender/geometry/intern/mesh_split_edges.cc index f5470ec225c..91b28652bbe 100644 --- a/source/blender/geometry/intern/mesh_split_edges.cc +++ b/source/blender/geometry/intern/mesh_split_edges.cc @@ -49,10 +49,12 @@ static void add_new_vertices(Mesh &mesh, const Span new_to_old_verts_map) attribute.finish(); } - if (float3 *orco = static_cast(CustomData_get_layer(&mesh.vdata, CD_ORCO))) { + if (float3 *orco = static_cast( + CustomData_get_layer_for_write(&mesh.vdata, CD_ORCO, mesh.totvert))) { copy_to_new_verts({orco, mesh.totvert}, new_to_old_verts_map); } - if (int *orig_indices = static_cast(CustomData_get_layer(&mesh.vdata, CD_ORIGINDEX))) { + if (int *orig_indices = static_cast( + CustomData_get_layer_for_write(&mesh.vdata, CD_ORIGINDEX, mesh.totvert))) { copy_to_new_verts({orig_indices, mesh.totvert}, new_to_old_verts_map); } } diff --git a/source/blender/gpu/intern/gpu_shader_builder_stubs.cc b/source/blender/gpu/intern/gpu_shader_builder_stubs.cc index 49042db50ba..dea62de94f6 100644 --- a/source/blender/gpu/intern/gpu_shader_builder_stubs.cc +++ b/source/blender/gpu/intern/gpu_shader_builder_stubs.cc @@ -208,18 +208,6 @@ bool CustomData_has_layer(const struct CustomData * /*data*/, int /*type*/) return false; } -void *CustomData_get_layer_named(const struct CustomData * /*data*/, - int /*type*/, - const char * /*name*/) -{ - return nullptr; -} - -void *CustomData_get_layer(const struct CustomData * /*data*/, int /*type*/) -{ - return nullptr; -} - /** \} */ /* -------------------------------------------------------------------- */ diff --git a/source/blender/io/alembic/exporter/abc_writer_hair.cc b/source/blender/io/alembic/exporter/abc_writer_hair.cc index e4c61a7cb68..1675750cda1 100644 --- a/source/blender/io/alembic/exporter/abc_writer_hair.cc +++ b/source/blender/io/alembic/exporter/abc_writer_hair.cc @@ -120,8 +120,9 @@ void ABCHairWriter::write_hair_sample(const HierarchyContext &context, float inv_mat[4][4]; invert_m4_m4_safe(inv_mat, context.object->object_to_world); - MTFace *mtface = (MTFace *)CustomData_get_layer(&mesh->fdata, CD_MTFACE); - MFace *mface = (MFace *)CustomData_get_layer(&mesh->fdata, CD_MFACE); + MTFace *mtface = (MTFace *)CustomData_get_layer_for_write( + &mesh->fdata, CD_MTFACE, mesh->totface); + const MFace *mface = (const MFace *)CustomData_get_layer(&mesh->fdata, CD_MFACE); const float(*positions)[3] = BKE_mesh_vert_positions(mesh); const float(*vert_normals)[3] = BKE_mesh_vertex_normals_ensure(mesh); @@ -153,7 +154,7 @@ void ABCHairWriter::write_hair_sample(const HierarchyContext &context, if (num < mesh->totface) { /* TODO(Sybren): check whether the NULL check here and if(mface) are actually required */ - MFace *face = mface == nullptr ? nullptr : &mface[num]; + const MFace *face = mface == nullptr ? nullptr : &mface[num]; MTFace *tface = mtface + num; if (mface) { @@ -189,8 +190,8 @@ void ABCHairWriter::write_hair_sample(const HierarchyContext &context, /* iterate over all faces to find a corresponding underlying UV */ for (int n = 0; n < mesh->totface; n++) { - MFace *face = &mface[n]; - MTFace *tface = mtface + n; + const MFace *face = &mface[n]; + const MTFace *tface = mtface + n; uint vtx[4]; vtx[0] = face->v1; vtx[1] = face->v2; @@ -244,8 +245,9 @@ void ABCHairWriter::write_hair_child_sample(const HierarchyContext &context, float inv_mat[4][4]; invert_m4_m4_safe(inv_mat, context.object->object_to_world); - MFace *mface = (MFace *)CustomData_get_layer(&mesh->fdata, CD_MFACE); - MTFace *mtface = (MTFace *)CustomData_get_layer(&mesh->fdata, CD_MTFACE); + const MFace *mface = (const MFace *)CustomData_get_layer(&mesh->fdata, CD_MFACE); + MTFace *mtface = (MTFace *)CustomData_get_layer_for_write( + &mesh->fdata, CD_MTFACE, mesh->totface); const float(*positions)[3] = BKE_mesh_vert_positions(mesh); const float(*vert_normals)[3] = BKE_mesh_vertex_normals_ensure(mesh); @@ -271,7 +273,7 @@ void ABCHairWriter::write_hair_child_sample(const HierarchyContext &context, continue; } - MFace *face = &mface[num]; + const MFace *face = &mface[num]; MTFace *tface = mtface + num; float r_uv[2], tmpnor[3], mapfw[4], vec[3]; diff --git a/source/blender/io/alembic/intern/abc_customdata.cc b/source/blender/io/alembic/intern/abc_customdata.cc index 035f49325c4..9e5182c53d4 100644 --- a/source/blender/io/alembic/intern/abc_customdata.cc +++ b/source/blender/io/alembic/intern/abc_customdata.cc @@ -535,7 +535,7 @@ void read_generated_coordinates(const ICompoundProperty &prop, void *cd_data; if (CustomData_has_layer(&mesh->vdata, CD_ORCO)) { - cd_data = CustomData_get_layer(&mesh->vdata, CD_ORCO); + cd_data = CustomData_get_layer_for_write(&mesh->vdata, CD_ORCO, mesh->totvert); } else { cd_data = CustomData_add_layer(&mesh->vdata, CD_ORCO, CD_CONSTRUCT, nullptr, totvert); diff --git a/source/blender/io/alembic/intern/abc_reader_mesh.cc b/source/blender/io/alembic/intern/abc_reader_mesh.cc index 53204fc3e47..11fb191b31c 100644 --- a/source/blender/io/alembic/intern/abc_reader_mesh.cc +++ b/source/blender/io/alembic/intern/abc_reader_mesh.cc @@ -383,7 +383,8 @@ static void *add_customdata_cb(Mesh *mesh, const char *name, int data_type) return nullptr; } - void *cd_ptr = CustomData_get_layer_named(&mesh->ldata, cd_data_type, name); + void *cd_ptr = CustomData_get_layer_named_for_write( + &mesh->ldata, cd_data_type, name, mesh->totloop); if (cd_ptr != nullptr) { /* layer already exists, so just return it. */ return cd_ptr; diff --git a/source/blender/io/collada/MeshImporter.cpp b/source/blender/io/collada/MeshImporter.cpp index 0c8bd29a3c9..aa29bfc3343 100644 --- a/source/blender/io/collada/MeshImporter.cpp +++ b/source/blender/io/collada/MeshImporter.cpp @@ -719,8 +719,9 @@ void MeshImporter::read_polys(COLLADAFW::Mesh *collada_mesh, uvset_index++) { /* get mtface by face index and uv set index */ COLLADAFW::IndexList &index_list = *index_list_array_uvcoord[uvset_index]; - blender::float2 *mloopuv = static_cast(CustomData_get_layer_named( - &me->ldata, CD_PROP_FLOAT2, index_list.getName().c_str())); + blender::float2 *mloopuv = static_cast( + CustomData_get_layer_named_for_write( + &me->ldata, CD_PROP_FLOAT2, index_list.getName().c_str(), me->totloop)); if (mloopuv == nullptr) { fprintf(stderr, "Collada import: Mesh [%s] : Unknown reference to TEXCOORD [#%s].\n", @@ -762,8 +763,8 @@ void MeshImporter::read_polys(COLLADAFW::Mesh *collada_mesh, COLLADAFW::IndexList &color_index_list = *mp->getColorIndices(vcolor_index); COLLADAFW::String colname = extract_vcolname(color_index_list.getName()); - MLoopCol *mloopcol = (MLoopCol *)CustomData_get_layer_named( - &me->ldata, CD_PROP_BYTE_COLOR, colname.c_str()); + MLoopCol *mloopcol = (MLoopCol *)CustomData_get_layer_named_for_write( + &me->ldata, CD_PROP_BYTE_COLOR, colname.c_str(), me->totloop); if (mloopcol == nullptr) { fprintf(stderr, "Collada import: Mesh [%s] : Unknown reference to VCOLOR [#%s].\n", diff --git a/source/blender/io/usd/intern/usd_reader_mesh.cc b/source/blender/io/usd/intern/usd_reader_mesh.cc index b88874e57c3..f961fa64a05 100644 --- a/source/blender/io/usd/intern/usd_reader_mesh.cc +++ b/source/blender/io/usd/intern/usd_reader_mesh.cc @@ -204,7 +204,7 @@ static void *add_customdata_cb(Mesh *mesh, const char *name, const int data_type } loopdata = &mesh->ldata; - cd_ptr = CustomData_get_layer_named(loopdata, cd_data_type, name); + cd_ptr = CustomData_get_layer_named_for_write(loopdata, cd_data_type, name, mesh->totloop); if (cd_ptr != nullptr) { /* layer already exists, so just return it. */ return cd_ptr; diff --git a/source/blender/makesrna/intern/rna_curves.c b/source/blender/makesrna/intern/rna_curves.c index f19f632519f..11114f298a9 100644 --- a/source/blender/makesrna/intern/rna_curves.c +++ b/source/blender/makesrna/intern/rna_curves.c @@ -77,8 +77,8 @@ static void rna_Curves_curve_offset_data_begin(CollectionPropertyIterator *iter, static float (*get_curves_positions(Curves *curves))[3] { - return (float(*)[3])CustomData_get_layer_named( - &curves->geometry.point_data, CD_PROP_FLOAT3, "position"); + return (float(*)[3])CustomData_get_layer_named_for_write( + &curves->geometry.point_data, CD_PROP_FLOAT3, "position", curves->geometry.point_num); } static const float (*get_curves_positions_const(const Curves *curves))[3] @@ -152,9 +152,9 @@ static float rna_CurvePoint_radius_get(PointerRNA *ptr) static void rna_CurvePoint_radius_set(PointerRNA *ptr, float value) { - const Curves *curves = rna_curves(ptr); - float *radii = (float *)CustomData_get_layer_named( - &curves->geometry.point_data, CD_PROP_FLOAT, "radius"); + Curves *curves = rna_curves(ptr); + float *radii = (float *)CustomData_get_layer_named_for_write( + &curves->geometry.point_data, CD_PROP_FLOAT, "radius", curves->geometry.point_num); if (radii == NULL) { return; } @@ -212,8 +212,7 @@ static void rna_CurveSlice_points_begin(CollectionPropertyIterator *iter, Pointe Curves *curves = rna_curves(ptr); const int offset = rna_CurveSlice_first_point_index_get(ptr); const int size = rna_CurveSlice_points_length_get(ptr); - float(*positions)[3] = (float(*)[3])CustomData_get_layer_named( - &curves->geometry.point_data, CD_PROP_FLOAT3, "position"); + float(*positions)[3] = get_curves_positions(curves); float(*co)[3] = positions + offset; rna_iterator_array_begin(iter, co, sizeof(float[3]), size, 0, NULL); } diff --git a/source/blender/makesrna/intern/rna_mesh.c b/source/blender/makesrna/intern/rna_mesh.c index e1e75d25157..6791a171e8e 100644 --- a/source/blender/makesrna/intern/rna_mesh.c +++ b/source/blender/makesrna/intern/rna_mesh.c @@ -469,7 +469,7 @@ static bool rna_MeshVertex_hide_get(PointerRNA *ptr) static void rna_MeshVertex_hide_set(PointerRNA *ptr, bool value) { Mesh *mesh = rna_mesh(ptr); - bool *hide_vert = (bool *)CustomData_duplicate_referenced_layer_named( + bool *hide_vert = (bool *)CustomData_get_layer_named_for_write( &mesh->vdata, CD_PROP_BOOL, ".hide_vert", mesh->totvert); if (!hide_vert) { if (!value) { @@ -495,7 +495,7 @@ static bool rna_MeshVertex_select_get(PointerRNA *ptr) static void rna_MeshVertex_select_set(PointerRNA *ptr, bool value) { Mesh *mesh = rna_mesh(ptr); - bool *select_vert = (bool *)CustomData_duplicate_referenced_layer_named( + bool *select_vert = (bool *)CustomData_get_layer_named_for_write( &mesh->vdata, CD_PROP_BOOL, ".select_vert", mesh->totvert); if (!select_vert) { if (!value) { @@ -564,13 +564,13 @@ static void rna_MeshLoop_normal_get(PointerRNA *ptr, float *values) { Mesh *me = rna_mesh(ptr); const int index = rna_MeshLoop_index_get(ptr); - const float(*vec)[3] = CustomData_get(&me->ldata, index, CD_NORMAL); + const float(*layer)[3] = CustomData_get_layer(&me->ldata, CD_NORMAL); - if (!vec) { + if (!layer) { zero_v3(values); } else { - copy_v3_v3(values, (const float *)vec); + copy_v3_v3(values, layer[index]); } } @@ -578,10 +578,10 @@ static void rna_MeshLoop_normal_set(PointerRNA *ptr, const float *values) { Mesh *me = rna_mesh(ptr); const int index = rna_MeshLoop_index_get(ptr); - float(*vec)[3] = CustomData_get(&me->ldata, index, CD_NORMAL); + float(*layer)[3] = CustomData_get_layer_for_write(&me->ldata, CD_NORMAL, me->totloop); - if (vec) { - normalize_v3_v3(*vec, values); + if (layer) { + normalize_v3_v3(*layer, values + index); } } @@ -589,13 +589,13 @@ static void rna_MeshLoop_tangent_get(PointerRNA *ptr, float *values) { Mesh *me = rna_mesh(ptr); const int index = rna_MeshLoop_index_get(ptr); - const float(*vec)[4] = CustomData_get(&me->ldata, index, CD_MLOOPTANGENT); + const float(*layer)[4] = CustomData_get_layer(&me->ldata, CD_MLOOPTANGENT); - if (!vec) { + if (!layer) { zero_v3(values); } else { - copy_v3_v3(values, (const float *)vec); + copy_v3_v3(values, (const float *)(layer + index)); } } @@ -603,21 +603,21 @@ static float rna_MeshLoop_bitangent_sign_get(PointerRNA *ptr) { Mesh *me = rna_mesh(ptr); const int index = rna_MeshLoop_index_get(ptr); - const float(*vec)[4] = CustomData_get(&me->ldata, index, CD_MLOOPTANGENT); + const float(*vec)[4] = CustomData_get_layer(&me->ldata, CD_MLOOPTANGENT); - return (vec) ? (*vec)[3] : 0.0f; + return (vec) ? vec[index][3] : 0.0f; } static void rna_MeshLoop_bitangent_get(PointerRNA *ptr, float *values) { Mesh *me = rna_mesh(ptr); const int index = rna_MeshLoop_index_get(ptr); - const float(*nor)[3] = CustomData_get(&me->ldata, index, CD_NORMAL); - const float(*vec)[4] = CustomData_get(&me->ldata, index, CD_MLOOPTANGENT); + const float(*nor)[3] = CustomData_get_layer(&me->ldata, CD_NORMAL); + const float(*vec)[4] = CustomData_get_layer(&me->ldata, CD_MLOOPTANGENT); if (nor && vec) { - cross_v3_v3v3(values, (const float *)nor, (const float *)vec); - mul_v3_fl(values, (*vec)[3]); + cross_v3_v3v3(values, nor[index], vec[index]); + mul_v3_fl(values, vec[index][3]); } else { zero_v3(values); @@ -645,7 +645,7 @@ static bool rna_MeshPolygon_hide_get(PointerRNA *ptr) static void rna_MeshPolygon_hide_set(PointerRNA *ptr, bool value) { Mesh *mesh = rna_mesh(ptr); - bool *hide_poly = (bool *)CustomData_duplicate_referenced_layer_named( + bool *hide_poly = (bool *)CustomData_get_layer_named_for_write( &mesh->pdata, CD_PROP_BOOL, ".hide_poly", mesh->totpoly); if (!hide_poly) { if (!value) { @@ -671,7 +671,7 @@ static bool rna_MeshPolygon_select_get(PointerRNA *ptr) static void rna_MeshPolygon_select_set(PointerRNA *ptr, bool value) { Mesh *mesh = rna_mesh(ptr); - bool *select_poly = (bool *)CustomData_duplicate_referenced_layer_named( + bool *select_poly = (bool *)CustomData_get_layer_named_for_write( &mesh->pdata, CD_PROP_BOOL, ".select_poly", mesh->totpoly); if (!select_poly) { if (!value) { @@ -723,7 +723,7 @@ static void rna_MeshPolygon_flip(ID *id, MPoly *mp) { Mesh *me = (Mesh *)id; MLoop *loops = BKE_mesh_loops_for_write(me); - BKE_mesh_polygon_flip(mp, loops, &me->ldata); + BKE_mesh_polygon_flip(mp, loops, &me->ldata, me->totloop); BKE_mesh_tessface_clear(me); BKE_mesh_runtime_clear_geometry(me); } @@ -914,25 +914,25 @@ static bool rna_MEdge_freestyle_edge_mark_get(PointerRNA *ptr) { const Mesh *me = rna_mesh(ptr); const int index = rna_MeshEdge_index_get(ptr); - const FreestyleEdge *fed = CustomData_get(&me->edata, index, CD_FREESTYLE_EDGE); + const FreestyleEdge *fed = CustomData_get_layer(&me->edata, CD_FREESTYLE_EDGE); - return fed && (fed->flag & FREESTYLE_EDGE_MARK) != 0; + return fed && (fed[index].flag & FREESTYLE_EDGE_MARK) != 0; } static void rna_MEdge_freestyle_edge_mark_set(PointerRNA *ptr, bool value) { Mesh *me = rna_mesh(ptr); const int index = rna_MeshEdge_index_get(ptr); - FreestyleEdge *fed = CustomData_get(&me->edata, index, CD_FREESTYLE_EDGE); + FreestyleEdge *fed = CustomData_get_layer_for_write(&me->edata, CD_FREESTYLE_EDGE, me->totedge); if (!fed) { fed = CustomData_add_layer(&me->edata, CD_FREESTYLE_EDGE, CD_SET_DEFAULT, NULL, me->totedge); } if (value) { - fed->flag |= FREESTYLE_EDGE_MARK; + fed[index].flag |= FREESTYLE_EDGE_MARK; } else { - fed->flag &= ~FREESTYLE_EDGE_MARK; + fed[index].flag &= ~FREESTYLE_EDGE_MARK; } } @@ -940,25 +940,25 @@ static bool rna_MPoly_freestyle_face_mark_get(PointerRNA *ptr) { const Mesh *me = rna_mesh(ptr); const int index = rna_MeshPolygon_index_get(ptr); - const FreestyleFace *ffa = CustomData_get(&me->pdata, index, CD_FREESTYLE_FACE); + const FreestyleFace *ffa = CustomData_get_layer(&me->pdata, CD_FREESTYLE_FACE); - return ffa && (ffa->flag & FREESTYLE_FACE_MARK) != 0; + return ffa && (ffa[index].flag & FREESTYLE_FACE_MARK) != 0; } static void rna_MPoly_freestyle_face_mark_set(PointerRNA *ptr, bool value) { Mesh *me = rna_mesh(ptr); const int index = rna_MeshPolygon_index_get(ptr); - FreestyleFace *ffa = CustomData_get(&me->pdata, index, CD_FREESTYLE_FACE); + FreestyleFace *ffa = CustomData_get_layer_for_write(&me->pdata, CD_FREESTYLE_FACE, me->totpoly); if (!ffa) { ffa = CustomData_add_layer(&me->pdata, CD_FREESTYLE_FACE, CD_SET_DEFAULT, NULL, me->totpoly); } if (value) { - ffa->flag |= FREESTYLE_FACE_MARK; + ffa[index].flag |= FREESTYLE_FACE_MARK; } else { - ffa->flag &= ~FREESTYLE_FACE_MARK; + ffa[index].flag &= ~FREESTYLE_FACE_MARK; } } @@ -1466,7 +1466,7 @@ static bool rna_MeshEdge_hide_get(PointerRNA *ptr) static void rna_MeshEdge_hide_set(PointerRNA *ptr, bool value) { Mesh *mesh = rna_mesh(ptr); - bool *hide_edge = (bool *)CustomData_duplicate_referenced_layer_named( + bool *hide_edge = (bool *)CustomData_get_layer_named_for_write( &mesh->edata, CD_PROP_BOOL, ".hide_edge", mesh->totedge); if (!hide_edge) { if (!value) { @@ -1492,7 +1492,7 @@ static bool rna_MeshEdge_select_get(PointerRNA *ptr) static void rna_MeshEdge_select_set(PointerRNA *ptr, bool value) { Mesh *mesh = rna_mesh(ptr); - bool *select_edge = (bool *)CustomData_duplicate_referenced_layer_named( + bool *select_edge = (bool *)CustomData_get_layer_named_for_write( &mesh->edata, CD_PROP_BOOL, ".select_edge", mesh->totedge); if (!select_edge) { if (!value) { @@ -1518,7 +1518,7 @@ static bool rna_MeshEdge_use_edge_sharp_get(PointerRNA *ptr) static void rna_MeshEdge_use_edge_sharp_set(PointerRNA *ptr, bool value) { Mesh *mesh = rna_mesh(ptr); - bool *sharp_edge = (bool *)CustomData_duplicate_referenced_layer_named( + bool *sharp_edge = (bool *)CustomData_get_layer_named_for_write( &mesh->edata, CD_PROP_BOOL, "sharp_edge", mesh->totedge); if (!sharp_edge) { if (!value) { @@ -1852,7 +1852,7 @@ static bool get_uv_index_and_layer(const PointerRNA *ptr, /* We don't know from which attribute the RNA pointer is from, so we need to scan them all. */ const int uv_layers_num = CustomData_number_of_layers(&mesh->ldata, CD_PROP_FLOAT2); for (int layer_i = 0; layer_i < uv_layers_num; layer_i++) { - const float(*layer_data)[2] = (float(*)[2])CustomData_get_layer_n( + const float(*layer_data)[2] = (const float(*)[2])CustomData_get_layer_n( &mesh->ldata, CD_PROP_FLOAT2, layer_i); const ptrdiff_t index = uv_coord - layer_data; if (index >= 0 && index < mesh->totloop) { diff --git a/source/blender/makesrna/intern/rna_mesh_api.c b/source/blender/makesrna/intern/rna_mesh_api.c index b36b14b95a4..12b9c805701 100644 --- a/source/blender/makesrna/intern/rna_mesh_api.c +++ b/source/blender/makesrna/intern/rna_mesh_api.c @@ -60,7 +60,7 @@ static void rna_Mesh_calc_tangents(Mesh *mesh, ReportList *reports, const char * float(*r_looptangents)[4]; if (CustomData_has_layer(&mesh->ldata, CD_MLOOPTANGENT)) { - r_looptangents = CustomData_get_layer(&mesh->ldata, CD_MLOOPTANGENT); + r_looptangents = CustomData_get_layer_for_write(&mesh->ldata, CD_MLOOPTANGENT, mesh->totloop); memset(r_looptangents, 0, sizeof(float[4]) * mesh->totloop); } else { diff --git a/source/blender/makesrna/intern/rna_pointcloud.c b/source/blender/makesrna/intern/rna_pointcloud.c index 904d011fa04..49317857441 100644 --- a/source/blender/makesrna/intern/rna_pointcloud.c +++ b/source/blender/makesrna/intern/rna_pointcloud.c @@ -35,7 +35,8 @@ static PointCloud *rna_pointcloud(const PointerRNA *ptr) static float (*get_pointcloud_positions(PointCloud *pointcloud))[3] { - return (float(*)[3])CustomData_get_layer_named(&pointcloud->pdata, CD_PROP_FLOAT3, "position"); + return (float(*)[3])CustomData_get_layer_named_for_write( + &pointcloud->pdata, CD_PROP_FLOAT3, "position", pointcloud->totpoint); } static const float (*get_pointcloud_positions_const(const PointCloud *pointcloud))[3] @@ -110,7 +111,8 @@ static float rna_Point_radius_get(PointerRNA *ptr) static void rna_Point_radius_set(PointerRNA *ptr, float value) { PointCloud *pointcloud = rna_pointcloud(ptr); - float *radii = (float *)CustomData_get_layer_named(&pointcloud->pdata, CD_PROP_FLOAT, "radius"); + float *radii = (float *)CustomData_get_layer_named_for_write( + &pointcloud->pdata, CD_PROP_FLOAT, "radius", pointcloud->totpoint); if (radii == NULL) { return; } diff --git a/source/blender/modifiers/intern/MOD_array.cc b/source/blender/modifiers/intern/MOD_array.cc index 665ed094631..6c15994e865 100644 --- a/source/blender/modifiers/intern/MOD_array.cc +++ b/source/blender/modifiers/intern/MOD_array.cc @@ -329,22 +329,26 @@ static void mesh_merge_transform(Mesh *result, } /* Set #CD_ORIGINDEX. */ - index_orig = static_cast(CustomData_get_layer(&result->vdata, CD_ORIGINDEX)); + index_orig = static_cast( + CustomData_get_layer_for_write(&result->vdata, CD_ORIGINDEX, result->totvert)); if (index_orig) { copy_vn_i(index_orig + cap_verts_index, cap_nverts, ORIGINDEX_NONE); } - index_orig = static_cast(CustomData_get_layer(&result->edata, CD_ORIGINDEX)); + index_orig = static_cast( + CustomData_get_layer_for_write(&result->edata, CD_ORIGINDEX, result->totedge)); if (index_orig) { copy_vn_i(index_orig + cap_edges_index, cap_nedges, ORIGINDEX_NONE); } - index_orig = static_cast(CustomData_get_layer(&result->pdata, CD_ORIGINDEX)); + index_orig = static_cast( + CustomData_get_layer_for_write(&result->pdata, CD_ORIGINDEX, result->totpoly)); if (index_orig) { copy_vn_i(index_orig + cap_polys_index, cap_npolys, ORIGINDEX_NONE); } - index_orig = static_cast(CustomData_get_layer(&result->ldata, CD_ORIGINDEX)); + index_orig = static_cast( + CustomData_get_layer_for_write(&result->ldata, CD_ORIGINDEX, result->totloop)); if (index_orig) { copy_vn_i(index_orig + cap_loops_index, cap_nloops, ORIGINDEX_NONE); } @@ -663,7 +667,7 @@ static Mesh *arrayModifier_doArray(ArrayModifierData *amd, const int totuv = CustomData_number_of_layers(&result->ldata, CD_PROP_FLOAT2); for (i = 0; i < totuv; i++) { blender::float2 *dmloopuv = static_cast( - CustomData_get_layer_n(&result->ldata, CD_PROP_FLOAT2, i)); + CustomData_get_layer_n_for_write(&result->ldata, CD_PROP_FLOAT2, i, result->totloop)); dmloopuv += chunk_nloops; for (c = 1; c < count; c++) { const float uv_offset[2] = { diff --git a/source/blender/modifiers/intern/MOD_cloth.c b/source/blender/modifiers/intern/MOD_cloth.c index dcb4803b08a..379ed32fce5 100644 --- a/source/blender/modifiers/intern/MOD_cloth.c +++ b/source/blender/modifiers/intern/MOD_cloth.c @@ -113,7 +113,8 @@ static void deformVerts(ModifierData *md, clmd->sim_parms->shapekey_rest); if (kb && kb->data != NULL) { float(*layerorco)[3]; - if (!(layerorco = CustomData_get_layer(&mesh_src->vdata, CD_CLOTH_ORCO))) { + if (!(layerorco = CustomData_get_layer_for_write( + &mesh_src->vdata, CD_CLOTH_ORCO, mesh_src->totvert))) { layerorco = CustomData_add_layer( &mesh_src->vdata, CD_CLOTH_ORCO, CD_SET_DEFAULT, NULL, mesh_src->totvert); } diff --git a/source/blender/modifiers/intern/MOD_displace.cc b/source/blender/modifiers/intern/MOD_displace.cc index 402752610eb..304c4be3e02 100644 --- a/source/blender/modifiers/intern/MOD_displace.cc +++ b/source/blender/modifiers/intern/MOD_displace.cc @@ -307,7 +307,8 @@ static void displaceModifier_do(DisplaceModifierData *dmd, BKE_mesh_calc_normals_split(mesh); } - float(*clnors)[3] = static_cast(CustomData_get_layer(ldata, CD_NORMAL)); + float(*clnors)[3] = static_cast( + CustomData_get_layer_for_write(ldata, CD_NORMAL, mesh->totloop)); vert_clnors = static_cast( MEM_malloc_arrayN(verts_num, sizeof(*vert_clnors), __func__)); BKE_mesh_normals_loop_to_vertex( diff --git a/source/blender/modifiers/intern/MOD_explode.c b/source/blender/modifiers/intern/MOD_explode.c index 819ffbba26d..9dcbb1f16a0 100644 --- a/source/blender/modifiers/intern/MOD_explode.c +++ b/source/blender/modifiers/intern/MOD_explode.c @@ -100,7 +100,7 @@ static void createFacepa(ExplodeModifierData *emd, ParticleSystemModifierData *p const bool invert_vgroup = (emd->flag & eExplodeFlag_INVERT_VGROUP) != 0; float(*positions)[3] = BKE_mesh_vert_positions_for_write(mesh); - mface = (MFace *)CustomData_get_layer(&mesh->fdata, CD_MFACE); + mface = (MFace *)CustomData_get_layer_for_write(&mesh->fdata, CD_MFACE, mesh->totface); totvert = mesh->totvert; totface = mesh->totface; totpart = psmd->psys->totpart; @@ -215,7 +215,7 @@ static const short add_faces[24] = { static MFace *get_dface(Mesh *mesh, Mesh *split, int cur, int i, MFace *mf) { - MFace *mfaces = CustomData_get_layer(&split->fdata, CD_MFACE); + MFace *mfaces = CustomData_get_layer_for_write(&split->fdata, CD_MFACE, split->totface); MFace *df = &mfaces[cur]; CustomData_copy_data(&mesh->fdata, &split->fdata, i, cur, 1); *df = *mf; @@ -284,11 +284,11 @@ static void remap_uvs_3_6_9_12( int l; for (l = 0; l < layers_num; l++) { - mf = CustomData_get_layer_n(&split->fdata, CD_MTFACE, l); + mf = CustomData_get_layer_n_for_write(&split->fdata, CD_MTFACE, l, split->totface); df1 = mf + cur; df2 = df1 + 1; df3 = df1 + 2; - mf = CustomData_get_layer_n(&mesh->fdata, CD_MTFACE, l); + mf = CustomData_get_layer_n_for_write(&mesh->fdata, CD_MTFACE, l, mesh->totface); mf += i; copy_v2_v2(df1->uv[0], mf->uv[c0]); @@ -344,10 +344,10 @@ static void remap_uvs_5_10( int l; for (l = 0; l < layers_num; l++) { - mf = CustomData_get_layer_n(&split->fdata, CD_MTFACE, l); + mf = CustomData_get_layer_n_for_write(&split->fdata, CD_MTFACE, l, split->totface); df1 = mf + cur; df2 = df1 + 1; - mf = CustomData_get_layer_n(&mesh->fdata, CD_MTFACE, l); + mf = CustomData_get_layer_n_for_write(&mesh->fdata, CD_MTFACE, l, mesh->totface); mf += i; copy_v2_v2(df1->uv[0], mf->uv[c0]); @@ -416,12 +416,12 @@ static void remap_uvs_15( int l; for (l = 0; l < layers_num; l++) { - mf = CustomData_get_layer_n(&split->fdata, CD_MTFACE, l); + mf = CustomData_get_layer_n_for_write(&split->fdata, CD_MTFACE, l, split->totface); df1 = mf + cur; df2 = df1 + 1; df3 = df1 + 2; df4 = df1 + 3; - mf = CustomData_get_layer_n(&mesh->fdata, CD_MTFACE, l); + mf = CustomData_get_layer_n_for_write(&mesh->fdata, CD_MTFACE, l, mesh->totface); mf += i; copy_v2_v2(df1->uv[0], mf->uv[c0]); @@ -492,11 +492,11 @@ static void remap_uvs_7_11_13_14( int l; for (l = 0; l < layers_num; l++) { - mf = CustomData_get_layer_n(&split->fdata, CD_MTFACE, l); + mf = CustomData_get_layer_n_for_write(&split->fdata, CD_MTFACE, l, split->totface); df1 = mf + cur; df2 = df1 + 1; df3 = df1 + 2; - mf = CustomData_get_layer_n(&mesh->fdata, CD_MTFACE, l); + mf = CustomData_get_layer_n_for_write(&mesh->fdata, CD_MTFACE, l, mesh->totface); mf += i; copy_v2_v2(df1->uv[0], mf->uv[c0]); @@ -552,10 +552,10 @@ static void remap_uvs_19_21_22( int l; for (l = 0; l < layers_num; l++) { - mf = CustomData_get_layer_n(&split->fdata, CD_MTFACE, l); + mf = CustomData_get_layer_n_for_write(&split->fdata, CD_MTFACE, l, split->totface); df1 = mf + cur; df2 = df1 + 1; - mf = CustomData_get_layer_n(&mesh->fdata, CD_MTFACE, l); + mf = CustomData_get_layer_n_for_write(&mesh->fdata, CD_MTFACE, l, mesh->totface); mf += i; copy_v2_v2(df1->uv[0], mf->uv[c0]); @@ -614,10 +614,10 @@ static void remap_uvs_23( int l; for (l = 0; l < layers_num; l++) { - mf = CustomData_get_layer_n(&split->fdata, CD_MTFACE, l); + mf = CustomData_get_layer_n_for_write(&split->fdata, CD_MTFACE, l, split->totface); df1 = mf + cur; df2 = df1 + 1; - mf = CustomData_get_layer_n(&mesh->fdata, CD_MTFACE, l); + mf = CustomData_get_layer_n_for_write(&mesh->fdata, CD_MTFACE, l, mesh->totface); mf += i; copy_v2_v2(df1->uv[0], mf->uv[c0]); @@ -639,7 +639,7 @@ static Mesh *cutEdges(ExplodeModifierData *emd, Mesh *mesh) { Mesh *split_m; MFace *mf = NULL, *df1 = NULL; - MFace *mface = CustomData_get_layer(&mesh->fdata, CD_MFACE); + MFace *mface = CustomData_get_layer_for_write(&mesh->fdata, CD_MFACE, mesh->totface); float *dupve; EdgeHash *edgehash; EdgeHashIterator *ehi; @@ -869,7 +869,7 @@ static Mesh *cutEdges(ExplodeModifierData *emd, Mesh *mesh) curdupface += add_faces[*fs] + 1; } - MFace *split_mface = CustomData_get_layer(&split_m->fdata, CD_MFACE); + MFace *split_mface = CustomData_get_layer_for_write(&split_m->fdata, CD_MFACE, split_m->totface); for (i = 0; i < curdupface; i++) { mf = &split_mface[i]; BKE_mesh_mface_index_validate(mf, &split_m->fdata, i, ((mf->flag & ME_FACE_SEL) ? 4 : 3)); @@ -909,7 +909,7 @@ static Mesh *explodeMesh(ExplodeModifierData *emd, totface = mesh->totface; totvert = mesh->totvert; - mface = CustomData_get_layer(&mesh->fdata, CD_MFACE); + mface = CustomData_get_layer_for_write(&mesh->fdata, CD_MFACE, mesh->totface); totpart = psmd->psys->totpart; sim.depsgraph = ctx->depsgraph; @@ -971,7 +971,8 @@ static Mesh *explodeMesh(ExplodeModifierData *emd, /* the final duplicated vertices */ explode = BKE_mesh_new_nomain_from_template(mesh, totdup, 0, totface - delface, 0, 0); - MTFace *mtface = CustomData_get_layer_named(&explode->fdata, CD_MTFACE, emd->uvname); + MTFace *mtface = CustomData_get_layer_named_for_write( + &explode->fdata, CD_MTFACE, emd->uvname, explode->totface); /* getting back to object space */ invert_m4_m4(imat, ctx->object->object_to_world); @@ -1029,7 +1030,8 @@ static Mesh *explodeMesh(ExplodeModifierData *emd, BLI_edgehashIterator_free(ehi); /* Map new vertices to faces. */ - MFace *explode_mface = CustomData_get_layer(&explode->fdata, CD_MFACE); + MFace *explode_mface = CustomData_get_layer_for_write( + &explode->fdata, CD_MFACE, explode->totface); for (i = 0, u = 0; i < totface; i++) { MFace source; int orig_v4; diff --git a/source/blender/modifiers/intern/MOD_multires.cc b/source/blender/modifiers/intern/MOD_multires.cc index 8b379f8ac60..737999e6b88 100644 --- a/source/blender/modifiers/intern/MOD_multires.cc +++ b/source/blender/modifiers/intern/MOD_multires.cc @@ -261,7 +261,8 @@ static Mesh *modifyMesh(ModifierData *md, const ModifierEvalContext *ctx, Mesh * result = multires_as_mesh(mmd, ctx, mesh, subdiv); if (use_clnors) { - float(*lnors)[3] = static_cast(CustomData_get_layer(&result->ldata, CD_NORMAL)); + float(*lnors)[3] = static_cast( + CustomData_get_layer_for_write(&result->ldata, CD_NORMAL, result->totloop)); BLI_assert(lnors != nullptr); BKE_mesh_set_custom_normals(result, lnors); CustomData_set_layer_flag(&mesh->ldata, CD_NORMAL, CD_FLAG_TEMPORARY); diff --git a/source/blender/modifiers/intern/MOD_normal_edit.cc b/source/blender/modifiers/intern/MOD_normal_edit.cc index 964e62ab79d..0bf1a268577 100644 --- a/source/blender/modifiers/intern/MOD_normal_edit.cc +++ b/source/blender/modifiers/intern/MOD_normal_edit.cc @@ -177,12 +177,13 @@ static void mix_normals(const float mix_factor, static bool polygons_check_flip(MLoop *mloop, float (*nos)[3], CustomData *ldata, + const int totloop, const MPoly *mpoly, float (*poly_normals)[3], const int polys_num) { const MPoly *mp; - MDisps *mdisp = static_cast(CustomData_get_layer(ldata, CD_MDISPS)); + MDisps *mdisp = static_cast(CustomData_get_layer_for_write(ldata, CD_MDISPS, totloop)); int i; bool flipped = false; @@ -325,9 +326,13 @@ static void normalEditModifier_do_radial(NormalEditModifierData *enmd, loops_num); } - if (do_polynors_fix && - polygons_check_flip( - mloop, nos, &mesh->ldata, mpoly, BKE_mesh_poly_normals_for_write(mesh), polys_num)) { + if (do_polynors_fix && polygons_check_flip(mloop, + nos, + &mesh->ldata, + mesh->totloop, + mpoly, + BKE_mesh_poly_normals_for_write(mesh), + polys_num)) { /* We need to recompute vertex normals! */ BKE_mesh_normals_tag_dirty(mesh); } @@ -444,9 +449,13 @@ static void normalEditModifier_do_directional(NormalEditModifierData *enmd, loops_num); } - if (do_polynors_fix && - polygons_check_flip( - mloop, nos, &mesh->ldata, mpoly, BKE_mesh_poly_normals_for_write(mesh), polys_num)) { + if (do_polynors_fix && polygons_check_flip(mloop, + nos, + &mesh->ldata, + mesh->totloop, + mpoly, + BKE_mesh_poly_normals_for_write(mesh), + polys_num)) { BKE_mesh_normals_tag_dirty(mesh); } @@ -553,10 +562,11 @@ static Mesh *normalEditModifier_do(NormalEditModifierData *enmd, bke::SpanAttributeWriter sharp_edges = attributes.lookup_or_add_for_write_span( "sharp_edge", ATTR_DOMAIN_EDGE); - short(*clnors)[2] = static_cast(CustomData_get_layer(ldata, CD_CUSTOMLOOPNORMAL)); + short(*clnors)[2] = static_cast( + CustomData_get_layer_for_write(ldata, CD_CUSTOMLOOPNORMAL, loops_num)); if (use_current_clnors) { clnors = static_cast( - CustomData_duplicate_referenced_layer(ldata, CD_CUSTOMLOOPNORMAL, loops_num)); + CustomData_get_layer_for_write(ldata, CD_CUSTOMLOOPNORMAL, loops_num)); loop_normals = static_cast( MEM_malloc_arrayN(size_t(loops_num), sizeof(*loop_normals), __func__)); diff --git a/source/blender/modifiers/intern/MOD_particleinstance.c b/source/blender/modifiers/intern/MOD_particleinstance.c index e0677751a16..e3682acf84a 100644 --- a/source/blender/modifiers/intern/MOD_particleinstance.c +++ b/source/blender/modifiers/intern/MOD_particleinstance.c @@ -322,10 +322,10 @@ static Mesh *modifyMesh(ModifierData *md, const ModifierEvalContext *ctx, Mesh * MPoly *mpoly = BKE_mesh_polys_for_write(result); MLoop *mloop = BKE_mesh_loops_for_write(result); - MLoopCol *mloopcols_index = CustomData_get_layer_named( - &result->ldata, CD_PROP_BYTE_COLOR, pimd->index_layer_name); - MLoopCol *mloopcols_value = CustomData_get_layer_named( - &result->ldata, CD_PROP_BYTE_COLOR, pimd->value_layer_name); + MLoopCol *mloopcols_index = CustomData_get_layer_named_for_write( + &result->ldata, CD_PROP_BYTE_COLOR, pimd->index_layer_name, result->totloop); + MLoopCol *mloopcols_value = CustomData_get_layer_named_for_write( + &result->ldata, CD_PROP_BYTE_COLOR, pimd->value_layer_name, result->totloop); int *vert_part_index = NULL; float *vert_part_value = NULL; if (mloopcols_index != NULL) { diff --git a/source/blender/modifiers/intern/MOD_screw.cc b/source/blender/modifiers/intern/MOD_screw.cc index 82592a13faf..0f094f06bd0 100644 --- a/source/blender/modifiers/intern/MOD_screw.cc +++ b/source/blender/modifiers/intern/MOD_screw.cc @@ -394,7 +394,8 @@ static Mesh *modifyMesh(ModifierData *md, const ModifierEvalContext *ctx, Mesh * CustomData_add_layer(&result->pdata, CD_ORIGINDEX, CD_SET_DEFAULT, nullptr, int(maxPolys)); } - int *origindex = static_cast(CustomData_get_layer(&result->pdata, CD_ORIGINDEX)); + int *origindex = static_cast( + CustomData_get_layer_for_write(&result->pdata, CD_ORIGINDEX, result->totpoly)); CustomData_copy_data(&mesh->vdata, &result->vdata, 0, 0, int(totvert)); @@ -406,8 +407,8 @@ static Mesh *modifyMesh(ModifierData *md, const ModifierEvalContext *ctx, Mesh * if (mloopuv_layers_tot) { uint uv_lay; for (uv_lay = 0; uv_lay < mloopuv_layers_tot; uv_lay++) { - mloopuv_layers[uv_lay] = static_cast( - CustomData_get_layer_n(&result->ldata, CD_PROP_FLOAT2, int(uv_lay))); + mloopuv_layers[uv_lay] = static_cast(CustomData_get_layer_n_for_write( + &result->ldata, CD_PROP_FLOAT2, int(uv_lay), result->totloop)); } if (ltmd->flag & MOD_SCREW_UV_STRETCH_V) { diff --git a/source/blender/modifiers/intern/MOD_skin.c b/source/blender/modifiers/intern/MOD_skin.c index b7b8c9cbe53..efb8ae8616a 100644 --- a/source/blender/modifiers/intern/MOD_skin.c +++ b/source/blender/modifiers/intern/MOD_skin.c @@ -922,7 +922,8 @@ static Mesh *subdivide_base(const Mesh *orig) float(*out_vert_positions)[3] = BKE_mesh_vert_positions_for_write(result); MEdge *outedge = BKE_mesh_edges_for_write(result); - MVertSkin *outnode = CustomData_get_layer(&result->vdata, CD_MVERT_SKIN); + MVertSkin *outnode = CustomData_get_layer_for_write( + &result->vdata, CD_MVERT_SKIN, result->totvert); MDeformVert *outdvert = NULL; if (origdvert) { outdvert = BKE_mesh_deform_verts_for_write(result); @@ -1909,7 +1910,7 @@ static void skin_set_orig_indices(Mesh *mesh) static Mesh *base_skin(Mesh *origmesh, SkinModifierData *smd, eSkinErrorFlag *r_error) { Mesh *result; - MVertSkin *nodes; + const MVertSkin *nodes; BMesh *bm; EMat *emat; SkinNode *skin_nodes; diff --git a/source/blender/modifiers/intern/MOD_solidify_extrude.c b/source/blender/modifiers/intern/MOD_solidify_extrude.c index 468f7af4f80..5013166cb3b 100644 --- a/source/blender/modifiers/intern/MOD_solidify_extrude.c +++ b/source/blender/modifiers/intern/MOD_solidify_extrude.c @@ -1042,7 +1042,7 @@ Mesh *MOD_solidify_extrude_modifyMesh(ModifierData *md, const ModifierEvalContex } /* add faces & edges */ - origindex_edge = CustomData_get_layer(&result->edata, CD_ORIGINDEX); + origindex_edge = CustomData_get_layer_for_write(&result->edata, CD_ORIGINDEX, result->totedge); orig_ed = (origindex_edge) ? &origindex_edge[(edges_num * stride) + newEdges] : NULL; MEdge *ed = &medge[(edges_num * stride) + newEdges]; /* start after copied edges */ for (i = 0; i < rimVerts; i++, ed++) { diff --git a/source/blender/modifiers/intern/MOD_solidify_nonmanifold.c b/source/blender/modifiers/intern/MOD_solidify_nonmanifold.c index 41217955f85..51043ea89e3 100644 --- a/source/blender/modifiers/intern/MOD_solidify_nonmanifold.c +++ b/source/blender/modifiers/intern/MOD_solidify_nonmanifold.c @@ -1966,10 +1966,13 @@ Mesh *MOD_solidify_nonmanifold_modifyMesh(ModifierData *md, MPoly *mpoly = BKE_mesh_polys_for_write(result); MLoop *mloop = BKE_mesh_loops_for_write(result); - int *origindex_edge = CustomData_get_layer(&result->edata, CD_ORIGINDEX); - int *origindex_poly = CustomData_get_layer(&result->pdata, CD_ORIGINDEX); + int *origindex_edge = CustomData_get_layer_for_write( + &result->edata, CD_ORIGINDEX, result->totedge); + int *origindex_poly = CustomData_get_layer_for_write( + &result->pdata, CD_ORIGINDEX, result->totpoly); - float *result_edge_bweight = CustomData_get_layer(&result->edata, CD_BWEIGHT); + float *result_edge_bweight = CustomData_get_layer_for_write( + &result->edata, CD_BWEIGHT, result->totedge); if (bevel_convex != 0.0f || orig_vert_bweight != NULL) { result_edge_bweight = CustomData_add_layer( &result->edata, CD_BWEIGHT, CD_SET_DEFAULT, NULL, result->totedge); diff --git a/source/blender/modifiers/intern/MOD_subsurf.cc b/source/blender/modifiers/intern/MOD_subsurf.cc index 5e77f0ffa9e..23989e7640c 100644 --- a/source/blender/modifiers/intern/MOD_subsurf.cc +++ b/source/blender/modifiers/intern/MOD_subsurf.cc @@ -265,7 +265,8 @@ static Mesh *modifyMesh(ModifierData *md, const ModifierEvalContext *ctx, Mesh * } if (use_clnors) { - float(*lnors)[3] = static_cast(CustomData_get_layer(&result->ldata, CD_NORMAL)); + float(*lnors)[3] = static_cast( + CustomData_get_layer_for_write(&result->ldata, CD_NORMAL, result->totloop)); BLI_assert(lnors != nullptr); BKE_mesh_set_custom_normals(result, lnors); CustomData_set_layer_flag(&mesh->ldata, CD_NORMAL, CD_FLAG_TEMPORARY); diff --git a/source/blender/modifiers/intern/MOD_triangulate.cc b/source/blender/modifiers/intern/MOD_triangulate.cc index 73ec3e25afe..3880bd8a233 100644 --- a/source/blender/modifiers/intern/MOD_triangulate.cc +++ b/source/blender/modifiers/intern/MOD_triangulate.cc @@ -74,7 +74,8 @@ static Mesh *triangulate_mesh(Mesh *mesh, BM_mesh_free(bm); if (keep_clnors) { - float(*lnors)[3] = static_cast(CustomData_get_layer(&result->ldata, CD_NORMAL)); + float(*lnors)[3] = static_cast( + CustomData_get_layer_for_write(&result->ldata, CD_NORMAL, result->totloop)); BLI_assert(lnors != nullptr); BKE_mesh_set_custom_normals(result, lnors); diff --git a/source/blender/modifiers/intern/MOD_uvproject.cc b/source/blender/modifiers/intern/MOD_uvproject.cc index 20b5d45ebcc..1d660a05c44 100644 --- a/source/blender/modifiers/intern/MOD_uvproject.cc +++ b/source/blender/modifiers/intern/MOD_uvproject.cc @@ -184,9 +184,8 @@ static Mesh *uvprojectModifier_do(UVProjectModifierData *umd, polys_num = mesh->totpoly; loops_num = mesh->totloop; - /* make sure we are not modifying the original UV map */ - float(*mloop_uv)[2] = static_cast(CustomData_duplicate_referenced_layer_named( - &mesh->ldata, CD_PROP_FLOAT2, uvname, loops_num)); + float(*mloop_uv)[2] = static_cast( + CustomData_get_layer_named_for_write(&mesh->ldata, CD_PROP_FLOAT2, uvname, loops_num)); coords = BKE_mesh_vert_coords_alloc(mesh, &verts_num); diff --git a/source/blender/modifiers/intern/MOD_uvwarp.cc b/source/blender/modifiers/intern/MOD_uvwarp.cc index ff036597f01..2c11b002fc5 100644 --- a/source/blender/modifiers/intern/MOD_uvwarp.cc +++ b/source/blender/modifiers/intern/MOD_uvwarp.cc @@ -197,9 +197,8 @@ static Mesh *modifyMesh(ModifierData *md, const ModifierEvalContext *ctx, Mesh * polys_num = mesh->totpoly; loops_num = mesh->totloop; - /* make sure we are not modifying the original UV map */ - float(*mloopuv)[2] = static_cast(CustomData_duplicate_referenced_layer_named( - &mesh->ldata, CD_PROP_FLOAT2, uvname, loops_num)); + float(*mloopuv)[2] = static_cast( + CustomData_get_layer_named_for_write(&mesh->ldata, CD_PROP_FLOAT2, uvname, loops_num)); MOD_get_vgroup(ctx->object, mesh, umd->vgroup_name, &dvert, &defgrp_index); UVWarpData data{}; diff --git a/source/blender/modifiers/intern/MOD_weighted_normal.cc b/source/blender/modifiers/intern/MOD_weighted_normal.cc index 416ce19a0c5..f8f84fcd735 100644 --- a/source/blender/modifiers/intern/MOD_weighted_normal.cc +++ b/source/blender/modifiers/intern/MOD_weighted_normal.cc @@ -614,7 +614,7 @@ static Mesh *modifyMesh(ModifierData *md, const ModifierEvalContext *ctx, Mesh * const float split_angle = mesh->smoothresh; short(*clnors)[2] = static_cast( - CustomData_get_layer(&result->ldata, CD_CUSTOMLOOPNORMAL)); + CustomData_get_layer_for_write(&result->ldata, CD_CUSTOMLOOPNORMAL, mesh->totloop)); /* Keep info whether we had clnors, * it helps when generating clnor spaces and default normals. */ diff --git a/source/blender/nodes/geometry/nodes/node_geo_extrude_mesh.cc b/source/blender/nodes/geometry/nodes/node_geo_extrude_mesh.cc index 1362e6e051c..29a5cb9b83c 100644 --- a/source/blender/nodes/geometry/nodes/node_geo_extrude_mesh.cc +++ b/source/blender/nodes/geometry/nodes/node_geo_extrude_mesh.cc @@ -143,7 +143,8 @@ static MutableSpan get_orig_index_layer(Mesh &mesh, const eAttrDomain domai { const bke::AttributeAccessor attributes = mesh.attributes(); CustomData &custom_data = get_customdata(mesh, domain); - if (int *orig_indices = static_cast(CustomData_get_layer(&custom_data, CD_ORIGINDEX))) { + if (int *orig_indices = static_cast(CustomData_get_layer_for_write( + &custom_data, CD_ORIGINDEX, attributes.domain_size(domain)))) { return {orig_indices, attributes.domain_size(domain)}; } return {}; diff --git a/source/blender/nodes/geometry/nodes/node_geo_subdivision_surface.cc b/source/blender/nodes/geometry/nodes/node_geo_subdivision_surface.cc index b6e0eeea3ff..3c06ce8b3f0 100644 --- a/source/blender/nodes/geometry/nodes/node_geo_subdivision_surface.cc +++ b/source/blender/nodes/geometry/nodes/node_geo_subdivision_surface.cc @@ -70,7 +70,7 @@ static void write_vertex_creases(Mesh &mesh, const VArray &crease_varray) float *crease; if (CustomData_has_layer(&mesh.vdata, CD_CREASE)) { crease = static_cast( - CustomData_duplicate_referenced_layer(&mesh.vdata, CD_CREASE, mesh.totvert)); + CustomData_get_layer_for_write(&mesh.vdata, CD_CREASE, mesh.totvert)); } else { crease = static_cast(