diff options
author | Hans Goudey <h.goudey@me.com> | 2022-08-31 17:09:01 +0300 |
---|---|---|
committer | Hans Goudey <h.goudey@me.com> | 2022-08-31 17:09:01 +0300 |
commit | f1c0249f34c4171ec311b5b9882e36fed5889259 (patch) | |
tree | 7dd630c9301b8dd468c8279e8c446c62824fb6d6 /source/blender/gpencil_modifiers | |
parent | 3e73afb5360592fe2bf43419e216035ea3c281f9 (diff) |
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
Diffstat (limited to 'source/blender/gpencil_modifiers')
-rw-r--r-- | source/blender/gpencil_modifiers/intern/lineart/lineart_cpu.c | 17 |
1 files changed, 13 insertions, 4 deletions
diff --git a/source/blender/gpencil_modifiers/intern/lineart/lineart_cpu.c b/source/blender/gpencil_modifiers/intern/lineart/lineart_cpu.c index 3e4e833438d..5bca49f632c 100644 --- a/source/blender/gpencil_modifiers/intern/lineart/lineart_cpu.c +++ b/source/blender/gpencil_modifiers/intern/lineart/lineart_cpu.c @@ -1472,6 +1472,7 @@ typedef struct EdgeFeatData { Mesh *me; Object *ob_eval; /* For evaluated materials. */ const MLoopTri *mlooptri; + const int *material_indices; LineartTriangle *tri_array; LineartVert *v_array; float crease_threshold; @@ -1503,6 +1504,7 @@ static void lineart_identify_mlooptri_feature_edges(void *__restrict userdata, EdgeFeatData *e_feat_data = (EdgeFeatData *)userdata; EdgeFeatReduceData *reduce_data = (EdgeFeatReduceData *)tls->userdata_chunk; Mesh *me = e_feat_data->me; + const int *material_indices = e_feat_data->material_indices; Object *ob_eval = e_feat_data->ob_eval; LineartEdgeNeighbor *edge_nabr = e_feat_data->edge_nabr; const MLoopTri *mlooptri = e_feat_data->mlooptri; @@ -1649,8 +1651,8 @@ static void lineart_identify_mlooptri_feature_edges(void *__restrict userdata, } } - int mat1 = me->mpoly[mlooptri[f1].poly].mat_nr; - int mat2 = me->mpoly[mlooptri[f2].poly].mat_nr; + int mat1 = material_indices ? material_indices[mlooptri[f1].poly] : 0; + int mat2 = material_indices ? material_indices[mlooptri[f2].poly] : 0; if (mat1 != mat2) { Material *m1 = BKE_object_material_get_eval(ob_eval, mat1 + 1); @@ -1841,6 +1843,7 @@ static void lineart_triangle_adjacent_assign(LineartTriangle *tri, typedef struct TriData { LineartObjectInfo *ob_info; const MLoopTri *mlooptri; + const int *material_indices; LineartVert *vert_arr; LineartTriangle *tri_arr; int lineart_triangle_size; @@ -1855,6 +1858,7 @@ static void lineart_load_tri_task(void *__restrict userdata, Mesh *me = tri_task_data->ob_info->original_me; LineartObjectInfo *ob_info = tri_task_data->ob_info; const MLoopTri *mlooptri = &tri_task_data->mlooptri[i]; + const int *material_indices = tri_task_data->material_indices; LineartVert *vert_arr = tri_task_data->vert_arr; LineartTriangle *tri = tri_task_data->tri_arr; @@ -1869,8 +1873,8 @@ static void lineart_load_tri_task(void *__restrict userdata, tri->v[2] = &vert_arr[v3]; /* Material mask bits and occlusion effectiveness assignment. */ - Material *mat = BKE_object_material_get_eval(ob_info->original_ob_eval, - me->mpoly[mlooptri->poly].mat_nr + 1); + Material *mat = BKE_object_material_get( + ob_info->original_ob_eval, material_indices ? material_indices[mlooptri->poly] + 1 : 1); tri->material_mask_bits |= ((mat && (mat->lineart.flags & LRT_MATERIAL_MASK_ENABLED)) ? mat->lineart.material_mask_bits : 0); @@ -1992,6 +1996,9 @@ static void lineart_geometry_object_load(LineartObjectInfo *ob_info, const MLoopTri *mlooptri = BKE_mesh_runtime_looptri_ensure(me); const int tot_tri = BKE_mesh_runtime_looptri_len(me); + const int *material_indices = (const int *)CustomData_get_layer_named( + &me->pdata, CD_PROP_INT32, "material_index"); + /* Check if we should look for custom data tags like Freestyle edges or faces. */ bool can_find_freestyle_edge = false; int layer_index = CustomData_get_active_layer_index(&me->edata, CD_FREESTYLE_EDGE); @@ -2095,6 +2102,7 @@ static void lineart_geometry_object_load(LineartObjectInfo *ob_info, TriData tri_data; tri_data.ob_info = ob_info; tri_data.mlooptri = mlooptri; + tri_data.material_indices = material_indices; tri_data.vert_arr = la_v_arr; tri_data.tri_arr = la_tri_arr; tri_data.lineart_triangle_size = la_data->sizeof_triangle; @@ -2122,6 +2130,7 @@ static void lineart_geometry_object_load(LineartObjectInfo *ob_info, edge_feat_data.me = me; edge_feat_data.ob_eval = ob_info->original_ob_eval; edge_feat_data.mlooptri = mlooptri; + edge_feat_data.material_indices = material_indices; edge_feat_data.edge_nabr = lineart_build_edge_neighbor(me, total_edges); edge_feat_data.tri_array = la_tri_arr; edge_feat_data.v_array = la_v_arr; |