From 82a46ea6f8829fc40205d0d3cabf4017eb738d9a Mon Sep 17 00:00:00 2001 From: Hans Goudey Date: Tue, 30 Aug 2022 11:08:27 -0500 Subject: Geometry Nodes: Use separate field context for each geometry type Using the same `GeometryComponentFieldContext` for all situations, even when only one geometry type is supported is misleading, and mixes too many different abstraction levels into code that could be simpler. With the attribute API moved out of geometry components recently, the "component" system is just getting in the way here. This commit adds specific field contexts for geometry types: meshes, curves, point clouds, and instances. There are also separate field input helper classes, to help reduce boilerplate for fields that only support specific geometry types. Another benefit of this change is that it separates geometry components from fields, which makes it easier to see the purpose of the two concepts, and how they relate. Because we want to be able to evaluate a field on just `CurvesGeometry` rather than the full `Curves` data-block, the generic "geometry context" had to be changed to avoid using `GeometryComponent`, since there is no corresponding geometry component type. The resulting void pointer is ugly, but only turns up in three places in practice. When Apple clang supports `std::variant`, that could be used instead. Differential Revision: https://developer.blender.org/D15519 --- .../nodes/node_geo_input_mesh_vertex_neighbors.cc | 56 ++++++++-------------- 1 file changed, 20 insertions(+), 36 deletions(-) (limited to 'source/blender/nodes/geometry/nodes/node_geo_input_mesh_vertex_neighbors.cc') diff --git a/source/blender/nodes/geometry/nodes/node_geo_input_mesh_vertex_neighbors.cc b/source/blender/nodes/geometry/nodes/node_geo_input_mesh_vertex_neighbors.cc index 62b3f9d0e92..244d454b8d1 100644 --- a/source/blender/nodes/geometry/nodes/node_geo_input_mesh_vertex_neighbors.cc +++ b/source/blender/nodes/geometry/nodes/node_geo_input_mesh_vertex_neighbors.cc @@ -20,41 +20,33 @@ static void node_declare(NodeDeclarationBuilder &b) .description(N_("Number of faces that contain the vertex")); } -static VArray construct_vertex_count_gvarray(const MeshComponent &component, - const eAttrDomain domain) +static VArray construct_vertex_count_gvarray(const Mesh &mesh, const eAttrDomain domain) { - const Mesh *mesh = component.get_for_read(); - if (mesh == nullptr) { - return {}; - } + const Span edges(mesh.medge, mesh.totedge); if (domain == ATTR_DOMAIN_POINT) { - Array vertices(mesh->totvert, 0); - for (const int i : IndexRange(mesh->totedge)) { - vertices[mesh->medge[i].v1]++; - vertices[mesh->medge[i].v2]++; + Array counts(mesh.totvert, 0); + for (const int i : edges.index_range()) { + counts[edges[i].v1]++; + counts[edges[i].v2]++; } - return VArray::ForContainer(std::move(vertices)); + return VArray::ForContainer(std::move(counts)); } return {}; } -class VertexCountFieldInput final : public GeometryFieldInput { +class VertexCountFieldInput final : public bke::MeshFieldInput { public: - VertexCountFieldInput() : GeometryFieldInput(CPPType::get(), "Vertex Count Field") + VertexCountFieldInput() : bke::MeshFieldInput(CPPType::get(), "Vertex Count Field") { category_ = Category::Generated; } - GVArray get_varray_for_context(const GeometryComponent &component, + GVArray get_varray_for_context(const Mesh &mesh, const eAttrDomain domain, IndexMask UNUSED(mask)) const final { - if (component.type() == GEO_COMPONENT_TYPE_MESH) { - const MeshComponent &mesh_component = static_cast(component); - return construct_vertex_count_gvarray(mesh_component, domain); - } - return {}; + return construct_vertex_count_gvarray(mesh, domain); } uint64_t hash() const override @@ -69,18 +61,14 @@ class VertexCountFieldInput final : public GeometryFieldInput { } }; -static VArray construct_face_count_gvarray(const MeshComponent &component, - const eAttrDomain domain) +static VArray construct_face_count_gvarray(const Mesh &mesh, const eAttrDomain domain) { - const Mesh *mesh = component.get_for_read(); - if (mesh == nullptr) { - return {}; - } + const Span loops(mesh.mloop, mesh.totloop); if (domain == ATTR_DOMAIN_POINT) { - Array vertices(mesh->totvert, 0); - for (const int i : IndexRange(mesh->totloop)) { - int vertex = mesh->mloop[i].v; + Array vertices(mesh.totvert, 0); + for (const int i : loops.index_range()) { + int vertex = loops[i].v; vertices[vertex]++; } return VArray::ForContainer(std::move(vertices)); @@ -88,22 +76,18 @@ static VArray construct_face_count_gvarray(const MeshComponent &component, return {}; } -class VertexFaceCountFieldInput final : public GeometryFieldInput { +class VertexFaceCountFieldInput final : public bke::MeshFieldInput { public: - VertexFaceCountFieldInput() : GeometryFieldInput(CPPType::get(), "Vertex Face Count Field") + VertexFaceCountFieldInput() : bke::MeshFieldInput(CPPType::get(), "Vertex Face Count Field") { category_ = Category::Generated; } - GVArray get_varray_for_context(const GeometryComponent &component, + GVArray get_varray_for_context(const Mesh &mesh, const eAttrDomain domain, IndexMask UNUSED(mask)) const final { - if (component.type() == GEO_COMPONENT_TYPE_MESH) { - const MeshComponent &mesh_component = static_cast(component); - return construct_face_count_gvarray(mesh_component, domain); - } - return {}; + return construct_face_count_gvarray(mesh, domain); } uint64_t hash() const override -- cgit v1.2.3 From 05952aa94d33eeb504fa63618ba35c2bcc8bd19b Mon Sep 17 00:00:00 2001 From: Hans Goudey Date: Mon, 5 Sep 2022 11:56:34 -0500 Subject: Mesh: Remove redundant custom data pointers For copy-on-write, we want to share attribute arrays between meshes where possible. Mutable pointers like `Mesh.mvert` make that difficult by making ownership vague. They also make code more complex by adding redundancy. The simplest solution is just removing them and retrieving layers from `CustomData` as needed. Similar changes have already been applied to curves and point clouds (e9f82d3dc7ee, 410a6efb747f). Removing use of the pointers generally makes code more obvious and more reusable. Mesh data is now accessed with a C++ API (`Mesh::edges()` or `Mesh::edges_for_write()`), and a C API (`BKE_mesh_edges(mesh)`). The CoW changes this commit makes possible are described in T95845 and T95842, and started in D14139 and D14140. The change also simplifies the ongoing mesh struct-of-array refactors from T95965. **RNA/Python Access Performance** Theoretically, accessing mesh elements with the RNA API may become slower, since the layer needs to be found on every random access. However, overhead is already high enough that this doesn't make a noticible differenc, and performance is actually improved in some cases. Random access can be up to 10% faster, but other situations might be a bit slower. Generally using `foreach_get/set` are the best way to improve performance. See the differential revision for more discussion about Python performance. Cycles has been updated to use raw pointers and the internal Blender mesh types, mostly because there is no sense in having this overhead when it's already compiled with Blender. In my tests this roughly halves the Cycles mesh creation time (0.19s to 0.10s for a 1 million face grid). Differential Revision: https://developer.blender.org/D15488 --- .../nodes/geometry/nodes/node_geo_input_mesh_vertex_neighbors.cc | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'source/blender/nodes/geometry/nodes/node_geo_input_mesh_vertex_neighbors.cc') diff --git a/source/blender/nodes/geometry/nodes/node_geo_input_mesh_vertex_neighbors.cc b/source/blender/nodes/geometry/nodes/node_geo_input_mesh_vertex_neighbors.cc index 244d454b8d1..ab44a6c8515 100644 --- a/source/blender/nodes/geometry/nodes/node_geo_input_mesh_vertex_neighbors.cc +++ b/source/blender/nodes/geometry/nodes/node_geo_input_mesh_vertex_neighbors.cc @@ -22,8 +22,7 @@ static void node_declare(NodeDeclarationBuilder &b) static VArray construct_vertex_count_gvarray(const Mesh &mesh, const eAttrDomain domain) { - const Span edges(mesh.medge, mesh.totedge); - + const Span edges = mesh.edges(); if (domain == ATTR_DOMAIN_POINT) { Array counts(mesh.totvert, 0); for (const int i : edges.index_range()) { @@ -63,8 +62,7 @@ class VertexCountFieldInput final : public bke::MeshFieldInput { static VArray construct_face_count_gvarray(const Mesh &mesh, const eAttrDomain domain) { - const Span loops(mesh.mloop, mesh.totloop); - + const Span loops = mesh.loops(); if (domain == ATTR_DOMAIN_POINT) { Array vertices(mesh.totvert, 0); for (const int i : loops.index_range()) { -- cgit v1.2.3