From f1c0249f34c4171ec311b5b9882e36fed5889259 Mon Sep 17 00:00:00 2001 From: Hans Goudey Date: Wed, 31 Aug 2022 09:09:01 -0500 Subject: Mesh: Move material indices to a generic attribute This patch moves material indices from the mesh `MPoly` struct to a generic integer attribute. The builtin material index was already exposed in geometry nodes, but this makes it a "proper" attribute accessible with Python and visible in the "Attributes" panel. The goals of the refactor are code simplification and memory and performance improvements, mainly because the attribute doesn't have to be stored and processed if there are no materials. However, until 4.0, material indices will still be read and written in the old format, meaning there may be a temporary increase in memory usage. Further notes: * Completely removing the `MPoly.mat_nr` after 4.0 may require changes to DNA or introducing a new `MPoly` type. * Geometry nodes regression tests didn't look at material indices, so the change reveals a bug in the realize instances node that I fixed. * Access to material indices from the RNA `MeshPolygon` type is slower with this patch. The `material_index` attribute can be used instead. * Cycles is changed to read from the attribute instead. * BMesh isn't changed in this patch. Theoretically it could be though, to save 2 bytes per face when less than two materials are used. * Eventually we could use a 16 bit integer attribute type instead. Ref T95967 Differential Revision: https://developer.blender.org/D15675 --- source/blender/blenkernel/BKE_mesh.h | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'source/blender/blenkernel/BKE_mesh.h') diff --git a/source/blender/blenkernel/BKE_mesh.h b/source/blender/blenkernel/BKE_mesh.h index 8cf973b785c..ec6799ee995 100644 --- a/source/blender/blenkernel/BKE_mesh.h +++ b/source/blender/blenkernel/BKE_mesh.h @@ -6,6 +6,9 @@ * \ingroup bke */ +#include "DNA_mesh_types.h" + +#include "BKE_customdata.h" #include "BKE_mesh_types.h" #include "BLI_compiler_attrs.h" #include "BLI_utildefines.h" @@ -1019,6 +1022,30 @@ char *BKE_mesh_debug_info(const struct Mesh *me) void BKE_mesh_debug_print(const struct Mesh *me) ATTR_NONNULL(1); #endif +/** + * \return The material index for each polygon. May be null. + * \note In C++ code, prefer using the attribute API (#MutableAttributeAccessor)/ + */ +BLI_INLINE const int *BKE_mesh_material_indices(const Mesh *mesh) +{ + return (const int *)CustomData_get_layer_named(&mesh->pdata, CD_PROP_INT32, "material_index"); +} + +/** + * \return The material index for each polygon. Create the layer if it doesn't exist. + * \note In C++ code, prefer using the attribute API (#MutableAttributeAccessor)/ + */ +BLI_INLINE int *BKE_mesh_material_indices_for_write(Mesh *mesh) +{ + int *indices = (int *)CustomData_duplicate_referenced_layer_named( + &mesh->pdata, CD_PROP_INT32, "material_index", mesh->totpoly); + if (indices) { + return indices; + } + return (int *)CustomData_add_layer_named( + &mesh->pdata, CD_PROP_INT32, CD_SET_DEFAULT, NULL, mesh->totpoly, "material_index"); +} + #ifdef __cplusplus } #endif -- cgit v1.2.3