diff options
author | Hans Goudey <h.goudey@me.com> | 2022-01-13 23:37:58 +0300 |
---|---|---|
committer | Hans Goudey <h.goudey@me.com> | 2022-01-13 23:38:25 +0300 |
commit | cfa53e0fbeed7178c7876413e2010fd3347d7f72 (patch) | |
tree | f5cdc39134a3477614ae98699f90da3722161ed0 /source/blender/nodes | |
parent | 800fc17367480b82e07570d5234726aa61c38563 (diff) |
Refactor: Move normals out of MVert, lazy calculation
As described in T91186, this commit moves mesh vertex normals into a
contiguous array of float vectors in a custom data layer, how face
normals are currently stored.
The main interface is documented in `BKE_mesh.h`. Vertex and face
normals are now calculated on-demand and cached, retrieved with an
"ensure" function. Since the logical state of a mesh is now "has
normals when necessary", they can be retrieved from a `const` mesh.
The goal is to use on-demand calculation for all derived data, but
leave room for eager calculation for performance purposes (modifier
evaluation is threaded, but viewport data generation is not).
**Benefits**
This moves us closer to a SoA approach rather than the current AoS
paradigm. Accessing a contiguous `float3` is much more efficient than
retrieving data from a larger struct. The memory requirements for
accessing only normals or vertex locations are smaller, and at the
cost of more memory usage for just normals, they now don't have to
be converted between float and short, which also simplifies code
In the future, the remaining items can be removed from `MVert`,
leaving only `float3`, which has similar benefits (see T93602).
Removing the combination of derived and original data makes it
conceptually simpler to only calculate normals when necessary.
This is especially important now that we have more opportunities
for temporary meshes in geometry nodes.
**Performance**
In addition to the theoretical future performance improvements by
making `MVert == float3`, I've done some basic performance testing
on this patch directly. The data is fairly rough, but it gives an idea
about where things stand generally.
- Mesh line primitive 4m Verts: 1.16x faster (36 -> 31 ms),
showing that accessing just `MVert` is now more efficient.
- Spring Splash Screen: 1.03-1.06 -> 1.06-1.11 FPS, a very slight
change that at least shows there is no regression.
- Sprite Fright Snail Smoosh: 3.30-3.40 -> 3.42-3.50 FPS, a small
but observable speedup.
- Set Position Node with Scaled Normal: 1.36x faster (53 -> 39 ms),
shows that using normals in geometry nodes is faster.
- Normal Calculation 1.6m Vert Cube: 1.19x faster (25 -> 21 ms),
shows that calculating normals is slightly faster now.
- File Size of 1.6m Vert Cube: 1.03x smaller (214.7 -> 208.4 MB),
Normals are not saved in files, which can help with large meshes.
As for memory usage, it may be slightly more in some cases, but
I didn't observe any difference in the production files I tested.
**Tests**
Some modifiers and cycles test results need to be updated with this
commit, for two reasons:
- The subdivision surface modifier is not responsible for calculating
normals anymore. In master, the modifier creates different normals
than the result of the `Mesh` normal calculation, so this is a bug
fix.
- There are small differences in the results of some modifiers that
use normals because they are not converted to and from `short`
anymore.
**Future improvements**
- Remove `ModifierTypeInfo::dependsOnNormals`. Code in each modifier
already retrieves normals if they are needed anyway.
- Copy normals as part of a better CoW system for attributes.
- Make more areas use lazy instead of eager normal calculation.
- Remove `BKE_mesh_normals_tag_dirty` in more places since that is
now the default state of a new mesh.
- Possibly apply a similar change to derived face corner normals.
Differential Revision: https://developer.blender.org/D12770
Diffstat (limited to 'source/blender/nodes')
5 files changed, 11 insertions, 26 deletions
diff --git a/source/blender/nodes/geometry/nodes/node_geo_mesh_primitive_circle.cc b/source/blender/nodes/geometry/nodes/node_geo_mesh_primitive_circle.cc index b07d809a091..b3b11b2e0e9 100644 --- a/source/blender/nodes/geometry/nodes/node_geo_mesh_primitive_circle.cc +++ b/source/blender/nodes/geometry/nodes/node_geo_mesh_primitive_circle.cc @@ -138,12 +138,6 @@ static Mesh *create_circle_mesh(const float radius, copy_v3_v3(verts.last().co, float3(0)); } - /* Point all vertex normals in the up direction. */ - const short up_normal[3] = {0, 0, SHRT_MAX}; - for (MVert &vert : verts) { - copy_v3_v3_short(vert.no, up_normal); - } - /* Create outer edges. */ const short edge_flag = (fill_type == GEO_NODE_MESH_CIRCLE_FILL_NONE) ? ME_LOOSEEDGE : diff --git a/source/blender/nodes/geometry/nodes/node_geo_mesh_primitive_cone.cc b/source/blender/nodes/geometry/nodes/node_geo_mesh_primitive_cone.cc index 2aa9522cf56..e0923344421 100644 --- a/source/blender/nodes/geometry/nodes/node_geo_mesh_primitive_cone.cc +++ b/source/blender/nodes/geometry/nodes/node_geo_mesh_primitive_cone.cc @@ -676,8 +676,6 @@ static Mesh *create_vertex_mesh() /* Returns a mesh with a single vertex at the origin. */ Mesh *mesh = BKE_mesh_new_nomain(1, 0, 0, 0, 0); copy_v3_fl3(mesh->mvert[0].co, 0.0f, 0.0f, 0.0f); - const short up[3] = {0, 0, SHRT_MAX}; - copy_v3_v3_short(mesh->mvert[0].no, up); return mesh; } @@ -720,8 +718,6 @@ Mesh *create_cylinder_or_cone_mesh(const float radius_top, calculate_cone_uvs(mesh, config); calculate_selection_outputs(mesh, config, attribute_outputs); - BKE_mesh_normals_tag_dirty(mesh); - return mesh; } diff --git a/source/blender/nodes/geometry/nodes/node_geo_mesh_primitive_grid.cc b/source/blender/nodes/geometry/nodes/node_geo_mesh_primitive_grid.cc index 2c4b5df6030..ecb3c785212 100644 --- a/source/blender/nodes/geometry/nodes/node_geo_mesh_primitive_grid.cc +++ b/source/blender/nodes/geometry/nodes/node_geo_mesh_primitive_grid.cc @@ -89,14 +89,6 @@ Mesh *create_grid_mesh(const int verts_x, }); } - /* Point all vertex normals in the up direction. */ - { - const short up_normal[3] = {0, 0, SHRT_MAX}; - for (MVert &vert : verts) { - copy_v3_v3_short(vert.no, up_normal); - } - } - const int y_edges_start = 0; const int x_edges_start = verts_x * edges_y; const short edge_flag = (edges_x == 0 || edges_y == 0) ? ME_LOOSEEDGE : diff --git a/source/blender/nodes/geometry/nodes/node_geo_mesh_primitive_line.cc b/source/blender/nodes/geometry/nodes/node_geo_mesh_primitive_line.cc index 8a2b054ece0..691267bccb8 100644 --- a/source/blender/nodes/geometry/nodes/node_geo_mesh_primitive_line.cc +++ b/source/blender/nodes/geometry/nodes/node_geo_mesh_primitive_line.cc @@ -203,12 +203,8 @@ Mesh *create_line_mesh(const float3 start, const float3 delta, const int count) MutableSpan<MVert> verts{mesh->mvert, mesh->totvert}; MutableSpan<MEdge> edges{mesh->medge, mesh->totedge}; - short normal[3]; - normal_float_to_short_v3(normal, math::normalize(delta)); - for (const int i : verts.index_range()) { copy_v3_v3(verts[i].co, start + delta * i); - copy_v3_v3_short(verts[i].no, normal); } fill_edge_data(edges); diff --git a/source/blender/nodes/geometry/nodes/node_geo_mesh_primitive_uv_sphere.cc b/source/blender/nodes/geometry/nodes/node_geo_mesh_primitive_uv_sphere.cc index 41178d5c4e6..751cf917f6f 100644 --- a/source/blender/nodes/geometry/nodes/node_geo_mesh_primitive_uv_sphere.cc +++ b/source/blender/nodes/geometry/nodes/node_geo_mesh_primitive_uv_sphere.cc @@ -71,7 +71,12 @@ static int sphere_face_total(const int segments, const int rings) return quads + triangles; } +/** + * Also calculate vertex normals here, since the calculation is trivial, and it allows avoiding the + * calculation later, if it's necessary. The vertex normals are just the normalized positions. + */ static void calculate_sphere_vertex_data(MutableSpan<MVert> verts, + MutableSpan<float3> vert_normals, const float radius, const int segments, const int rings) @@ -80,7 +85,7 @@ static void calculate_sphere_vertex_data(MutableSpan<MVert> verts, const float delta_phi = (2.0f * M_PI) / segments; copy_v3_v3(verts[0].co, float3(0.0f, 0.0f, radius)); - normal_float_to_short_v3(verts[0].no, float3(0.0f, 0.0f, 1.0f)); + vert_normals.first() = float3(0.0f, 0.0f, 1.0f); int vert_index = 1; for (const int ring : IndexRange(1, rings - 1)) { @@ -92,13 +97,13 @@ static void calculate_sphere_vertex_data(MutableSpan<MVert> verts, const float x = sin_theta * std::cos(phi); const float y = sin_theta * std::sin(phi); copy_v3_v3(verts[vert_index].co, float3(x, y, z) * radius); - normal_float_to_short_v3(verts[vert_index].no, float3(x, y, z)); + vert_normals[vert_index] = float3(x, y, z); vert_index++; } } copy_v3_v3(verts.last().co, float3(0.0f, 0.0f, -radius)); - normal_float_to_short_v3(verts.last().no, float3(0.0f, 0.0f, -1.0f)); + vert_normals.last() = float3(0.0f, 0.0f, -1.0f); } static void calculate_sphere_edge_indices(MutableSpan<MEdge> edges, @@ -279,7 +284,9 @@ static Mesh *create_uv_sphere_mesh(const float radius, const int segments, const MutableSpan<MEdge> edges{mesh->medge, mesh->totedge}; MutableSpan<MPoly> polys{mesh->mpoly, mesh->totpoly}; - calculate_sphere_vertex_data(verts, radius, segments, rings); + MutableSpan vert_normals{(float3 *)BKE_mesh_vertex_normals_for_write(mesh), mesh->totvert}; + calculate_sphere_vertex_data(verts, vert_normals, radius, segments, rings); + BKE_mesh_vertex_normals_clear_dirty(mesh); calculate_sphere_edge_indices(edges, segments, rings); |