2023-05-31 16:19:06 +02:00
|
|
|
/* SPDX-FileCopyrightText: 2023 Blender Foundation
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
2018-06-20 10:03:50 +02:00
|
|
|
|
2019-02-17 22:08:12 +01:00
|
|
|
/** \file
|
|
|
|
* \ingroup bke
|
2018-06-20 10:03:50 +02:00
|
|
|
*
|
|
|
|
* Functions for iterating mesh features.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "DNA_mesh_types.h"
|
|
|
|
#include "DNA_meshdata_types.h"
|
|
|
|
|
|
|
|
#include "BKE_customdata.h"
|
2020-05-25 12:16:42 +02:00
|
|
|
#include "BKE_editmesh.h"
|
2023-07-07 14:19:52 +02:00
|
|
|
#include "BKE_editmesh_cache.hh"
|
2023-03-12 22:29:15 +01:00
|
|
|
#include "BKE_mesh.hh"
|
2023-08-02 22:14:18 +02:00
|
|
|
#include "BKE_mesh_iterators.hh"
|
2018-06-20 10:03:50 +02:00
|
|
|
|
2018-06-28 15:42:00 +02:00
|
|
|
#include "BLI_bitmap.h"
|
Cleanup: reduce amount of math-related includes
Using ClangBuildAnalyzer on the whole Blender build, it was pointing
out that BLI_math.h is the heaviest "header hub" (i.e. non tiny file
that is included a lot).
However, there's very little (actually zero) source files in Blender
that need "all the math" (base, colors, vectors, matrices,
quaternions, intersection, interpolation, statistics, solvers and
time). A common use case is source files needing just vectors, or
just vectors & matrices, or just colors etc. Actually, 181 files
were including the whole math thing without needing it at all.
This change removes BLI_math.h completely, and instead in all the
places that need it, includes BLI_math_vector.h or BLI_math_color.h
and so on.
Change from that:
- BLI_math_color.h was included 1399 times -> now 408 (took 114.0sec
to parse -> now 36.3sec)
- BLI_simd.h 1403 -> 418 (109.7sec -> 34.9sec).
Full rebuild of Blender (Apple M1, Xcode, RelWithDebInfo) is not
affected much (342sec -> 334sec). Most of benefit would be when
someone's changing BLI_simd.h or BLI_math_color.h or similar files,
that now there's 3x fewer files result in a recompile.
Pull Request #110944
2023-08-09 10:39:20 +02:00
|
|
|
#include "BLI_math_vector.h"
|
2018-06-28 15:42:00 +02:00
|
|
|
|
|
|
|
#include "MEM_guardedalloc.h"
|
2018-06-20 10:03:50 +02:00
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
/* General note on iterating verts/loops/edges/faces and end mode.
|
2022-02-02 10:35:32 +01:00
|
|
|
*
|
|
|
|
* The edit mesh pointer is set for both final and cage meshes in both cases when there are
|
|
|
|
* modifiers applied and not. This helps consistency of checks in the draw manager, where the
|
|
|
|
* existence of the edit mesh pointer does not depend on object configuration.
|
|
|
|
*
|
|
|
|
* For the iterating, however, we need to follow the `CD_ORIGINDEX` code paths when there are
|
|
|
|
* modifiers applied on the cage. In the code terms it means that the check for the edit mode code
|
|
|
|
* path needs to consist of both edit mesh and edit data checks. */
|
|
|
|
|
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
2022-01-13 21:37:58 +01:00
|
|
|
void BKE_mesh_foreach_mapped_vert(
|
2023-03-27 17:48:10 +02:00
|
|
|
const Mesh *mesh,
|
2023-07-27 04:04:18 +02:00
|
|
|
void (*func)(void *user_data, int index, const float co[3], const float no[3]),
|
|
|
|
void *user_data,
|
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
2022-01-13 21:37:58 +01:00
|
|
|
MeshForeachFlag flag)
|
2018-06-20 10:03:50 +02:00
|
|
|
{
|
2022-10-13 03:55:26 +02:00
|
|
|
if (mesh->edit_mesh != nullptr && mesh->runtime->edit_data != nullptr) {
|
2020-05-25 12:16:42 +02:00
|
|
|
BMEditMesh *em = mesh->edit_mesh;
|
|
|
|
BMesh *bm = em->bm;
|
|
|
|
BMIter iter;
|
|
|
|
BMVert *eve;
|
|
|
|
int i;
|
2023-07-10 19:49:54 +02:00
|
|
|
if (!mesh->runtime->edit_data->vertexCos.is_empty()) {
|
|
|
|
const blender::Span<blender::float3> positions = mesh->runtime->edit_data->vertexCos;
|
|
|
|
blender::Span<blender::float3> vert_normals;
|
2020-05-25 12:16:42 +02:00
|
|
|
if (flag & MESH_FOREACH_USE_NORMAL) {
|
2022-10-13 03:55:26 +02:00
|
|
|
BKE_editmesh_cache_ensure_vert_normals(em, mesh->runtime->edit_data);
|
2023-07-10 19:49:54 +02:00
|
|
|
vert_normals = mesh->runtime->edit_data->vertexNos;
|
2020-05-25 12:16:42 +02:00
|
|
|
}
|
|
|
|
BM_ITER_MESH_INDEX (eve, &iter, bm, BM_VERTS_OF_MESH, i) {
|
2023-07-10 19:49:54 +02:00
|
|
|
const float *no = (flag & MESH_FOREACH_USE_NORMAL) ? &vert_normals[i].x : nullptr;
|
2023-07-27 04:04:18 +02:00
|
|
|
func(user_data, i, positions[i], no);
|
2020-05-25 12:16:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
BM_ITER_MESH_INDEX (eve, &iter, bm, BM_VERTS_OF_MESH, i) {
|
2022-09-30 01:32:44 +02:00
|
|
|
const float *no = (flag & MESH_FOREACH_USE_NORMAL) ? eve->no : nullptr;
|
2023-07-27 04:04:18 +02:00
|
|
|
func(user_data, i, eve->co, no);
|
2018-10-11 01:31:11 +02:00
|
|
|
}
|
2018-06-20 10:03:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2023-06-14 17:59:32 +02:00
|
|
|
const blender::Span<blender::float3> positions = mesh->vert_positions();
|
2023-07-25 21:23:56 +02:00
|
|
|
const int *index = static_cast<const int *>(
|
|
|
|
CustomData_get_layer(&mesh->vert_data, CD_ORIGINDEX));
|
2023-03-15 19:00:40 +01:00
|
|
|
blender::Span<blender::float3> vert_normals;
|
|
|
|
if (flag & MESH_FOREACH_USE_NORMAL) {
|
|
|
|
vert_normals = mesh->vert_normals();
|
|
|
|
}
|
2020-05-25 12:16:42 +02:00
|
|
|
|
|
|
|
if (index) {
|
Mesh: Move positions to a generic attribute
**Changes**
As described in T93602, this patch removes all use of the `MVert`
struct, replacing it with a generic named attribute with the name
`"position"`, consistent with other geometry types.
Variable names have been changed from `verts` to `positions`, to align
with the attribute name and the more generic design (positions are not
vertices, they are just an attribute stored on the point domain).
This change is made possible by previous commits that moved all other
data out of `MVert` to runtime data or other generic attributes. What
remains is mostly a simple type change. Though, the type still shows up
859 times, so the patch is quite large.
One compromise is that now `CD_MASK_BAREMESH` now contains
`CD_PROP_FLOAT3`. With the general move towards generic attributes
over custom data types, we are removing use of these type masks anyway.
**Benefits**
The most obvious benefit is reduced memory usage and the benefits
that brings in memory-bound situations. `float3` is only 3 bytes, in
comparison to `MVert` which was 4. When there are millions of vertices
this starts to matter more.
The other benefits come from using a more generic type. Instead of
writing algorithms specifically for `MVert`, code can just use arrays
of vectors. This will allow eliminating many temporary arrays or
wrappers used to extract positions.
Many possible improvements aren't implemented in this patch, though
I did switch simplify or remove the process of creating temporary
position arrays in a few places.
The design clarity that "positions are just another attribute" brings
allows removing explicit copying of vertices in some procedural
operations-- they are just processed like most other attributes.
**Performance**
This touches so many areas that it's hard to benchmark exhaustively,
but I observed some areas as examples.
* The mesh line node with 4 million count was 1.5x (8ms to 12ms) faster.
* The Spring splash screen went from ~4.3 to ~4.5 fps.
* The subdivision surface modifier/node was slightly faster
RNA access through Python may be slightly slower, since now we need
a name lookup instead of just a custom data type lookup for each index.
**Future Improvements**
* Remove uses of "vert_coords" functions:
* `BKE_mesh_vert_coords_alloc`
* `BKE_mesh_vert_coords_get`
* `BKE_mesh_vert_coords_apply{_with_mat4}`
* Remove more hidden copying of positions
* General simplification now possible in many areas
* Convert more code to C++ to use `float3` instead of `float[3]`
* Currently `reinterpret_cast` is used for those C-API functions
Differential Revision: https://developer.blender.org/D15982
2023-01-10 06:10:43 +01:00
|
|
|
for (int i = 0; i < mesh->totvert; i++) {
|
2023-03-15 19:00:40 +01:00
|
|
|
const float *no = (flag & MESH_FOREACH_USE_NORMAL) ? &vert_normals[i].x : nullptr;
|
2020-05-25 12:16:42 +02:00
|
|
|
const int orig = *index++;
|
|
|
|
if (orig == ORIGINDEX_NONE) {
|
|
|
|
continue;
|
|
|
|
}
|
2023-07-27 04:04:18 +02:00
|
|
|
func(user_data, orig, positions[i], no);
|
2020-05-25 12:16:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
Mesh: Move positions to a generic attribute
**Changes**
As described in T93602, this patch removes all use of the `MVert`
struct, replacing it with a generic named attribute with the name
`"position"`, consistent with other geometry types.
Variable names have been changed from `verts` to `positions`, to align
with the attribute name and the more generic design (positions are not
vertices, they are just an attribute stored on the point domain).
This change is made possible by previous commits that moved all other
data out of `MVert` to runtime data or other generic attributes. What
remains is mostly a simple type change. Though, the type still shows up
859 times, so the patch is quite large.
One compromise is that now `CD_MASK_BAREMESH` now contains
`CD_PROP_FLOAT3`. With the general move towards generic attributes
over custom data types, we are removing use of these type masks anyway.
**Benefits**
The most obvious benefit is reduced memory usage and the benefits
that brings in memory-bound situations. `float3` is only 3 bytes, in
comparison to `MVert` which was 4. When there are millions of vertices
this starts to matter more.
The other benefits come from using a more generic type. Instead of
writing algorithms specifically for `MVert`, code can just use arrays
of vectors. This will allow eliminating many temporary arrays or
wrappers used to extract positions.
Many possible improvements aren't implemented in this patch, though
I did switch simplify or remove the process of creating temporary
position arrays in a few places.
The design clarity that "positions are just another attribute" brings
allows removing explicit copying of vertices in some procedural
operations-- they are just processed like most other attributes.
**Performance**
This touches so many areas that it's hard to benchmark exhaustively,
but I observed some areas as examples.
* The mesh line node with 4 million count was 1.5x (8ms to 12ms) faster.
* The Spring splash screen went from ~4.3 to ~4.5 fps.
* The subdivision surface modifier/node was slightly faster
RNA access through Python may be slightly slower, since now we need
a name lookup instead of just a custom data type lookup for each index.
**Future Improvements**
* Remove uses of "vert_coords" functions:
* `BKE_mesh_vert_coords_alloc`
* `BKE_mesh_vert_coords_get`
* `BKE_mesh_vert_coords_apply{_with_mat4}`
* Remove more hidden copying of positions
* General simplification now possible in many areas
* Convert more code to C++ to use `float3` instead of `float[3]`
* Currently `reinterpret_cast` is used for those C-API functions
Differential Revision: https://developer.blender.org/D15982
2023-01-10 06:10:43 +01:00
|
|
|
for (int i = 0; i < mesh->totvert; i++) {
|
2023-03-15 19:00:40 +01:00
|
|
|
const float *no = (flag & MESH_FOREACH_USE_NORMAL) ? &vert_normals[i].x : nullptr;
|
2023-07-27 04:04:18 +02:00
|
|
|
func(user_data, i, positions[i], no);
|
2020-05-25 12:16:42 +02:00
|
|
|
}
|
2018-06-20 10:03:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BKE_mesh_foreach_mapped_edge(
|
|
|
|
Mesh *mesh,
|
2021-07-23 16:15:18 +02:00
|
|
|
const int tot_edges,
|
2023-07-27 04:04:18 +02:00
|
|
|
void (*func)(void *user_data, int index, const float v0co[3], const float v1co[3]),
|
|
|
|
void *user_data)
|
2018-06-20 10:03:50 +02:00
|
|
|
{
|
2022-10-13 03:55:26 +02:00
|
|
|
if (mesh->edit_mesh != nullptr && mesh->runtime->edit_data) {
|
2020-05-25 12:16:42 +02:00
|
|
|
BMEditMesh *em = mesh->edit_mesh;
|
|
|
|
BMesh *bm = em->bm;
|
|
|
|
BMIter iter;
|
|
|
|
BMEdge *eed;
|
|
|
|
int i;
|
2023-07-10 19:49:54 +02:00
|
|
|
if (!mesh->runtime->edit_data->vertexCos.is_empty()) {
|
|
|
|
const blender::Span<blender::float3> positions = mesh->runtime->edit_data->vertexCos;
|
2020-05-25 12:16:42 +02:00
|
|
|
BM_mesh_elem_index_ensure(bm, BM_VERT);
|
|
|
|
BM_ITER_MESH_INDEX (eed, &iter, bm, BM_EDGES_OF_MESH, i) {
|
2023-07-27 04:04:18 +02:00
|
|
|
func(user_data,
|
2020-05-25 12:16:42 +02:00
|
|
|
i,
|
2023-07-10 19:49:54 +02:00
|
|
|
positions[BM_elem_index_get(eed->v1)],
|
|
|
|
positions[BM_elem_index_get(eed->v2)]);
|
2020-05-25 12:16:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
BM_ITER_MESH_INDEX (eed, &iter, bm, BM_EDGES_OF_MESH, i) {
|
2023-07-27 04:04:18 +02:00
|
|
|
func(user_data, i, eed->v1->co, eed->v2->co);
|
2018-10-11 01:31:11 +02:00
|
|
|
}
|
2018-06-20 10:03:50 +02:00
|
|
|
}
|
2018-10-11 01:31:11 +02:00
|
|
|
}
|
|
|
|
else {
|
2023-06-14 17:59:32 +02:00
|
|
|
const blender::Span<blender::float3> positions = mesh->vert_positions();
|
Mesh: Move edges to a generic attribute
Implements #95966, as the final step of #95965.
This commit changes the storage of mesh edge vertex indices from the
`MEdge` type to the generic `int2` attribute type. This follows the
general design for geometry and the attribute system, where the data
storage type and the usage semantics are separated.
The main benefit of the change is reduced memory usage-- the
requirements of storing mesh edges is reduced by 1/3. For example,
this saves 8MB on a 1 million vertex grid. This also gives performance
benefits to any memory-bound mesh processing algorithm that uses edges.
Another benefit is that all of the edge's vertex indices are
contiguous. In a few cases, it's helpful to process all of them as
`Span<int>` rather than `Span<int2>`. Similarly, the type is more
likely to match a generic format used by a library, or code that
shouldn't know about specific Blender `Mesh` types.
Various Notes:
- The `.edge_verts` name is used to reflect a mapping between domains,
similar to `.corner_verts`, etc. The period means that it the data
shouldn't change arbitrarily by the user or procedural operations.
- `edge[0]` is now used instead of `edge.v1`
- Signed integers are used instead of unsigned to reduce the mixing
of signed-ness, which can be error prone.
- All of the previously used core mesh data types (`MVert`, `MEdge`,
`MLoop`, `MPoly` are now deprecated. Only generic types are used).
- The `vec2i` DNA type is used in the few C files where necessary.
Pull Request: https://projects.blender.org/blender/blender/pulls/106638
2023-04-17 13:47:41 +02:00
|
|
|
const blender::Span<blender::int2> edges = mesh->edges();
|
2023-07-25 21:23:56 +02:00
|
|
|
const int *index = static_cast<const int *>(
|
|
|
|
CustomData_get_layer(&mesh->edge_data, CD_ORIGINDEX));
|
2020-05-25 12:16:42 +02:00
|
|
|
|
|
|
|
if (index) {
|
2023-02-23 16:39:51 +01:00
|
|
|
for (const int i : edges.index_range()) {
|
|
|
|
|
2020-05-25 12:16:42 +02:00
|
|
|
const int orig = *index++;
|
|
|
|
if (orig == ORIGINDEX_NONE) {
|
|
|
|
continue;
|
|
|
|
}
|
2023-07-27 04:04:18 +02:00
|
|
|
func(user_data, orig, positions[edges[i][0]], positions[edges[i][1]]);
|
2020-05-25 12:16:42 +02:00
|
|
|
}
|
|
|
|
}
|
2021-07-23 16:15:18 +02:00
|
|
|
else if (mesh->totedge == tot_edges) {
|
2023-02-23 16:39:51 +01:00
|
|
|
for (const int i : edges.index_range()) {
|
2023-07-27 04:04:18 +02:00
|
|
|
func(user_data, i, positions[edges[i][0]], positions[edges[i][1]]);
|
2020-05-25 12:16:42 +02:00
|
|
|
}
|
2018-10-11 01:31:11 +02:00
|
|
|
}
|
2018-06-20 10:03:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BKE_mesh_foreach_mapped_loop(Mesh *mesh,
|
2023-07-27 04:04:18 +02:00
|
|
|
void (*func)(void *user_data,
|
2018-06-20 10:03:50 +02:00
|
|
|
int vertex_index,
|
|
|
|
int face_index,
|
|
|
|
const float co[3],
|
|
|
|
const float no[3]),
|
2023-07-27 04:04:18 +02:00
|
|
|
void *user_data,
|
2018-06-20 10:03:50 +02:00
|
|
|
MeshForeachFlag flag)
|
|
|
|
{
|
2020-05-25 12:16:42 +02:00
|
|
|
|
2022-10-03 01:24:08 +02:00
|
|
|
/* We can't use `dm->getLoopDataLayout(dm)` here,
|
|
|
|
* we want to always access `dm->loopData`, `EditDerivedBMesh` would
|
|
|
|
* return loop data from BMesh itself. */
|
2022-10-13 03:55:26 +02:00
|
|
|
if (mesh->edit_mesh != nullptr && mesh->runtime->edit_data) {
|
2020-05-25 12:16:42 +02:00
|
|
|
BMEditMesh *em = mesh->edit_mesh;
|
|
|
|
BMesh *bm = em->bm;
|
|
|
|
BMIter iter;
|
|
|
|
BMFace *efa;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-10 19:49:54 +02:00
|
|
|
const blender::Span<blender::float3> positions = mesh->runtime->edit_data->vertexCos;
|
2020-05-25 12:16:42 +02:00
|
|
|
|
|
|
|
/* XXX: investigate using EditMesh data. */
|
2023-04-28 19:34:15 +02:00
|
|
|
blender::Span<blender::float3> corner_normals;
|
|
|
|
if (flag & MESH_FOREACH_USE_NORMAL) {
|
|
|
|
corner_normals = {
|
2023-07-25 21:15:52 +02:00
|
|
|
static_cast<const blender::float3 *>(CustomData_get_layer(&mesh->loop_data, CD_NORMAL)),
|
2023-04-28 19:34:15 +02:00
|
|
|
mesh->totloop};
|
|
|
|
}
|
2020-05-25 12:16:42 +02:00
|
|
|
|
|
|
|
int f_idx;
|
|
|
|
|
|
|
|
BM_mesh_elem_index_ensure(bm, BM_VERT);
|
|
|
|
|
|
|
|
BM_ITER_MESH_INDEX (efa, &iter, bm, BM_FACES_OF_MESH, f_idx) {
|
|
|
|
BMLoop *l_iter, *l_first;
|
|
|
|
|
|
|
|
l_iter = l_first = BM_FACE_FIRST_LOOP(efa);
|
|
|
|
do {
|
|
|
|
const BMVert *eve = l_iter->v;
|
|
|
|
const int v_idx = BM_elem_index_get(eve);
|
2023-07-27 04:04:18 +02:00
|
|
|
func(user_data,
|
2023-07-10 19:49:54 +02:00
|
|
|
v_idx,
|
|
|
|
f_idx,
|
|
|
|
positions.is_empty() ? positions[v_idx] : blender::float3(eve->co),
|
|
|
|
corner_normals.is_empty() ? nullptr : &corner_normals[BM_elem_index_get(l_iter)].x);
|
2020-05-25 12:16:42 +02:00
|
|
|
} while ((l_iter = l_iter->next) != l_first);
|
2018-10-11 01:31:11 +02:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2018-10-11 01:31:11 +02:00
|
|
|
else {
|
2023-04-28 19:34:15 +02:00
|
|
|
blender::Span<blender::float3> corner_normals;
|
|
|
|
if (flag & MESH_FOREACH_USE_NORMAL) {
|
|
|
|
corner_normals = {
|
2023-07-25 21:15:52 +02:00
|
|
|
static_cast<const blender::float3 *>(CustomData_get_layer(&mesh->loop_data, CD_NORMAL)),
|
2023-04-28 19:34:15 +02:00
|
|
|
mesh->totloop};
|
|
|
|
}
|
2020-05-25 12:16:42 +02:00
|
|
|
|
2023-06-14 17:59:32 +02:00
|
|
|
const blender::Span<blender::float3> positions = mesh->vert_positions();
|
2023-07-24 22:06:55 +02:00
|
|
|
const blender::OffsetIndices faces = mesh->faces();
|
Mesh: Replace MLoop struct with generic attributes
Implements #102359.
Split the `MLoop` struct into two separate integer arrays called
`corner_verts` and `corner_edges`, referring to the vertex each corner
is attached to and the next edge around the face at each corner. These
arrays can be sliced to give access to the edges or vertices in a face.
Then they are often referred to as "poly_verts" or "poly_edges".
The main benefits are halving the necessary memory bandwidth when only
one array is used and simplifications from using regular integer indices
instead of a special-purpose struct.
The commit also starts a renaming from "loop" to "corner" in mesh code.
Like the other mesh struct of array refactors, forward compatibility is
kept by writing files with the older format. This will be done until 4.0
to ease the transition process.
Looking at a small portion of the patch should give a good impression
for the rest of the changes. I tried to make the changes as small as
possible so it's easy to tell the correctness from the diff. Though I
found Blender developers have been very inventive over the last decade
when finding different ways to loop over the corners in a face.
For performance, nearly every piece of code that deals with `Mesh` is
slightly impacted. Any algorithm that is memory bottle-necked should
see an improvement. For example, here is a comparison of interpolating
a vertex float attribute to face corners (Ryzen 3700x):
**Before** (Average: 3.7 ms, Min: 3.4 ms)
```
threading::parallel_for(loops.index_range(), 4096, [&](IndexRange range) {
for (const int64_t i : range) {
dst[i] = src[loops[i].v];
}
});
```
**After** (Average: 2.9 ms, Min: 2.6 ms)
```
array_utils::gather(src, corner_verts, dst);
```
That's an improvement of 28% to the average timings, and it's also a
simplification, since an index-based routine can be used instead.
For more examples using the new arrays, see the design task.
Pull Request: https://projects.blender.org/blender/blender/pulls/104424
2023-03-20 15:55:13 +01:00
|
|
|
const blender::Span<int> corner_verts = mesh->corner_verts();
|
2022-09-30 01:32:44 +02:00
|
|
|
const int *v_index = static_cast<const int *>(
|
2023-07-25 21:15:52 +02:00
|
|
|
CustomData_get_layer(&mesh->vert_data, CD_ORIGINDEX));
|
2022-09-30 01:32:44 +02:00
|
|
|
const int *f_index = static_cast<const int *>(
|
2023-07-25 21:15:52 +02:00
|
|
|
CustomData_get_layer(&mesh->face_data, CD_ORIGINDEX));
|
2020-05-25 12:16:42 +02:00
|
|
|
|
|
|
|
if (v_index || f_index) {
|
2023-07-24 22:06:55 +02:00
|
|
|
for (const int face_i : faces.index_range()) {
|
|
|
|
for (const int corner : faces[face_i]) {
|
2023-04-28 19:34:15 +02:00
|
|
|
const int vert = corner_verts[corner];
|
Mesh: Replace MLoop struct with generic attributes
Implements #102359.
Split the `MLoop` struct into two separate integer arrays called
`corner_verts` and `corner_edges`, referring to the vertex each corner
is attached to and the next edge around the face at each corner. These
arrays can be sliced to give access to the edges or vertices in a face.
Then they are often referred to as "poly_verts" or "poly_edges".
The main benefits are halving the necessary memory bandwidth when only
one array is used and simplifications from using regular integer indices
instead of a special-purpose struct.
The commit also starts a renaming from "loop" to "corner" in mesh code.
Like the other mesh struct of array refactors, forward compatibility is
kept by writing files with the older format. This will be done until 4.0
to ease the transition process.
Looking at a small portion of the patch should give a good impression
for the rest of the changes. I tried to make the changes as small as
possible so it's easy to tell the correctness from the diff. Though I
found Blender developers have been very inventive over the last decade
when finding different ways to loop over the corners in a face.
For performance, nearly every piece of code that deals with `Mesh` is
slightly impacted. Any algorithm that is memory bottle-necked should
see an improvement. For example, here is a comparison of interpolating
a vertex float attribute to face corners (Ryzen 3700x):
**Before** (Average: 3.7 ms, Min: 3.4 ms)
```
threading::parallel_for(loops.index_range(), 4096, [&](IndexRange range) {
for (const int64_t i : range) {
dst[i] = src[loops[i].v];
}
});
```
**After** (Average: 2.9 ms, Min: 2.6 ms)
```
array_utils::gather(src, corner_verts, dst);
```
That's an improvement of 28% to the average timings, and it's also a
simplification, since an index-based routine can be used instead.
For more examples using the new arrays, see the design task.
Pull Request: https://projects.blender.org/blender/blender/pulls/104424
2023-03-20 15:55:13 +01:00
|
|
|
const int v_idx = v_index ? v_index[vert] : vert;
|
2023-07-24 22:06:55 +02:00
|
|
|
const int f_idx = f_index ? f_index[face_i] : face_i;
|
2023-04-28 19:34:15 +02:00
|
|
|
const float *no = corner_normals.is_empty() ? nullptr : &corner_normals[corner].x;
|
2020-05-25 12:16:42 +02:00
|
|
|
if (ELEM(ORIGINDEX_NONE, v_idx, f_idx)) {
|
|
|
|
continue;
|
|
|
|
}
|
2023-07-27 04:04:18 +02:00
|
|
|
func(user_data, v_idx, f_idx, positions[vert], no);
|
2020-05-25 12:16:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2023-07-24 22:06:55 +02:00
|
|
|
for (const int face_i : faces.index_range()) {
|
|
|
|
for (const int corner : faces[face_i]) {
|
2023-04-28 19:34:15 +02:00
|
|
|
const int vert = corner_verts[corner];
|
|
|
|
const float *no = corner_normals.is_empty() ? nullptr : &corner_normals[corner].x;
|
2023-07-27 04:04:18 +02:00
|
|
|
func(user_data, vert, face_i, positions[vert], no);
|
2020-05-25 12:16:42 +02:00
|
|
|
}
|
2018-06-20 10:03:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BKE_mesh_foreach_mapped_face_center(
|
|
|
|
Mesh *mesh,
|
2023-07-27 04:04:18 +02:00
|
|
|
void (*func)(void *user_data, int index, const float cent[3], const float no[3]),
|
|
|
|
void *user_data,
|
2018-06-20 10:03:50 +02:00
|
|
|
MeshForeachFlag flag)
|
|
|
|
{
|
2023-03-12 22:29:15 +01:00
|
|
|
using namespace blender;
|
2022-10-13 03:55:26 +02:00
|
|
|
if (mesh->edit_mesh != nullptr && mesh->runtime->edit_data != nullptr) {
|
2020-05-25 12:16:42 +02:00
|
|
|
BMEditMesh *em = mesh->edit_mesh;
|
|
|
|
BMesh *bm = em->bm;
|
2023-07-24 22:06:55 +02:00
|
|
|
blender::Span<blender::float3> face_centers;
|
|
|
|
blender::Span<blender::float3> face_normals;
|
2020-05-25 12:16:42 +02:00
|
|
|
BMFace *efa;
|
|
|
|
BMIter iter;
|
|
|
|
int i;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
BKE_editmesh_cache_ensure_face_centers(em, mesh->runtime->edit_data);
|
|
|
|
face_centers = mesh->runtime->edit_data->faceCos; /* always set */
|
2020-05-25 12:16:42 +02:00
|
|
|
|
|
|
|
if (flag & MESH_FOREACH_USE_NORMAL) {
|
2023-07-24 22:06:55 +02:00
|
|
|
BKE_editmesh_cache_ensure_face_normals(em, mesh->runtime->edit_data);
|
|
|
|
face_normals = mesh->runtime->edit_data->faceNos; /* maybe nullptr */
|
2020-05-25 12:16:42 +02:00
|
|
|
}
|
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
if (!face_normals.is_empty()) {
|
2020-05-25 12:16:42 +02:00
|
|
|
BM_ITER_MESH_INDEX (efa, &iter, bm, BM_FACES_OF_MESH, i) {
|
2023-07-24 22:06:55 +02:00
|
|
|
const float *no = face_normals[i];
|
2023-07-27 04:04:18 +02:00
|
|
|
func(user_data, i, face_centers[i], no);
|
2018-10-11 01:31:11 +02:00
|
|
|
}
|
2020-05-25 12:16:42 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
BM_ITER_MESH_INDEX (efa, &iter, bm, BM_FACES_OF_MESH, i) {
|
2022-09-30 01:32:44 +02:00
|
|
|
const float *no = (flag & MESH_FOREACH_USE_NORMAL) ? efa->no : nullptr;
|
2023-07-27 04:04:18 +02:00
|
|
|
func(user_data, i, face_centers[i], no);
|
2018-10-11 01:31:11 +02:00
|
|
|
}
|
2018-06-20 10:03:50 +02:00
|
|
|
}
|
2018-10-11 01:31:11 +02:00
|
|
|
}
|
|
|
|
else {
|
2023-03-12 22:29:15 +01:00
|
|
|
const blender::Span<float3> positions = mesh->vert_positions();
|
2023-07-24 22:06:55 +02:00
|
|
|
const blender::OffsetIndices faces = mesh->faces();
|
Mesh: Replace MLoop struct with generic attributes
Implements #102359.
Split the `MLoop` struct into two separate integer arrays called
`corner_verts` and `corner_edges`, referring to the vertex each corner
is attached to and the next edge around the face at each corner. These
arrays can be sliced to give access to the edges or vertices in a face.
Then they are often referred to as "poly_verts" or "poly_edges".
The main benefits are halving the necessary memory bandwidth when only
one array is used and simplifications from using regular integer indices
instead of a special-purpose struct.
The commit also starts a renaming from "loop" to "corner" in mesh code.
Like the other mesh struct of array refactors, forward compatibility is
kept by writing files with the older format. This will be done until 4.0
to ease the transition process.
Looking at a small portion of the patch should give a good impression
for the rest of the changes. I tried to make the changes as small as
possible so it's easy to tell the correctness from the diff. Though I
found Blender developers have been very inventive over the last decade
when finding different ways to loop over the corners in a face.
For performance, nearly every piece of code that deals with `Mesh` is
slightly impacted. Any algorithm that is memory bottle-necked should
see an improvement. For example, here is a comparison of interpolating
a vertex float attribute to face corners (Ryzen 3700x):
**Before** (Average: 3.7 ms, Min: 3.4 ms)
```
threading::parallel_for(loops.index_range(), 4096, [&](IndexRange range) {
for (const int64_t i : range) {
dst[i] = src[loops[i].v];
}
});
```
**After** (Average: 2.9 ms, Min: 2.6 ms)
```
array_utils::gather(src, corner_verts, dst);
```
That's an improvement of 28% to the average timings, and it's also a
simplification, since an index-based routine can be used instead.
For more examples using the new arrays, see the design task.
Pull Request: https://projects.blender.org/blender/blender/pulls/104424
2023-03-20 15:55:13 +01:00
|
|
|
const blender::Span<int> corner_verts = mesh->corner_verts();
|
2023-07-25 21:23:56 +02:00
|
|
|
const int *index = static_cast<const int *>(
|
|
|
|
CustomData_get_layer(&mesh->face_data, CD_ORIGINDEX));
|
2020-05-25 12:16:42 +02:00
|
|
|
|
|
|
|
if (index) {
|
2023-07-24 22:06:55 +02:00
|
|
|
for (const int i : faces.index_range()) {
|
2020-05-25 12:16:42 +02:00
|
|
|
const int orig = *index++;
|
|
|
|
if (orig == ORIGINDEX_NONE) {
|
|
|
|
continue;
|
|
|
|
}
|
2023-07-24 22:06:55 +02:00
|
|
|
const Span<int> face_verts = corner_verts.slice(faces[i]);
|
|
|
|
const float3 center = bke::mesh::face_center_calc(positions, face_verts);
|
2020-05-25 12:16:42 +02:00
|
|
|
if (flag & MESH_FOREACH_USE_NORMAL) {
|
2023-07-24 22:06:55 +02:00
|
|
|
const float3 normal = bke::mesh::face_normal_calc(positions, face_verts);
|
2023-07-27 04:04:18 +02:00
|
|
|
func(user_data, orig, center, normal);
|
2023-03-12 22:29:15 +01:00
|
|
|
}
|
|
|
|
else {
|
2023-07-27 04:04:18 +02:00
|
|
|
func(user_data, orig, center, nullptr);
|
2020-05-25 12:16:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2023-07-24 22:06:55 +02:00
|
|
|
for (const int i : faces.index_range()) {
|
|
|
|
const Span<int> face_verts = corner_verts.slice(faces[i]);
|
|
|
|
const float3 center = bke::mesh::face_center_calc(positions, face_verts);
|
2020-05-25 12:16:42 +02:00
|
|
|
if (flag & MESH_FOREACH_USE_NORMAL) {
|
2023-07-24 22:06:55 +02:00
|
|
|
const float3 normal = bke::mesh::face_normal_calc(positions, face_verts);
|
2023-07-27 04:04:18 +02:00
|
|
|
func(user_data, i, center, normal);
|
2023-03-12 22:29:15 +01:00
|
|
|
}
|
|
|
|
else {
|
2023-07-27 04:04:18 +02:00
|
|
|
func(user_data, i, center, nullptr);
|
2020-05-25 12:16:42 +02:00
|
|
|
}
|
2018-10-11 01:31:11 +02:00
|
|
|
}
|
2018-06-20 10:03:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-06-28 15:42:00 +02:00
|
|
|
|
2019-07-07 18:58:11 +02:00
|
|
|
void BKE_mesh_foreach_mapped_subdiv_face_center(
|
|
|
|
Mesh *mesh,
|
2023-07-27 04:04:18 +02:00
|
|
|
void (*func)(void *user_data, int index, const float cent[3], const float no[3]),
|
|
|
|
void *user_data,
|
2019-07-07 18:58:11 +02:00
|
|
|
MeshForeachFlag flag)
|
|
|
|
{
|
2023-06-14 17:59:32 +02:00
|
|
|
const blender::Span<blender::float3> positions = mesh->vert_positions();
|
2023-07-24 22:06:55 +02:00
|
|
|
const blender::OffsetIndices faces = mesh->faces();
|
Mesh: Replace MLoop struct with generic attributes
Implements #102359.
Split the `MLoop` struct into two separate integer arrays called
`corner_verts` and `corner_edges`, referring to the vertex each corner
is attached to and the next edge around the face at each corner. These
arrays can be sliced to give access to the edges or vertices in a face.
Then they are often referred to as "poly_verts" or "poly_edges".
The main benefits are halving the necessary memory bandwidth when only
one array is used and simplifications from using regular integer indices
instead of a special-purpose struct.
The commit also starts a renaming from "loop" to "corner" in mesh code.
Like the other mesh struct of array refactors, forward compatibility is
kept by writing files with the older format. This will be done until 4.0
to ease the transition process.
Looking at a small portion of the patch should give a good impression
for the rest of the changes. I tried to make the changes as small as
possible so it's easy to tell the correctness from the diff. Though I
found Blender developers have been very inventive over the last decade
when finding different ways to loop over the corners in a face.
For performance, nearly every piece of code that deals with `Mesh` is
slightly impacted. Any algorithm that is memory bottle-necked should
see an improvement. For example, here is a comparison of interpolating
a vertex float attribute to face corners (Ryzen 3700x):
**Before** (Average: 3.7 ms, Min: 3.4 ms)
```
threading::parallel_for(loops.index_range(), 4096, [&](IndexRange range) {
for (const int64_t i : range) {
dst[i] = src[loops[i].v];
}
});
```
**After** (Average: 2.9 ms, Min: 2.6 ms)
```
array_utils::gather(src, corner_verts, dst);
```
That's an improvement of 28% to the average timings, and it's also a
simplification, since an index-based routine can be used instead.
For more examples using the new arrays, see the design task.
Pull Request: https://projects.blender.org/blender/blender/pulls/104424
2023-03-20 15:55:13 +01:00
|
|
|
const blender::Span<int> corner_verts = mesh->corner_verts();
|
2023-03-15 19:00:40 +01:00
|
|
|
blender::Span<blender::float3> vert_normals;
|
|
|
|
if (flag & MESH_FOREACH_USE_NORMAL) {
|
|
|
|
vert_normals = mesh->vert_normals();
|
|
|
|
}
|
2023-07-25 21:23:56 +02:00
|
|
|
const int *index = static_cast<const int *>(
|
|
|
|
CustomData_get_layer(&mesh->face_data, CD_ORIGINDEX));
|
2023-02-20 11:51:16 +01:00
|
|
|
const blender::BitSpan facedot_tags = mesh->runtime->subsurf_face_dot_tags;
|
2019-07-07 18:58:11 +02:00
|
|
|
|
|
|
|
if (index) {
|
2023-07-24 22:06:55 +02:00
|
|
|
for (const int i : faces.index_range()) {
|
2019-07-07 18:58:11 +02:00
|
|
|
const int orig = *index++;
|
|
|
|
if (orig == ORIGINDEX_NONE) {
|
|
|
|
continue;
|
|
|
|
}
|
2023-07-24 22:06:55 +02:00
|
|
|
for (const int vert : corner_verts.slice(faces[i])) {
|
Mesh: Replace MLoop struct with generic attributes
Implements #102359.
Split the `MLoop` struct into two separate integer arrays called
`corner_verts` and `corner_edges`, referring to the vertex each corner
is attached to and the next edge around the face at each corner. These
arrays can be sliced to give access to the edges or vertices in a face.
Then they are often referred to as "poly_verts" or "poly_edges".
The main benefits are halving the necessary memory bandwidth when only
one array is used and simplifications from using regular integer indices
instead of a special-purpose struct.
The commit also starts a renaming from "loop" to "corner" in mesh code.
Like the other mesh struct of array refactors, forward compatibility is
kept by writing files with the older format. This will be done until 4.0
to ease the transition process.
Looking at a small portion of the patch should give a good impression
for the rest of the changes. I tried to make the changes as small as
possible so it's easy to tell the correctness from the diff. Though I
found Blender developers have been very inventive over the last decade
when finding different ways to loop over the corners in a face.
For performance, nearly every piece of code that deals with `Mesh` is
slightly impacted. Any algorithm that is memory bottle-necked should
see an improvement. For example, here is a comparison of interpolating
a vertex float attribute to face corners (Ryzen 3700x):
**Before** (Average: 3.7 ms, Min: 3.4 ms)
```
threading::parallel_for(loops.index_range(), 4096, [&](IndexRange range) {
for (const int64_t i : range) {
dst[i] = src[loops[i].v];
}
});
```
**After** (Average: 2.9 ms, Min: 2.6 ms)
```
array_utils::gather(src, corner_verts, dst);
```
That's an improvement of 28% to the average timings, and it's also a
simplification, since an index-based routine can be used instead.
For more examples using the new arrays, see the design task.
Pull Request: https://projects.blender.org/blender/blender/pulls/104424
2023-03-20 15:55:13 +01:00
|
|
|
if (facedot_tags[vert]) {
|
2023-07-27 04:04:18 +02:00
|
|
|
func(user_data,
|
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
2022-01-13 21:37:58 +01:00
|
|
|
orig,
|
Mesh: Replace MLoop struct with generic attributes
Implements #102359.
Split the `MLoop` struct into two separate integer arrays called
`corner_verts` and `corner_edges`, referring to the vertex each corner
is attached to and the next edge around the face at each corner. These
arrays can be sliced to give access to the edges or vertices in a face.
Then they are often referred to as "poly_verts" or "poly_edges".
The main benefits are halving the necessary memory bandwidth when only
one array is used and simplifications from using regular integer indices
instead of a special-purpose struct.
The commit also starts a renaming from "loop" to "corner" in mesh code.
Like the other mesh struct of array refactors, forward compatibility is
kept by writing files with the older format. This will be done until 4.0
to ease the transition process.
Looking at a small portion of the patch should give a good impression
for the rest of the changes. I tried to make the changes as small as
possible so it's easy to tell the correctness from the diff. Though I
found Blender developers have been very inventive over the last decade
when finding different ways to loop over the corners in a face.
For performance, nearly every piece of code that deals with `Mesh` is
slightly impacted. Any algorithm that is memory bottle-necked should
see an improvement. For example, here is a comparison of interpolating
a vertex float attribute to face corners (Ryzen 3700x):
**Before** (Average: 3.7 ms, Min: 3.4 ms)
```
threading::parallel_for(loops.index_range(), 4096, [&](IndexRange range) {
for (const int64_t i : range) {
dst[i] = src[loops[i].v];
}
});
```
**After** (Average: 2.9 ms, Min: 2.6 ms)
```
array_utils::gather(src, corner_verts, dst);
```
That's an improvement of 28% to the average timings, and it's also a
simplification, since an index-based routine can be used instead.
For more examples using the new arrays, see the design task.
Pull Request: https://projects.blender.org/blender/blender/pulls/104424
2023-03-20 15:55:13 +01:00
|
|
|
positions[vert],
|
|
|
|
(flag & MESH_FOREACH_USE_NORMAL) ? &vert_normals[vert].x : nullptr);
|
2019-07-07 18:58:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2023-07-24 22:06:55 +02:00
|
|
|
for (const int i : faces.index_range()) {
|
|
|
|
for (const int vert : corner_verts.slice(faces[i])) {
|
Mesh: Replace MLoop struct with generic attributes
Implements #102359.
Split the `MLoop` struct into two separate integer arrays called
`corner_verts` and `corner_edges`, referring to the vertex each corner
is attached to and the next edge around the face at each corner. These
arrays can be sliced to give access to the edges or vertices in a face.
Then they are often referred to as "poly_verts" or "poly_edges".
The main benefits are halving the necessary memory bandwidth when only
one array is used and simplifications from using regular integer indices
instead of a special-purpose struct.
The commit also starts a renaming from "loop" to "corner" in mesh code.
Like the other mesh struct of array refactors, forward compatibility is
kept by writing files with the older format. This will be done until 4.0
to ease the transition process.
Looking at a small portion of the patch should give a good impression
for the rest of the changes. I tried to make the changes as small as
possible so it's easy to tell the correctness from the diff. Though I
found Blender developers have been very inventive over the last decade
when finding different ways to loop over the corners in a face.
For performance, nearly every piece of code that deals with `Mesh` is
slightly impacted. Any algorithm that is memory bottle-necked should
see an improvement. For example, here is a comparison of interpolating
a vertex float attribute to face corners (Ryzen 3700x):
**Before** (Average: 3.7 ms, Min: 3.4 ms)
```
threading::parallel_for(loops.index_range(), 4096, [&](IndexRange range) {
for (const int64_t i : range) {
dst[i] = src[loops[i].v];
}
});
```
**After** (Average: 2.9 ms, Min: 2.6 ms)
```
array_utils::gather(src, corner_verts, dst);
```
That's an improvement of 28% to the average timings, and it's also a
simplification, since an index-based routine can be used instead.
For more examples using the new arrays, see the design task.
Pull Request: https://projects.blender.org/blender/blender/pulls/104424
2023-03-20 15:55:13 +01:00
|
|
|
if (facedot_tags[vert]) {
|
2023-07-27 04:04:18 +02:00
|
|
|
func(user_data,
|
2022-09-30 01:32:44 +02:00
|
|
|
i,
|
Mesh: Replace MLoop struct with generic attributes
Implements #102359.
Split the `MLoop` struct into two separate integer arrays called
`corner_verts` and `corner_edges`, referring to the vertex each corner
is attached to and the next edge around the face at each corner. These
arrays can be sliced to give access to the edges or vertices in a face.
Then they are often referred to as "poly_verts" or "poly_edges".
The main benefits are halving the necessary memory bandwidth when only
one array is used and simplifications from using regular integer indices
instead of a special-purpose struct.
The commit also starts a renaming from "loop" to "corner" in mesh code.
Like the other mesh struct of array refactors, forward compatibility is
kept by writing files with the older format. This will be done until 4.0
to ease the transition process.
Looking at a small portion of the patch should give a good impression
for the rest of the changes. I tried to make the changes as small as
possible so it's easy to tell the correctness from the diff. Though I
found Blender developers have been very inventive over the last decade
when finding different ways to loop over the corners in a face.
For performance, nearly every piece of code that deals with `Mesh` is
slightly impacted. Any algorithm that is memory bottle-necked should
see an improvement. For example, here is a comparison of interpolating
a vertex float attribute to face corners (Ryzen 3700x):
**Before** (Average: 3.7 ms, Min: 3.4 ms)
```
threading::parallel_for(loops.index_range(), 4096, [&](IndexRange range) {
for (const int64_t i : range) {
dst[i] = src[loops[i].v];
}
});
```
**After** (Average: 2.9 ms, Min: 2.6 ms)
```
array_utils::gather(src, corner_verts, dst);
```
That's an improvement of 28% to the average timings, and it's also a
simplification, since an index-based routine can be used instead.
For more examples using the new arrays, see the design task.
Pull Request: https://projects.blender.org/blender/blender/pulls/104424
2023-03-20 15:55:13 +01:00
|
|
|
positions[vert],
|
|
|
|
(flag & MESH_FOREACH_USE_NORMAL) ? &vert_normals[vert].x : nullptr);
|
2019-07-07 18:58:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-28 15:42:00 +02:00
|
|
|
/* Helpers based on above foreach loopers> */
|
|
|
|
|
2022-09-30 01:32:44 +02:00
|
|
|
struct MappedVCosData {
|
2018-06-28 15:42:00 +02:00
|
|
|
float (*vertexcos)[3];
|
|
|
|
BLI_bitmap *vertex_visit;
|
2022-09-30 01:32:44 +02:00
|
|
|
};
|
2018-06-28 15:42:00 +02:00
|
|
|
|
|
|
|
static void get_vertexcos__mapFunc(void *user_data,
|
|
|
|
int index,
|
|
|
|
const float co[3],
|
2022-10-12 01:27:27 +02:00
|
|
|
const float /*no*/[3])
|
2018-06-28 15:42:00 +02:00
|
|
|
{
|
|
|
|
MappedVCosData *mapped_vcos_data = (MappedVCosData *)user_data;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-06-28 15:42:00 +02:00
|
|
|
if (BLI_BITMAP_TEST(mapped_vcos_data->vertex_visit, index) == 0) {
|
|
|
|
/* We need coord from prototype vertex, not from copies,
|
|
|
|
* we assume they stored in the beginning of vertex array stored in evaluated mesh
|
|
|
|
* (mirror modifier for eg does this). */
|
|
|
|
copy_v3_v3(mapped_vcos_data->vertexcos[index], co);
|
|
|
|
BLI_BITMAP_ENABLE(mapped_vcos_data->vertex_visit, index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-30 22:25:06 +02:00
|
|
|
void BKE_mesh_foreach_mapped_vert_coords_get(const Mesh *me_eval,
|
|
|
|
float (*r_cos)[3],
|
|
|
|
const int totcos)
|
2018-06-28 15:42:00 +02:00
|
|
|
{
|
|
|
|
MappedVCosData user_data;
|
|
|
|
memset(r_cos, 0, sizeof(*r_cos) * totcos);
|
|
|
|
user_data.vertexcos = r_cos;
|
|
|
|
user_data.vertex_visit = BLI_BITMAP_NEW(totcos, __func__);
|
|
|
|
BKE_mesh_foreach_mapped_vert(me_eval, get_vertexcos__mapFunc, &user_data, MESH_FOREACH_NOP);
|
|
|
|
MEM_freeN(user_data.vertex_visit);
|
|
|
|
}
|