2023-08-16 00:20:26 +10:00
|
|
|
/* SPDX-FileCopyrightText: 2023 Blender Authors
|
2023-05-31 16:19:06 +02:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
2018-05-25 22:24:24 +05:30
|
|
|
|
2019-02-18 08:08:12 +11:00
|
|
|
/** \file
|
|
|
|
|
* \ingroup modifiers
|
2018-05-25 22:24:24 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
|
|
2023-10-06 23:00:29 +02:00
|
|
|
#include "BLI_array_utils.hh"
|
2020-02-26 14:24:57 +11:00
|
|
|
#include "BLI_bitmap.h"
|
2019-02-25 11:39:14 +01:00
|
|
|
#include "BLI_linklist.h"
|
2022-12-21 13:10:51 -06:00
|
|
|
#include "BLI_math_vector.h"
|
2019-02-25 11:39:14 +01:00
|
|
|
|
2020-06-05 10:41:03 -04:00
|
|
|
#include "BLT_translation.h"
|
|
|
|
|
|
2020-10-01 09:38:00 -05:00
|
|
|
#include "DNA_defaults.h"
|
2018-05-25 22:24:24 +05:30
|
|
|
#include "DNA_mesh_types.h"
|
2020-02-26 14:24:57 +11:00
|
|
|
#include "DNA_meshdata_types.h"
|
2018-05-25 22:24:24 +05:30
|
|
|
#include "DNA_object_types.h"
|
|
|
|
|
#include "DNA_scene_types.h"
|
2020-06-05 10:41:03 -04:00
|
|
|
#include "DNA_screen_types.h"
|
2018-05-25 22:24:24 +05:30
|
|
|
|
2023-01-10 16:12:14 -05:00
|
|
|
#include "BKE_attribute.hh"
|
2023-11-16 11:41:55 +01:00
|
|
|
#include "BKE_context.hh"
|
2024-01-29 18:57:16 -05:00
|
|
|
#include "BKE_deform.hh"
|
2024-01-15 12:44:04 -05:00
|
|
|
#include "BKE_lib_id.hh"
|
2023-03-12 22:29:15 +01:00
|
|
|
#include "BKE_mesh.hh"
|
2023-08-02 22:14:18 +02:00
|
|
|
#include "BKE_mesh_mapping.hh"
|
2023-09-25 17:48:21 -04:00
|
|
|
#include "BKE_screen.hh"
|
2020-06-05 10:41:03 -04:00
|
|
|
|
2023-08-05 02:57:52 +02:00
|
|
|
#include "UI_interface.hh"
|
|
|
|
|
#include "UI_resources.hh"
|
2020-06-05 10:41:03 -04:00
|
|
|
|
2023-08-10 22:40:27 +02:00
|
|
|
#include "RNA_access.hh"
|
2022-03-14 16:54:46 +01:00
|
|
|
#include "RNA_prototypes.h"
|
2018-05-25 22:24:24 +05:30
|
|
|
|
2023-05-04 18:35:37 +02:00
|
|
|
#include "MOD_modifiertypes.hh"
|
|
|
|
|
#include "MOD_ui_common.hh"
|
|
|
|
|
#include "MOD_util.hh"
|
2018-05-25 22:24:24 +05:30
|
|
|
|
2023-12-05 23:01:12 +01:00
|
|
|
#include "bmesh.hh"
|
2019-05-20 08:51:18 -04:00
|
|
|
|
2018-05-25 22:24:24 +05:30
|
|
|
#define CLNORS_VALID_VEC_LEN (1e-6f)
|
|
|
|
|
|
2022-10-06 16:05:56 -05:00
|
|
|
struct ModePair {
|
2018-05-25 22:24:24 +05:30
|
|
|
float val; /* Contains mode based value (face area / corner angle). */
|
2023-07-24 22:06:55 +02:00
|
|
|
int index; /* Index value per face or per loop. */
|
2022-10-06 16:05:56 -05:00
|
|
|
};
|
2018-05-25 22:24:24 +05:30
|
|
|
|
|
|
|
|
/* Sorting function used in modifier, sorts in decreasing order. */
|
|
|
|
|
static int modepair_cmp_by_val_inverse(const void *p1, const void *p2)
|
|
|
|
|
{
|
|
|
|
|
ModePair *r1 = (ModePair *)p1;
|
|
|
|
|
ModePair *r2 = (ModePair *)p2;
|
|
|
|
|
|
|
|
|
|
return (r1->val < r2->val) ? 1 : ((r1->val > r2->val) ? -1 : 0);
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-01 07:40:07 +10:00
|
|
|
/* There will be one of those per vertex
|
|
|
|
|
* (simple case, computing one normal per vertex), or per smooth fan. */
|
2022-10-06 16:05:56 -05:00
|
|
|
struct WeightedNormalDataAggregateItem {
|
2018-05-25 22:24:24 +05:30
|
|
|
float normal[3];
|
|
|
|
|
|
2022-03-28 12:29:47 +11:00
|
|
|
int loops_num; /* Count number of loops using this item so far. */
|
2018-05-25 22:24:24 +05:30
|
|
|
float curr_val; /* Current max val for this item. */
|
|
|
|
|
int curr_strength; /* Current max strength encountered for this item. */
|
2022-10-06 16:05:56 -05:00
|
|
|
};
|
2018-05-25 22:24:24 +05:30
|
|
|
|
|
|
|
|
#define NUM_CACHED_INVERSE_POWERS_OF_WEIGHT 128
|
|
|
|
|
|
2022-10-06 16:05:56 -05:00
|
|
|
struct WeightedNormalData {
|
|
|
|
|
int verts_num;
|
2018-05-25 22:24:24 +05:30
|
|
|
|
2023-03-12 22:29:15 +01:00
|
|
|
blender::Span<blender::float3> vert_positions;
|
|
|
|
|
blender::Span<blender::float3> vert_normals;
|
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
|
|
|
blender::Span<blender::int2> edges;
|
2023-03-12 22:29:15 +01:00
|
|
|
blender::MutableSpan<bool> sharp_edges;
|
2018-05-25 22:24:24 +05:30
|
|
|
|
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
|
|
|
blender::Span<int> corner_verts;
|
|
|
|
|
blender::Span<int> corner_edges;
|
2023-07-24 22:06:55 +02:00
|
|
|
blender::Span<int> loop_to_face;
|
2023-04-28 13:34:15 -04:00
|
|
|
blender::MutableSpan<blender::short2> clnors;
|
2022-10-06 16:05:56 -05:00
|
|
|
bool has_clnors; /* True if clnors already existed, false if we had to create them. */
|
2018-05-25 22:24:24 +05:30
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
blender::OffsetIndices<int> faces;
|
|
|
|
|
blender::Span<blender::float3> face_normals;
|
2023-12-12 17:49:51 -05:00
|
|
|
blender::VArraySpan<bool> sharp_faces;
|
2023-07-24 22:06:55 +02:00
|
|
|
const int *face_strength;
|
2018-05-25 22:24:24 +05:30
|
|
|
|
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
2022-09-05 11:56:34 -05:00
|
|
|
const MDeformVert *dvert;
|
2022-10-06 16:05:56 -05:00
|
|
|
int defgrp_index;
|
|
|
|
|
bool use_invert_vgroup;
|
2018-05-25 22:24:24 +05:30
|
|
|
|
2022-10-06 16:05:56 -05:00
|
|
|
float weight;
|
|
|
|
|
short mode;
|
2018-05-25 22:24:24 +05:30
|
|
|
|
|
|
|
|
/* Lower-level, internal processing data. */
|
|
|
|
|
float cached_inverse_powers_of_weight[NUM_CACHED_INVERSE_POWERS_OF_WEIGHT];
|
|
|
|
|
|
2023-05-02 16:58:54 -04:00
|
|
|
blender::Span<WeightedNormalDataAggregateItem> items_data;
|
2018-05-25 22:24:24 +05:30
|
|
|
|
|
|
|
|
ModePair *mode_pair;
|
2022-10-06 16:05:56 -05:00
|
|
|
};
|
2018-05-25 22:24:24 +05:30
|
|
|
|
2019-05-01 07:40:07 +10:00
|
|
|
/**
|
2023-07-24 22:06:55 +02:00
|
|
|
* Check strength of given face compared to those found so far for that given item
|
2019-05-01 07:40:07 +10:00
|
|
|
* (vertex or smooth fan), and reset matching item_data in case we get a stronger new strength.
|
|
|
|
|
*/
|
2023-07-24 22:06:55 +02:00
|
|
|
static bool check_item_face_strength(WeightedNormalData *wn_data,
|
2018-05-25 22:24:24 +05:30
|
|
|
WeightedNormalDataAggregateItem *item_data,
|
2023-07-24 22:06:55 +02:00
|
|
|
const int face_index)
|
2018-05-25 22:24:24 +05:30
|
|
|
{
|
2023-07-24 22:06:55 +02:00
|
|
|
BLI_assert(wn_data->face_strength != nullptr);
|
2018-05-25 22:24:24 +05:30
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
const int mp_strength = wn_data->face_strength[face_index];
|
2018-05-25 22:24:24 +05:30
|
|
|
|
|
|
|
|
if (mp_strength > item_data->curr_strength) {
|
|
|
|
|
item_data->curr_strength = mp_strength;
|
|
|
|
|
item_data->curr_val = 0.0f;
|
2022-03-28 12:29:47 +11:00
|
|
|
item_data->loops_num = 0;
|
2018-05-25 22:24:24 +05:30
|
|
|
zero_v3(item_data->normal);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return mp_strength == item_data->curr_strength;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void aggregate_item_normal(WeightedNormalModifierData *wnmd,
|
|
|
|
|
WeightedNormalData *wn_data,
|
|
|
|
|
WeightedNormalDataAggregateItem *item_data,
|
|
|
|
|
const int mv_index,
|
2023-07-24 22:06:55 +02:00
|
|
|
const int face_index,
|
2018-05-25 22:24:24 +05:30
|
|
|
const float curr_val,
|
|
|
|
|
const bool use_face_influence)
|
|
|
|
|
{
|
2023-07-24 22:06:55 +02:00
|
|
|
const blender::Span<blender::float3> face_normals = wn_data->face_normals;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
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
2022-09-05 11:56:34 -05:00
|
|
|
const MDeformVert *dvert = wn_data->dvert;
|
2018-05-25 22:24:24 +05:30
|
|
|
const int defgrp_index = wn_data->defgrp_index;
|
|
|
|
|
const bool use_invert_vgroup = wn_data->use_invert_vgroup;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-05-25 22:24:24 +05:30
|
|
|
const float weight = wn_data->weight;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-05-25 22:24:24 +05:30
|
|
|
float *cached_inverse_powers_of_weight = wn_data->cached_inverse_powers_of_weight;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-10-06 16:05:56 -05:00
|
|
|
const bool has_vgroup = dvert != nullptr;
|
2018-05-25 22:24:24 +05:30
|
|
|
const bool vert_of_group = has_vgroup &&
|
2022-10-06 16:05:56 -05:00
|
|
|
BKE_defvert_find_index(&dvert[mv_index], defgrp_index) != nullptr;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-05-25 22:24:24 +05:30
|
|
|
if (has_vgroup &&
|
2024-01-02 18:12:54 +01:00
|
|
|
((vert_of_group && use_invert_vgroup) || (!vert_of_group && !use_invert_vgroup)))
|
|
|
|
|
{
|
2018-05-25 22:24:24 +05:30
|
|
|
return;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
if (use_face_influence && !check_item_face_strength(wn_data, item_data, face_index)) {
|
2018-05-25 22:24:24 +05:30
|
|
|
return;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-05-25 22:24:24 +05:30
|
|
|
/* If item's curr_val is 0 init it to present value. */
|
|
|
|
|
if (item_data->curr_val == 0.0f) {
|
|
|
|
|
item_data->curr_val = curr_val;
|
|
|
|
|
}
|
|
|
|
|
if (!compare_ff(item_data->curr_val, curr_val, wnmd->thresh)) {
|
|
|
|
|
/* item's curr_val and present value differ more than threshold, update. */
|
2022-03-28 12:29:47 +11:00
|
|
|
item_data->loops_num++;
|
2018-05-25 22:24:24 +05:30
|
|
|
item_data->curr_val = curr_val;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-05-01 07:40:07 +10:00
|
|
|
/* Exponentially divided weight for each normal
|
|
|
|
|
* (since a few values will be used by most cases, we cache those). */
|
2022-03-28 12:29:47 +11:00
|
|
|
const int loops_num = item_data->loops_num;
|
|
|
|
|
if (loops_num < NUM_CACHED_INVERSE_POWERS_OF_WEIGHT &&
|
|
|
|
|
cached_inverse_powers_of_weight[loops_num] == 0.0f)
|
|
|
|
|
{
|
|
|
|
|
cached_inverse_powers_of_weight[loops_num] = 1.0f / powf(weight, loops_num);
|
2018-05-25 22:24:24 +05:30
|
|
|
}
|
2022-03-28 12:29:47 +11:00
|
|
|
const float inverted_n_weight = loops_num < NUM_CACHED_INVERSE_POWERS_OF_WEIGHT ?
|
|
|
|
|
cached_inverse_powers_of_weight[loops_num] :
|
|
|
|
|
1.0f / powf(weight, loops_num);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
madd_v3_v3fl(item_data->normal, face_normals[face_index], curr_val * inverted_n_weight);
|
2018-05-25 22:24:24 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void apply_weights_vertex_normal(WeightedNormalModifierData *wnmd,
|
|
|
|
|
WeightedNormalData *wn_data)
|
|
|
|
|
{
|
2022-11-11 22:56:44 -06:00
|
|
|
using namespace blender;
|
2022-03-28 12:29:47 +11:00
|
|
|
const int verts_num = wn_data->verts_num;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-03-12 22:29:15 +01:00
|
|
|
const blender::Span<blender::float3> positions = wn_data->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<int2> edges = wn_data->edges;
|
2023-07-24 22:06:55 +02:00
|
|
|
const blender::OffsetIndices faces = wn_data->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 = wn_data->corner_verts;
|
|
|
|
|
const blender::Span<int> corner_edges = wn_data->corner_edges;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-04-28 13:34:15 -04:00
|
|
|
MutableSpan<short2> clnors = wn_data->clnors;
|
2023-07-24 22:06:55 +02:00
|
|
|
const blender::Span<int> loop_to_face = wn_data->loop_to_face;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
const blender::Span<blender::float3> face_normals = wn_data->face_normals;
|
|
|
|
|
const int *face_strength = wn_data->face_strength;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
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
2022-09-05 11:56:34 -05:00
|
|
|
const MDeformVert *dvert = wn_data->dvert;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-05-25 22:24:24 +05:30
|
|
|
const short mode = wn_data->mode;
|
|
|
|
|
ModePair *mode_pair = wn_data->mode_pair;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-05-25 22:24:24 +05:30
|
|
|
const bool has_clnors = wn_data->has_clnors;
|
2023-05-10 14:41:10 +02:00
|
|
|
bke::mesh::CornerNormalSpaceArray lnors_spacearr;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-05-25 22:24:24 +05:30
|
|
|
const bool keep_sharp = (wnmd->flag & MOD_WEIGHTEDNORMAL_KEEP_SHARP) != 0;
|
|
|
|
|
const bool use_face_influence = (wnmd->flag & MOD_WEIGHTEDNORMAL_FACE_INFLUENCE) != 0 &&
|
2023-07-24 22:06:55 +02:00
|
|
|
face_strength != nullptr;
|
2022-10-06 16:05:56 -05:00
|
|
|
const bool has_vgroup = dvert != nullptr;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2024-01-15 11:39:50 -05:00
|
|
|
blender::Array<blender::float3> corner_normals;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-05-02 16:58:54 -04:00
|
|
|
Array<WeightedNormalDataAggregateItem> items_data;
|
2018-05-25 22:24:24 +05:30
|
|
|
if (keep_sharp) {
|
2019-05-01 07:40:07 +10:00
|
|
|
/* This will give us loop normal spaces,
|
2024-01-15 11:39:50 -05:00
|
|
|
* we do not actually care about computed corner_normals for now... */
|
|
|
|
|
corner_normals.reinitialize(corner_verts.size());
|
2024-01-10 13:12:16 -05:00
|
|
|
bke::mesh::normals_calc_corners(positions,
|
|
|
|
|
edges,
|
|
|
|
|
faces,
|
|
|
|
|
corner_verts,
|
|
|
|
|
corner_edges,
|
|
|
|
|
loop_to_face,
|
|
|
|
|
wn_data->vert_normals,
|
|
|
|
|
wn_data->face_normals,
|
|
|
|
|
wn_data->sharp_edges,
|
|
|
|
|
wn_data->sharp_faces,
|
|
|
|
|
has_clnors ? clnors.data() : nullptr,
|
|
|
|
|
&lnors_spacearr,
|
2024-01-15 11:39:50 -05:00
|
|
|
corner_normals);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-05-10 14:41:10 +02:00
|
|
|
WeightedNormalDataAggregateItem start_item{};
|
|
|
|
|
start_item.curr_strength = FACE_STRENGTH_WEAK;
|
|
|
|
|
items_data = Array<WeightedNormalDataAggregateItem>(lnors_spacearr.spaces.size(), start_item);
|
2018-05-25 22:24:24 +05:30
|
|
|
}
|
|
|
|
|
else {
|
2023-05-10 14:41:10 +02:00
|
|
|
WeightedNormalDataAggregateItem start_item{};
|
|
|
|
|
start_item.curr_strength = FACE_STRENGTH_WEAK;
|
|
|
|
|
items_data = Array<WeightedNormalDataAggregateItem>(verts_num, start_item);
|
2023-05-24 13:23:39 -04:00
|
|
|
lnors_spacearr.corner_space_indices.reinitialize(corner_verts.size());
|
2023-10-06 23:00:29 +02:00
|
|
|
array_utils::fill_index_range<int>(lnors_spacearr.corner_space_indices);
|
2018-05-25 22:24:24 +05:30
|
|
|
}
|
|
|
|
|
wn_data->items_data = items_data;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-05-25 22:24:24 +05:30
|
|
|
switch (mode) {
|
|
|
|
|
case MOD_WEIGHTEDNORMAL_MODE_FACE:
|
2023-07-24 22:06:55 +02:00
|
|
|
for (const int i : faces.index_range()) {
|
|
|
|
|
const int face_index = mode_pair[i].index;
|
2018-05-25 22:24:24 +05:30
|
|
|
const float mp_val = mode_pair[i].val;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2024-01-05 12:49:28 -05:00
|
|
|
for (const int corner : faces[face_index]) {
|
|
|
|
|
const int mv_index = corner_verts[corner];
|
|
|
|
|
const int space_index = lnors_spacearr.corner_space_indices[corner];
|
2023-05-10 14:41:10 +02:00
|
|
|
|
|
|
|
|
WeightedNormalDataAggregateItem *item_data = keep_sharp ? &items_data[space_index] :
|
|
|
|
|
&items_data[mv_index];
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-05-25 22:24:24 +05:30
|
|
|
aggregate_item_normal(
|
2023-07-24 22:06:55 +02:00
|
|
|
wnmd, wn_data, item_data, mv_index, face_index, mp_val, use_face_influence);
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
2018-05-25 22:24:24 +05:30
|
|
|
case MOD_WEIGHTEDNORMAL_MODE_ANGLE:
|
|
|
|
|
case MOD_WEIGHTEDNORMAL_MODE_FACE_ANGLE:
|
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
|
|
|
for (int i = 0; i < corner_verts.size(); i++) {
|
2024-01-05 12:49:28 -05:00
|
|
|
const int corner = mode_pair[i].index;
|
2018-05-25 22:24:24 +05:30
|
|
|
const float ml_val = mode_pair[i].val;
|
2024-01-05 12:49:28 -05:00
|
|
|
const int space_index = lnors_spacearr.corner_space_indices[corner];
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2024-01-05 12:49:28 -05:00
|
|
|
const int face_index = loop_to_face[corner];
|
|
|
|
|
const int mv_index = corner_verts[corner];
|
2023-05-10 14:41:10 +02:00
|
|
|
WeightedNormalDataAggregateItem *item_data = keep_sharp ? &items_data[space_index] :
|
|
|
|
|
&items_data[mv_index];
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-05-25 22:24:24 +05:30
|
|
|
aggregate_item_normal(
|
2023-07-24 22:06:55 +02:00
|
|
|
wnmd, wn_data, item_data, mv_index, face_index, ml_val, use_face_influence);
|
2018-05-25 22:24:24 +05:30
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
2022-05-17 15:12:12 +02:00
|
|
|
BLI_assert_unreachable();
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
|
|
|
|
|
2018-05-25 22:24:24 +05:30
|
|
|
/* Validate computed weighted normals. */
|
2023-05-02 16:58:54 -04:00
|
|
|
for (int item_index : items_data.index_range()) {
|
2018-05-25 22:24:24 +05:30
|
|
|
if (normalize_v3(items_data[item_index].normal) < CLNORS_VALID_VEC_LEN) {
|
|
|
|
|
zero_v3(items_data[item_index].normal);
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-25 22:24:24 +05:30
|
|
|
if (keep_sharp) {
|
|
|
|
|
/* Set loop normals for normal computed for each lnor space (smooth fan).
|
2024-01-15 11:39:50 -05:00
|
|
|
* Note that corner_normals is already populated with clnors
|
2019-05-01 07:40:07 +10:00
|
|
|
* (before this modifier is applied, at start of this function),
|
|
|
|
|
* so no need to recompute them here. */
|
2024-01-05 12:49:28 -05:00
|
|
|
for (int corner = 0; corner < corner_verts.size(); corner++) {
|
|
|
|
|
const int space_index = lnors_spacearr.corner_space_indices[corner];
|
2023-05-10 14:41:10 +02:00
|
|
|
WeightedNormalDataAggregateItem *item_data = &items_data[space_index];
|
2018-05-25 22:24:24 +05:30
|
|
|
if (!is_zero_v3(item_data->normal)) {
|
2024-01-15 11:39:50 -05:00
|
|
|
copy_v3_v3(corner_normals[corner], item_data->normal);
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-10 13:12:16 -05:00
|
|
|
blender::bke::mesh::normals_corner_custom_set(positions,
|
|
|
|
|
edges,
|
|
|
|
|
faces,
|
|
|
|
|
corner_verts,
|
|
|
|
|
corner_edges,
|
|
|
|
|
wn_data->vert_normals,
|
|
|
|
|
face_normals,
|
|
|
|
|
wn_data->sharp_faces,
|
|
|
|
|
wn_data->sharp_edges,
|
2024-01-15 11:39:50 -05:00
|
|
|
corner_normals,
|
2024-01-10 13:12:16 -05:00
|
|
|
clnors);
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
|
|
|
|
else {
|
2022-09-08 11:22:30 +10:00
|
|
|
/* TODO: Ideally, we could add an option to `BKE_mesh_normals_loop_custom_[from_verts_]set()`
|
2020-02-13 14:01:52 +11:00
|
|
|
* to keep current clnors instead of resetting them to default auto-computed ones,
|
2019-05-01 07:40:07 +10:00
|
|
|
* when given new custom normal is zero-vec.
|
2018-05-25 22:24:24 +05:30
|
|
|
* But this is not exactly trivial change, better to keep this optimization for later...
|
|
|
|
|
*/
|
|
|
|
|
if (!has_vgroup) {
|
2021-07-03 23:08:40 +10:00
|
|
|
/* NOTE: in theory, we could avoid this extra allocation & copying...
|
2019-05-01 07:40:07 +10:00
|
|
|
* But think we can live with it for now,
|
2018-05-25 22:24:24 +05:30
|
|
|
* and it makes code simpler & cleaner. */
|
2023-04-27 10:31:24 -04:00
|
|
|
blender::Array<blender::float3> vert_normals(verts_num, float3(0.0f));
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2024-01-05 12:49:28 -05:00
|
|
|
for (int corner = 0; corner < corner_verts.size(); corner++) {
|
|
|
|
|
const int mv_index = corner_verts[corner];
|
2018-05-25 22:24:24 +05:30
|
|
|
copy_v3_v3(vert_normals[mv_index], items_data[mv_index].normal);
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2024-01-10 13:12:16 -05:00
|
|
|
blender::bke::mesh::normals_corner_custom_set_from_verts(positions,
|
|
|
|
|
edges,
|
|
|
|
|
faces,
|
|
|
|
|
corner_verts,
|
|
|
|
|
corner_edges,
|
|
|
|
|
wn_data->vert_normals,
|
|
|
|
|
face_normals,
|
|
|
|
|
wn_data->sharp_faces,
|
|
|
|
|
wn_data->sharp_edges,
|
|
|
|
|
vert_normals,
|
|
|
|
|
clnors);
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2018-05-25 22:24:24 +05:30
|
|
|
else {
|
2024-01-15 11:39:50 -05:00
|
|
|
corner_normals.reinitialize(corner_verts.size());
|
2024-01-10 13:12:16 -05:00
|
|
|
blender::bke::mesh::normals_calc_corners(positions,
|
|
|
|
|
edges,
|
|
|
|
|
faces,
|
|
|
|
|
corner_verts,
|
|
|
|
|
corner_edges,
|
|
|
|
|
loop_to_face,
|
|
|
|
|
wn_data->vert_normals,
|
|
|
|
|
face_normals,
|
|
|
|
|
wn_data->sharp_edges,
|
|
|
|
|
wn_data->sharp_faces,
|
|
|
|
|
has_clnors ? clnors.data() : nullptr,
|
|
|
|
|
nullptr,
|
2024-01-15 11:39:50 -05:00
|
|
|
corner_normals);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2024-01-05 12:49:28 -05:00
|
|
|
for (int corner = 0; corner < corner_verts.size(); corner++) {
|
|
|
|
|
const int item_index = corner_verts[corner];
|
2018-05-25 22:24:24 +05:30
|
|
|
if (!is_zero_v3(items_data[item_index].normal)) {
|
2024-01-15 11:39:50 -05:00
|
|
|
copy_v3_v3(corner_normals[corner], items_data[item_index].normal);
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2018-05-25 22:24:24 +05:30
|
|
|
}
|
2024-01-10 13:12:16 -05:00
|
|
|
blender::bke::mesh::normals_corner_custom_set(positions,
|
|
|
|
|
edges,
|
|
|
|
|
faces,
|
|
|
|
|
corner_verts,
|
|
|
|
|
corner_edges,
|
|
|
|
|
wn_data->vert_normals,
|
|
|
|
|
face_normals,
|
|
|
|
|
wn_data->sharp_faces,
|
|
|
|
|
wn_data->sharp_edges,
|
2024-01-15 11:39:50 -05:00
|
|
|
corner_normals,
|
2024-01-10 13:12:16 -05:00
|
|
|
clnors);
|
2018-05-25 22:24:24 +05:30
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2018-05-25 22:24:24 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void wn_face_area(WeightedNormalModifierData *wnmd, WeightedNormalData *wn_data)
|
|
|
|
|
{
|
2023-03-12 22:29:15 +01:00
|
|
|
const blender::Span<blender::float3> positions = wn_data->vert_positions;
|
2023-07-24 22:06:55 +02:00
|
|
|
const blender::OffsetIndices faces = wn_data->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 = wn_data->corner_verts;
|
2018-05-25 22:24:24 +05:30
|
|
|
|
2022-10-06 16:05:56 -05:00
|
|
|
ModePair *face_area = static_cast<ModePair *>(
|
2023-07-24 22:06:55 +02:00
|
|
|
MEM_malloc_arrayN(faces.size(), sizeof(*face_area), __func__));
|
2018-05-25 22:24:24 +05:30
|
|
|
|
|
|
|
|
ModePair *f_area = face_area;
|
2023-07-24 22:06:55 +02:00
|
|
|
for (const int i : faces.index_range()) {
|
|
|
|
|
f_area[i].val = blender::bke::mesh::face_area_calc(positions, corner_verts.slice(faces[i]));
|
2023-02-28 12:46:35 -05:00
|
|
|
f_area[i].index = i;
|
2018-05-25 22:24:24 +05:30
|
|
|
}
|
|
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
qsort(face_area, faces.size(), sizeof(*face_area), modepair_cmp_by_val_inverse);
|
2018-05-25 22:24:24 +05:30
|
|
|
|
|
|
|
|
wn_data->mode_pair = face_area;
|
|
|
|
|
apply_weights_vertex_normal(wnmd, wn_data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void wn_corner_angle(WeightedNormalModifierData *wnmd, WeightedNormalData *wn_data)
|
|
|
|
|
{
|
2023-03-12 22:29:15 +01:00
|
|
|
const blender::Span<blender::float3> positions = wn_data->vert_positions;
|
2023-07-24 22:06:55 +02:00
|
|
|
const blender::OffsetIndices faces = wn_data->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 = wn_data->corner_verts;
|
2018-05-25 22:24:24 +05:30
|
|
|
|
2022-10-06 16:05:56 -05:00
|
|
|
ModePair *corner_angle = static_cast<ModePair *>(
|
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
|
|
|
MEM_malloc_arrayN(corner_verts.size(), sizeof(*corner_angle), __func__));
|
2018-05-25 22:24:24 +05:30
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
for (const int i : faces.index_range()) {
|
|
|
|
|
const blender::IndexRange face = faces[i];
|
2022-10-06 16:05:56 -05:00
|
|
|
float *index_angle = static_cast<float *>(
|
2023-07-24 22:06:55 +02:00
|
|
|
MEM_malloc_arrayN(face.size(), sizeof(*index_angle), __func__));
|
|
|
|
|
blender::bke::mesh::face_angles_calc(
|
|
|
|
|
positions, corner_verts.slice(face), {index_angle, face.size()});
|
2018-05-25 22:24:24 +05:30
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
ModePair *c_angl = &corner_angle[face.start()];
|
2018-05-25 22:24:24 +05:30
|
|
|
float *angl = index_angle;
|
2024-01-05 12:49:28 -05:00
|
|
|
for (int corner = face.start(); corner < face.start() + face.size();
|
|
|
|
|
corner++, c_angl++, angl++)
|
2018-05-25 22:24:24 +05:30
|
|
|
{
|
2022-10-07 22:52:52 +11:00
|
|
|
c_angl->val = float(M_PI) - *angl;
|
2024-01-05 12:49:28 -05:00
|
|
|
c_angl->index = corner;
|
2018-05-25 22:24:24 +05:30
|
|
|
}
|
|
|
|
|
MEM_freeN(index_angle);
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
qsort(corner_angle, corner_verts.size(), sizeof(*corner_angle), modepair_cmp_by_val_inverse);
|
2018-05-25 22:24:24 +05:30
|
|
|
|
|
|
|
|
wn_data->mode_pair = corner_angle;
|
|
|
|
|
apply_weights_vertex_normal(wnmd, wn_data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void wn_face_with_angle(WeightedNormalModifierData *wnmd, WeightedNormalData *wn_data)
|
|
|
|
|
{
|
2023-03-12 22:29:15 +01:00
|
|
|
const blender::Span<blender::float3> positions = wn_data->vert_positions;
|
2023-07-24 22:06:55 +02:00
|
|
|
const blender::OffsetIndices faces = wn_data->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 = wn_data->corner_verts;
|
2018-05-25 22:24:24 +05:30
|
|
|
|
2022-10-06 16:05:56 -05:00
|
|
|
ModePair *combined = static_cast<ModePair *>(
|
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
|
|
|
MEM_malloc_arrayN(corner_verts.size(), sizeof(*combined), __func__));
|
2018-05-25 22:24:24 +05:30
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
for (const int i : faces.index_range()) {
|
|
|
|
|
const blender::IndexRange face = faces[i];
|
|
|
|
|
const blender::Span<int> face_verts = corner_verts.slice(face);
|
|
|
|
|
const float face_area = blender::bke::mesh::face_area_calc(positions, face_verts);
|
2022-10-06 16:05:56 -05:00
|
|
|
float *index_angle = static_cast<float *>(
|
2023-07-24 22:06:55 +02:00
|
|
|
MEM_malloc_arrayN(size_t(face.size()), sizeof(*index_angle), __func__));
|
|
|
|
|
blender::bke::mesh::face_angles_calc(positions, face_verts, {index_angle, face.size()});
|
2018-05-25 22:24:24 +05:30
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
ModePair *cmbnd = &combined[face.start()];
|
2018-05-25 22:24:24 +05:30
|
|
|
float *angl = index_angle;
|
2024-01-05 12:49:28 -05:00
|
|
|
for (int corner = face.start(); corner < face.start() + face.size(); corner++, cmbnd++, angl++)
|
2018-05-25 22:24:24 +05:30
|
|
|
{
|
|
|
|
|
/* In this case val is product of corner angle and face area. */
|
2022-10-07 22:52:52 +11:00
|
|
|
cmbnd->val = (float(M_PI) - *angl) * face_area;
|
2024-01-05 12:49:28 -05:00
|
|
|
cmbnd->index = corner;
|
2018-05-25 22:24:24 +05:30
|
|
|
}
|
|
|
|
|
MEM_freeN(index_angle);
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
qsort(combined, corner_verts.size(), sizeof(*combined), modepair_cmp_by_val_inverse);
|
2018-05-25 22:24:24 +05:30
|
|
|
|
|
|
|
|
wn_data->mode_pair = combined;
|
|
|
|
|
apply_weights_vertex_normal(wnmd, wn_data);
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-27 12:04:18 +10:00
|
|
|
static Mesh *modify_mesh(ModifierData *md, const ModifierEvalContext *ctx, Mesh *mesh)
|
2018-05-25 22:24:24 +05:30
|
|
|
{
|
2022-11-11 22:56:44 -06:00
|
|
|
using namespace blender;
|
2018-05-25 22:24:24 +05:30
|
|
|
WeightedNormalModifierData *wnmd = (WeightedNormalModifierData *)md;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-05-01 07:34:41 +10:00
|
|
|
Mesh *result;
|
2022-10-06 16:05:56 -05:00
|
|
|
result = (Mesh *)BKE_id_copy_ex(nullptr, &mesh->id, nullptr, LIB_ID_COPY_LOCALIZE);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-12-20 02:21:48 +01:00
|
|
|
const int verts_num = result->verts_num;
|
2023-03-12 22:29:15 +01: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<int2> edges = mesh->edges();
|
2023-07-24 22:06:55 +02:00
|
|
|
const OffsetIndices faces = result->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();
|
|
|
|
|
const blender::Span<int> corner_edges = mesh->corner_edges();
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-05-01 07:34:41 +10:00
|
|
|
/* Right now:
|
2019-05-01 07:40:07 +10:00
|
|
|
* If weight = 50 then all faces are given equal weight.
|
2023-05-24 11:21:18 +10:00
|
|
|
* If weight > 50 then more weight given to faces with larger values (face area / corner angle).
|
|
|
|
|
* If weight < 50 then more weight given to faces with lesser values. However current calculation
|
2019-05-01 07:40:07 +10:00
|
|
|
* does not converge to min/max.
|
|
|
|
|
*/
|
2022-10-07 22:52:52 +11:00
|
|
|
float weight = float(wnmd->weight) / 50.0f;
|
2019-05-01 07:34:41 +10:00
|
|
|
if (wnmd->weight == 100) {
|
2022-10-07 22:52:52 +11:00
|
|
|
weight = float(SHRT_MAX);
|
2019-05-01 07:34:41 +10:00
|
|
|
}
|
|
|
|
|
else if (wnmd->weight == 1) {
|
2022-10-07 22:52:52 +11:00
|
|
|
weight = 1 / float(SHRT_MAX);
|
2019-05-01 07:34:41 +10:00
|
|
|
}
|
|
|
|
|
else if ((weight - 1) * 25 > 1) {
|
|
|
|
|
weight = (weight - 1) * 25;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-12-19 20:38:59 -05:00
|
|
|
blender::short2 *clnors = static_cast<blender::short2 *>(CustomData_get_layer_for_write(
|
|
|
|
|
&result->corner_data, CD_CUSTOMLOOPNORMAL, mesh->corners_num));
|
2019-05-01 07:34:41 +10:00
|
|
|
|
2021-07-23 16:56:00 +10:00
|
|
|
/* Keep info whether we had clnors,
|
2019-05-01 07:40:07 +10:00
|
|
|
* it helps when generating clnor spaces and default normals. */
|
2022-10-06 16:05:56 -05:00
|
|
|
const bool has_clnors = clnors != nullptr;
|
2019-05-01 07:34:41 +10:00
|
|
|
if (!clnors) {
|
2023-04-28 13:34:15 -04:00
|
|
|
clnors = static_cast<blender::short2 *>(CustomData_add_layer(
|
2023-12-19 20:38:59 -05:00
|
|
|
&result->corner_data, CD_CUSTOMLOOPNORMAL, CD_SET_DEFAULT, corner_verts.size()));
|
2019-05-01 07:34:41 +10:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
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
2022-09-05 11:56:34 -05:00
|
|
|
const MDeformVert *dvert;
|
2019-05-01 07:34:41 +10:00
|
|
|
int defgrp_index;
|
|
|
|
|
MOD_get_vgroup(ctx->object, mesh, wnmd->defgrp_name, &dvert, &defgrp_index);
|
|
|
|
|
|
2023-08-30 23:41:59 +02:00
|
|
|
const Span<int> loop_to_face_map = result->corner_to_face_map();
|
2022-11-11 22:56:44 -06:00
|
|
|
|
2023-01-10 16:12:14 -05:00
|
|
|
bke::MutableAttributeAccessor attributes = result->attributes_for_write();
|
|
|
|
|
bke::SpanAttributeWriter<bool> sharp_edges = attributes.lookup_or_add_for_write_span<bool>(
|
2023-12-20 13:13:16 -05:00
|
|
|
"sharp_edge", bke::AttrDomain::Edge);
|
2023-01-10 16:12:14 -05:00
|
|
|
|
2022-10-06 16:05:56 -05:00
|
|
|
WeightedNormalData wn_data{};
|
|
|
|
|
wn_data.verts_num = verts_num;
|
2019-05-01 07:34:41 +10:00
|
|
|
|
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 00:10:43 -05:00
|
|
|
wn_data.vert_positions = positions;
|
2023-03-12 22:29:15 +01:00
|
|
|
wn_data.vert_normals = result->vert_normals();
|
2023-02-23 10:39:51 -05:00
|
|
|
wn_data.edges = edges;
|
2023-03-12 22:29:15 +01:00
|
|
|
wn_data.sharp_edges = sharp_edges.span;
|
2019-05-01 07:34:41 +10:00
|
|
|
|
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
|
|
|
wn_data.corner_verts = corner_verts;
|
|
|
|
|
wn_data.corner_edges = corner_edges;
|
2023-07-24 22:06:55 +02:00
|
|
|
wn_data.loop_to_face = loop_to_face_map;
|
2023-12-20 02:21:48 +01:00
|
|
|
wn_data.clnors = {clnors, mesh->corners_num};
|
2022-10-06 16:05:56 -05:00
|
|
|
wn_data.has_clnors = has_clnors;
|
2019-05-01 07:34:41 +10:00
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
wn_data.faces = faces;
|
|
|
|
|
wn_data.face_normals = mesh->face_normals();
|
2023-12-20 13:13:16 -05:00
|
|
|
wn_data.sharp_faces = *attributes.lookup<bool>("sharp_face", bke::AttrDomain::Face);
|
2023-07-24 22:06:55 +02:00
|
|
|
wn_data.face_strength = static_cast<const int *>(CustomData_get_layer_named(
|
2023-07-25 21:15:52 +02:00
|
|
|
&result->face_data, CD_PROP_INT32, MOD_WEIGHTEDNORMALS_FACEWEIGHT_CDLAYER_ID));
|
2019-05-01 07:34:41 +10:00
|
|
|
|
2022-10-06 16:05:56 -05:00
|
|
|
wn_data.dvert = dvert;
|
|
|
|
|
wn_data.defgrp_index = defgrp_index;
|
|
|
|
|
wn_data.use_invert_vgroup = (wnmd->flag & MOD_WEIGHTEDNORMAL_INVERT_VGROUP) != 0;
|
2019-05-01 07:34:41 +10:00
|
|
|
|
2022-10-06 16:05:56 -05:00
|
|
|
wn_data.weight = weight;
|
|
|
|
|
wn_data.mode = wnmd->mode;
|
2019-05-01 07:34:41 +10:00
|
|
|
|
|
|
|
|
switch (wnmd->mode) {
|
|
|
|
|
case MOD_WEIGHTEDNORMAL_MODE_FACE:
|
|
|
|
|
wn_face_area(wnmd, &wn_data);
|
|
|
|
|
break;
|
|
|
|
|
case MOD_WEIGHTEDNORMAL_MODE_ANGLE:
|
|
|
|
|
wn_corner_angle(wnmd, &wn_data);
|
|
|
|
|
break;
|
|
|
|
|
case MOD_WEIGHTEDNORMAL_MODE_FACE_ANGLE:
|
|
|
|
|
wn_face_with_angle(wnmd, &wn_data);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-05-01 07:34:41 +10:00
|
|
|
MEM_SAFE_FREE(wn_data.mode_pair);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-10-12 20:55:26 -05:00
|
|
|
result->runtime->is_original_bmesh = false;
|
2020-08-11 21:46:06 +10:00
|
|
|
|
2023-01-10 16:12:14 -05:00
|
|
|
sharp_edges.finish();
|
|
|
|
|
|
2019-05-01 07:34:41 +10:00
|
|
|
return result;
|
2018-05-25 22:24:24 +05:30
|
|
|
}
|
|
|
|
|
|
2023-07-27 12:04:18 +10:00
|
|
|
static void init_data(ModifierData *md)
|
2018-05-25 22:24:24 +05:30
|
|
|
{
|
|
|
|
|
WeightedNormalModifierData *wnmd = (WeightedNormalModifierData *)md;
|
2020-10-01 09:38:00 -05:00
|
|
|
|
|
|
|
|
BLI_assert(MEMCMP_STRUCT_AFTER_IS_ZERO(wnmd, modifier));
|
|
|
|
|
|
|
|
|
|
MEMCPY_STRUCT_AFTER(wnmd, DNA_struct_default_get(WeightedNormalModifierData), modifier);
|
2018-05-25 22:24:24 +05:30
|
|
|
}
|
|
|
|
|
|
2023-07-27 12:04:18 +10:00
|
|
|
static void required_data_mask(ModifierData *md, CustomData_MeshMasks *r_cddata_masks)
|
2018-05-25 22:24:24 +05:30
|
|
|
{
|
|
|
|
|
WeightedNormalModifierData *wnmd = (WeightedNormalModifierData *)md;
|
|
|
|
|
|
Refactor CDData masks, to have one mask per mesh elem type.
We already have different storages for cddata of verts, edges etc.,
'simply' do the same for the mask flags we use all around Blender code
to request some data, or limit some operation to some layers, etc.
Reason we need this is that some cddata types (like Normals) are
actually shared between verts/polys/loops, and we don’t want to generate
clnors everytime we request vnors!
As a side note, this also does final fix to T59338, which was the
trigger for this patch (need to request computed loop normals for
another mesh than evaluated one).
Reviewers: brecht, campbellbarton, sergey
Differential Revision: https://developer.blender.org/D4407
2019-03-07 11:13:40 +01:00
|
|
|
r_cddata_masks->lmask = CD_MASK_CUSTOMLOOPNORMAL;
|
|
|
|
|
|
|
|
|
|
if (wnmd->defgrp_name[0] != '\0') {
|
|
|
|
|
r_cddata_masks->vmask |= CD_MASK_MDEFORMVERT;
|
2018-05-25 22:24:24 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (wnmd->flag & MOD_WEIGHTEDNORMAL_FACE_INFLUENCE) {
|
2020-06-10 15:47:31 +02:00
|
|
|
r_cddata_masks->pmask |= CD_MASK_PROP_INT32;
|
2018-05-25 22:24:24 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-27 12:04:18 +10:00
|
|
|
static bool depends_on_normals(ModifierData * /*md*/)
|
2018-05-25 22:24:24 +05:30
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-12 10:27:27 +11:00
|
|
|
static void panel_draw(const bContext * /*C*/, Panel *panel)
|
2020-06-05 10:41:03 -04:00
|
|
|
{
|
UI: Small Tweaks to Modifier Layouts for Consistency
These changes are smaller, made based on feedback and a pass on all
the layouts for clarity and consistency. The Multires modifier UI will
be addressed in a separate patch. Here is an overview of the changes:
Renaming Options:
- Build: "Start" -> "Start Frame"
- Curve: "From Radius" -> "Size from Radius"
- Screw: "Calc Order" -> "Calculate Order"
- Displace, Warp, Wave: "Texture Coordinates Object" -> "Object"
Move Mode Toggle to Top & Expand:
- Bevel, Boolean, Normal Edit, Subdivision
Use Columns for Tighter Spacing:
- Displace, Explode, Ocean, Particle Instance, Remesh, Shrinkwrap,
Solidify, Warp, Weighted Normal, Wave
Misc:
- Bevel: Set inactive properties for vertex bevel
- Mesh Sequence Cache: Remove box for cache file
- Skin: Don't align "Mark Loose" and "Clear Loose"
- Array: Expand relative offset subpanel by default
- Array: Move start cap, end cap to a new subpanel
- Bevel: Move width type above width
Differential Revision: https://developer.blender.org/D8115
2020-07-02 10:47:02 -04:00
|
|
|
uiLayout *col;
|
2020-06-05 10:41:03 -04:00
|
|
|
uiLayout *layout = panel->layout;
|
|
|
|
|
|
|
|
|
|
PointerRNA ob_ptr;
|
2020-09-02 14:13:26 -05:00
|
|
|
PointerRNA *ptr = modifier_panel_get_property_pointers(panel, &ob_ptr);
|
2020-06-05 10:41:03 -04:00
|
|
|
|
|
|
|
|
uiLayoutSetPropSep(layout, true);
|
|
|
|
|
|
2023-07-29 15:06:33 +10:00
|
|
|
uiItemR(layout, ptr, "mode", UI_ITEM_NONE, nullptr, ICON_NONE);
|
2020-06-05 10:41:03 -04:00
|
|
|
|
2023-07-29 15:06:33 +10:00
|
|
|
uiItemR(layout, ptr, "weight", UI_ITEM_NONE, IFACE_("Weight"), ICON_NONE);
|
|
|
|
|
uiItemR(layout, ptr, "thresh", UI_ITEM_NONE, IFACE_("Threshold"), ICON_NONE);
|
2020-06-05 10:41:03 -04:00
|
|
|
|
UI: Small Tweaks to Modifier Layouts for Consistency
These changes are smaller, made based on feedback and a pass on all
the layouts for clarity and consistency. The Multires modifier UI will
be addressed in a separate patch. Here is an overview of the changes:
Renaming Options:
- Build: "Start" -> "Start Frame"
- Curve: "From Radius" -> "Size from Radius"
- Screw: "Calc Order" -> "Calculate Order"
- Displace, Warp, Wave: "Texture Coordinates Object" -> "Object"
Move Mode Toggle to Top & Expand:
- Bevel, Boolean, Normal Edit, Subdivision
Use Columns for Tighter Spacing:
- Displace, Explode, Ocean, Particle Instance, Remesh, Shrinkwrap,
Solidify, Warp, Weighted Normal, Wave
Misc:
- Bevel: Set inactive properties for vertex bevel
- Mesh Sequence Cache: Remove box for cache file
- Skin: Don't align "Mark Loose" and "Clear Loose"
- Array: Expand relative offset subpanel by default
- Array: Move start cap, end cap to a new subpanel
- Bevel: Move width type above width
Differential Revision: https://developer.blender.org/D8115
2020-07-02 10:47:02 -04:00
|
|
|
col = uiLayoutColumn(layout, false);
|
2023-07-29 15:06:33 +10:00
|
|
|
uiItemR(col, ptr, "keep_sharp", UI_ITEM_NONE, nullptr, ICON_NONE);
|
|
|
|
|
uiItemR(col, ptr, "use_face_influence", UI_ITEM_NONE, nullptr, ICON_NONE);
|
2020-06-05 10:41:03 -04:00
|
|
|
|
2022-10-06 16:05:56 -05:00
|
|
|
modifier_vgroup_ui(layout, ptr, &ob_ptr, "vertex_group", "invert_vertex_group", nullptr);
|
2020-06-05 10:41:03 -04:00
|
|
|
|
2020-09-02 14:13:26 -05:00
|
|
|
modifier_panel_end(layout, ptr);
|
2020-06-05 10:41:03 -04:00
|
|
|
}
|
|
|
|
|
|
2023-07-27 12:04:18 +10:00
|
|
|
static void panel_register(ARegionType *region_type)
|
2020-06-05 10:41:03 -04:00
|
|
|
{
|
|
|
|
|
modifier_panel_register(region_type, eModifierType_WeightedNormal, panel_draw);
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-25 22:24:24 +05:30
|
|
|
ModifierTypeInfo modifierType_WeightedNormal = {
|
2023-07-26 17:08:14 +02:00
|
|
|
/*idname*/ "WeightedNormal",
|
2023-01-16 12:41:11 +11:00
|
|
|
/*name*/ N_("WeightedNormal"),
|
2023-07-27 12:04:18 +10:00
|
|
|
/*struct_name*/ "WeightedNormalModifierData",
|
|
|
|
|
/*struct_size*/ sizeof(WeightedNormalModifierData),
|
2023-01-16 12:41:11 +11:00
|
|
|
/*srna*/ &RNA_WeightedNormalModifier,
|
2023-11-14 10:03:56 +01:00
|
|
|
/*type*/ ModifierTypeType::Constructive,
|
2023-01-16 12:41:11 +11:00
|
|
|
/*flags*/ eModifierTypeFlag_AcceptsMesh | eModifierTypeFlag_SupportsMapping |
|
2018-05-25 22:24:24 +05:30
|
|
|
eModifierTypeFlag_SupportsEditmode | eModifierTypeFlag_EnableInEditmode,
|
2023-01-16 12:41:11 +11:00
|
|
|
/*icon*/ ICON_MOD_NORMALEDIT,
|
|
|
|
|
|
2023-07-27 12:04:18 +10:00
|
|
|
/*copy_data*/ BKE_modifier_copydata_generic,
|
|
|
|
|
|
|
|
|
|
/*deform_verts*/ nullptr,
|
|
|
|
|
/*deform_matrices*/ nullptr,
|
|
|
|
|
/*deform_verts_EM*/ nullptr,
|
|
|
|
|
/*deform_matrices_EM*/ nullptr,
|
|
|
|
|
/*modify_mesh*/ modify_mesh,
|
|
|
|
|
/*modify_geometry_set*/ nullptr,
|
|
|
|
|
|
|
|
|
|
/*init_data*/ init_data,
|
|
|
|
|
/*required_data_mask*/ required_data_mask,
|
|
|
|
|
/*free_data*/ nullptr,
|
|
|
|
|
/*is_disabled*/ nullptr,
|
|
|
|
|
/*update_depsgraph*/ nullptr,
|
|
|
|
|
/*depends_on_time*/ nullptr,
|
|
|
|
|
/*depends_on_normals*/ depends_on_normals,
|
|
|
|
|
/*foreach_ID_link*/ nullptr,
|
|
|
|
|
/*foreach_tex_link*/ nullptr,
|
|
|
|
|
/*free_runtime_data*/ nullptr,
|
|
|
|
|
/*panel_register*/ panel_register,
|
|
|
|
|
/*blend_write*/ nullptr,
|
|
|
|
|
/*blend_read*/ nullptr,
|
2024-01-18 22:51:30 +01:00
|
|
|
/*foreach_cache*/ nullptr,
|
2018-05-25 22:24:24 +05:30
|
|
|
};
|