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 /intern/cycles/blender | |
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 'intern/cycles/blender')
-rw-r--r-- | intern/cycles/blender/mesh.cpp | 37 |
1 files changed, 33 insertions, 4 deletions
diff --git a/intern/cycles/blender/mesh.cpp b/intern/cycles/blender/mesh.cpp index 63913b7cd7f..2e2dfd6583b 100644 --- a/intern/cycles/blender/mesh.cpp +++ b/intern/cycles/blender/mesh.cpp @@ -1,6 +1,8 @@ /* SPDX-License-Identifier: Apache-2.0 * Copyright 2011-2022 Blender Foundation */ +#include <optional> + #include "blender/session.h" #include "blender/sync.h" #include "blender/util.h" @@ -879,6 +881,23 @@ static void attr_create_random_per_island(Scene *scene, /* Create Mesh */ +static std::optional<BL::IntAttribute> find_material_index_attribute(BL::Mesh b_mesh) +{ + for (BL::Attribute &b_attribute : b_mesh.attributes) { + if (b_attribute.domain() != BL::Attribute::domain_FACE) { + continue; + } + if (b_attribute.data_type() != BL::Attribute::data_type_INT) { + continue; + } + if (b_attribute.name() != "material_index") { + continue; + } + return BL::IntAttribute{b_attribute}; + } + return std::nullopt; +} + static void create_mesh(Scene *scene, Mesh *mesh, BL::Mesh &b_mesh, @@ -950,13 +969,22 @@ static void create_mesh(Scene *scene, } } + std::optional<BL::IntAttribute> material_indices = find_material_index_attribute(b_mesh); + auto get_material_index = [&](const int poly_index) -> int { + if (material_indices) { + return clamp(material_indices->data[poly_index].value(), 0, used_shaders.size() - 1); + } + return 0; + }; + /* create faces */ if (!subdivision) { for (BL::MeshLoopTriangle &t : b_mesh.loop_triangles) { - BL::MeshPolygon p = b_mesh.polygons[t.polygon_index()]; + const int poly_index = t.polygon_index(); + BL::MeshPolygon p = b_mesh.polygons[poly_index]; int3 vi = get_int3(t.vertices()); - int shader = clamp(p.material_index(), 0, used_shaders.size() - 1); + int shader = get_material_index(poly_index); bool smooth = p.use_smooth() || use_loop_normals; if (use_loop_normals) { @@ -977,9 +1005,10 @@ static void create_mesh(Scene *scene, else { vector<int> vi; - for (BL::MeshPolygon &p : b_mesh.polygons) { + for (int poly_index = 0; poly_index < numfaces; poly_index++) { + BL::MeshPolygon p = b_mesh.polygons[poly_index]; int n = p.loop_total(); - int shader = clamp(p.material_index(), 0, used_shaders.size() - 1); + int shader = get_material_index(poly_index); bool smooth = p.use_smooth() || use_loop_normals; vi.resize(n); |