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/render | |
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/render')
-rw-r--r-- | source/blender/render/intern/bake.c | 5 | ||||
-rw-r--r-- | source/blender/render/intern/multires_bake.c | 6 |
2 files changed, 7 insertions, 4 deletions
diff --git a/source/blender/render/intern/bake.c b/source/blender/render/intern/bake.c index 74e9662d5db..d6e612ee061 100644 --- a/source/blender/render/intern/bake.c +++ b/source/blender/render/intern/bake.c @@ -740,14 +740,15 @@ void RE_bake_pixels_populate(Mesh *me, BKE_mesh_recalc_looptri(me->mloop, me->mpoly, me->mvert, me->totloop, me->totpoly, looptri); + const int *material_indices = BKE_mesh_material_indices(me); + for (int i = 0; i < tottri; i++) { const MLoopTri *lt = &looptri[i]; - const MPoly *mp = &me->mpoly[lt->poly]; bd.primitive_id = i; /* Find images matching this material. */ - Image *image = targets->material_to_image[mp->mat_nr]; + Image *image = targets->material_to_image[material_indices ? material_indices[lt->poly] : 0]; for (int image_id = 0; image_id < targets->images_num; image_id++) { BakeImage *bk_image = &targets->images[image_id]; if (bk_image->image != image) { diff --git a/source/blender/render/intern/multires_bake.c b/source/blender/render/intern/multires_bake.c index e3229e20595..e116f41321f 100644 --- a/source/blender/render/intern/multires_bake.c +++ b/source/blender/render/intern/multires_bake.c @@ -63,6 +63,7 @@ typedef struct { MVert *mvert; const float (*vert_normals)[3]; MPoly *mpoly; + const int *material_indices; MLoop *mloop; MLoopUV *mloopuv; float uv_offset[2]; @@ -382,8 +383,7 @@ static void *do_multires_bake_thread(void *data_v) while ((tri_index = multires_bake_queue_next_tri(handle->queue)) >= 0) { const MLoopTri *lt = &data->mlooptri[tri_index]; - const MPoly *mp = &data->mpoly[lt->poly]; - const short mat_nr = mp->mat_nr; + const short mat_nr = data->material_indices == NULL ? 0 : data->material_indices[lt->poly]; const MLoopUV *mloopuv = data->mloopuv; if (multiresbake_test_break(bkr)) { @@ -545,6 +545,8 @@ static void do_multires_bake(MultiresBakeRender *bkr, handle->queue = &queue; handle->data.mpoly = mpoly; + handle->data.material_indices = CustomData_get_layer_named( + &dm->polyData, CD_PROP_INT32, "material_index"); handle->data.mvert = mvert; handle->data.vert_normals = vert_normals; handle->data.mloopuv = mloopuv; |