2022-02-11 09:07:11 +11:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
2015-02-05 14:49:44 +01:00
|
|
|
|
2019-02-18 08:08:12 +11:00
|
|
|
/** \file
|
|
|
|
|
* \ingroup modifiers
|
2015-02-05 14:49:44 +01:00
|
|
|
*/
|
|
|
|
|
|
2022-10-05 13:44:02 -05:00
|
|
|
#include <cstring>
|
2015-02-05 14:49:44 +01:00
|
|
|
|
|
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
|
|
2019-02-25 11:39:14 +01:00
|
|
|
#include "BLI_utildefines.h"
|
|
|
|
|
|
|
|
|
|
#include "BLI_bitmap.h"
|
|
|
|
|
#include "BLI_math.h"
|
|
|
|
|
|
2020-06-05 10:41:03 -04:00
|
|
|
#include "BLT_translation.h"
|
|
|
|
|
|
2020-10-01 09:38:00 -05:00
|
|
|
#include "DNA_defaults.h"
|
2015-02-05 14:49:44 +01:00
|
|
|
#include "DNA_mesh_types.h"
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "DNA_meshdata_types.h"
|
|
|
|
|
#include "DNA_object_types.h"
|
2020-06-05 10:41:03 -04:00
|
|
|
#include "DNA_screen_types.h"
|
2015-02-05 14:49:44 +01:00
|
|
|
|
2023-01-10 16:12:14 -05:00
|
|
|
#include "BKE_attribute.hh"
|
2020-06-05 10:41:03 -04:00
|
|
|
#include "BKE_context.h"
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "BKE_deform.h"
|
2020-02-10 12:58:59 +01:00
|
|
|
#include "BKE_lib_id.h"
|
|
|
|
|
#include "BKE_lib_query.h"
|
2015-02-05 14:49:44 +01:00
|
|
|
#include "BKE_mesh.h"
|
2020-06-05 10:41:03 -04:00
|
|
|
#include "BKE_screen.h"
|
|
|
|
|
|
|
|
|
|
#include "UI_interface.h"
|
|
|
|
|
#include "UI_resources.h"
|
|
|
|
|
|
|
|
|
|
#include "RNA_access.h"
|
2022-03-14 16:54:46 +01:00
|
|
|
#include "RNA_prototypes.h"
|
2015-02-05 14:49:44 +01:00
|
|
|
|
2018-12-07 15:45:53 +01:00
|
|
|
#include "DEG_depsgraph_query.h"
|
|
|
|
|
|
2020-06-05 10:41:03 -04:00
|
|
|
#include "MOD_ui_common.h"
|
2015-02-05 14:49:44 +01:00
|
|
|
#include "MOD_util.h"
|
|
|
|
|
|
|
|
|
|
static void generate_vert_coordinates(Mesh *mesh,
|
2018-05-07 14:36:00 +02:00
|
|
|
Object *ob,
|
|
|
|
|
Object *ob_center,
|
|
|
|
|
const float offset[3],
|
2022-03-28 12:29:47 +11:00
|
|
|
const int verts_num,
|
2015-02-05 14:49:44 +01:00
|
|
|
float (*r_cos)[3],
|
|
|
|
|
float r_size[3])
|
|
|
|
|
{
|
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
|
|
|
using namespace blender;
|
2015-02-05 14:49:44 +01:00
|
|
|
float min_co[3], max_co[3];
|
|
|
|
|
float diff[3];
|
|
|
|
|
bool do_diff = false;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-02-05 14:49:44 +01:00
|
|
|
INIT_MINMAX(min_co, max_co);
|
2019-04-17 06:17:24 +02: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
|
|
|
const Span<float3> positions = mesh->vert_positions();
|
|
|
|
|
for (int i = 0; i < mesh->totvert; i++) {
|
|
|
|
|
copy_v3_v3(r_cos[i], positions[i]);
|
2022-10-02 11:16:14 -05:00
|
|
|
if (r_size != nullptr && ob_center == nullptr) {
|
2018-05-07 14:36:00 +02:00
|
|
|
minmax_v3v3_v3(min_co, max_co, r_cos[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-05-01 07:40:07 +10:00
|
|
|
/* Get size (i.e. deformation of the spheroid generating normals),
|
|
|
|
|
* either from target object, or own geometry. */
|
2022-10-02 11:16:14 -05:00
|
|
|
if (r_size != nullptr) {
|
|
|
|
|
if (ob_center != nullptr) {
|
2019-02-20 08:25:00 +11:00
|
|
|
/* Using 'scale' as 'size' here. The input object is typically an empty
|
|
|
|
|
* who's scale is used to define an ellipsoid instead of a simple sphere. */
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-05-01 07:40:07 +10:00
|
|
|
/* Not we are not interested in signs here - they are even troublesome actually,
|
|
|
|
|
* due to security clamping! */
|
2019-02-18 15:43:06 +11:00
|
|
|
abs_v3_v3(r_size, ob_center->scale);
|
2018-05-07 14:36:00 +02:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
/* Set size. */
|
|
|
|
|
sub_v3_v3v3(r_size, max_co, min_co);
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-05-07 14:36:00 +02:00
|
|
|
/* Error checks - we do not want one or more of our sizes to be null! */
|
|
|
|
|
if (is_zero_v3(r_size)) {
|
|
|
|
|
r_size[0] = r_size[1] = r_size[2] = 1.0f;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
CLAMP_MIN(r_size[0], FLT_EPSILON);
|
|
|
|
|
CLAMP_MIN(r_size[1], FLT_EPSILON);
|
|
|
|
|
CLAMP_MIN(r_size[2], FLT_EPSILON);
|
|
|
|
|
}
|
2015-02-05 14:49:44 +01:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-10-02 11:16:14 -05:00
|
|
|
if (ob_center != nullptr) {
|
2015-05-12 10:56:28 +02:00
|
|
|
float inv_obmat[4][4];
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-02-05 14:49:44 +01:00
|
|
|
/* Translate our coordinates so that center of ob_center is at (0, 0, 0). */
|
2015-03-17 18:14:38 +01:00
|
|
|
/* Get ob_center (world) coordinates in ob local coordinates.
|
2023-02-12 14:37:16 +11:00
|
|
|
* No need to take into account ob_center's space here, see #44027. */
|
2022-10-24 14:16:37 +02:00
|
|
|
invert_m4_m4(inv_obmat, ob->object_to_world);
|
|
|
|
|
mul_v3_m4v3(diff, inv_obmat, ob_center->object_to_world[3]);
|
2015-03-17 18:14:38 +01:00
|
|
|
negate_v3(diff);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-02-05 14:49:44 +01:00
|
|
|
do_diff = true;
|
|
|
|
|
}
|
2022-10-02 11:16:14 -05:00
|
|
|
else if (offset != nullptr && !is_zero_v3(offset)) {
|
2015-02-05 14:49:44 +01:00
|
|
|
negate_v3_v3(diff, offset);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-02-05 14:49:44 +01:00
|
|
|
do_diff = true;
|
|
|
|
|
}
|
|
|
|
|
/* Else, no need to change coordinates! */
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-02-05 14:49:44 +01:00
|
|
|
if (do_diff) {
|
2022-03-28 12:29:47 +11:00
|
|
|
int i = verts_num;
|
2015-02-05 14:49:44 +01:00
|
|
|
while (i--) {
|
|
|
|
|
add_v3_v3(r_cos[i], diff);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Note this modifies nos_new in-place. */
|
|
|
|
|
static void mix_normals(const float mix_factor,
|
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,
|
2015-02-05 14:49:44 +01:00
|
|
|
const int defgrp_index,
|
|
|
|
|
const bool use_invert_vgroup,
|
2016-06-06 21:41:17 +02:00
|
|
|
const float mix_limit,
|
|
|
|
|
const short mix_mode,
|
2022-03-28 12:29:47 +11:00
|
|
|
const int verts_num,
|
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 MLoop *mloop,
|
2015-02-05 14:49:44 +01:00
|
|
|
float (*nos_old)[3],
|
|
|
|
|
float (*nos_new)[3],
|
2022-03-28 12:29:47 +11:00
|
|
|
const int loops_num)
|
2015-02-05 14:49:44 +01:00
|
|
|
{
|
|
|
|
|
/* Mix with org normals... */
|
2022-10-02 11:16:14 -05:00
|
|
|
float *facs = nullptr, *wfac;
|
2015-02-05 14:49:44 +01:00
|
|
|
float(*no_new)[3], (*no_old)[3];
|
|
|
|
|
int i;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-02-05 14:49:44 +01:00
|
|
|
if (dvert) {
|
2022-10-03 10:24:06 +11:00
|
|
|
facs = static_cast<float *>(MEM_malloc_arrayN(size_t(loops_num), sizeof(*facs), __func__));
|
2015-02-05 14:49:44 +01:00
|
|
|
BKE_defvert_extract_vgroup_to_loopweights(
|
2022-05-13 18:31:29 +02:00
|
|
|
dvert, defgrp_index, verts_num, mloop, loops_num, use_invert_vgroup, facs);
|
2015-02-05 14:49:44 +01:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-03-28 12:29:47 +11:00
|
|
|
for (i = loops_num, no_new = nos_new, no_old = nos_old, wfac = facs; i--;
|
2015-02-05 14:49:44 +01:00
|
|
|
no_new++, no_old++, wfac++) {
|
|
|
|
|
const float fac = facs ? *wfac * mix_factor : mix_factor;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-02-05 14:49:44 +01:00
|
|
|
switch (mix_mode) {
|
|
|
|
|
case MOD_NORMALEDIT_MIX_ADD:
|
|
|
|
|
add_v3_v3(*no_new, *no_old);
|
|
|
|
|
normalize_v3(*no_new);
|
|
|
|
|
break;
|
|
|
|
|
case MOD_NORMALEDIT_MIX_SUB:
|
|
|
|
|
sub_v3_v3(*no_new, *no_old);
|
|
|
|
|
normalize_v3(*no_new);
|
|
|
|
|
break;
|
|
|
|
|
case MOD_NORMALEDIT_MIX_MUL:
|
|
|
|
|
mul_v3_v3(*no_new, *no_old);
|
|
|
|
|
normalize_v3(*no_new);
|
|
|
|
|
break;
|
|
|
|
|
case MOD_NORMALEDIT_MIX_COPY:
|
|
|
|
|
break;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-07-22 04:05:38 +10:00
|
|
|
interp_v3_v3v3_slerp_safe(
|
|
|
|
|
*no_new,
|
|
|
|
|
*no_old,
|
|
|
|
|
*no_new,
|
2022-10-03 10:24:06 +11:00
|
|
|
(mix_limit < float(M_PI)) ? min_ff(fac, mix_limit / angle_v3v3(*no_new, *no_old)) : fac);
|
2015-02-05 14:49:44 +01:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-02-05 14:49:44 +01:00
|
|
|
MEM_SAFE_FREE(facs);
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-28 15:48:08 +01:00
|
|
|
/* Check poly normals and new loop normals are compatible, otherwise flip polygons
|
|
|
|
|
* (and invert matching poly normals). */
|
|
|
|
|
static bool polygons_check_flip(MLoop *mloop,
|
|
|
|
|
float (*nos)[3],
|
|
|
|
|
CustomData *ldata,
|
2023-01-13 17:21:20 -06:00
|
|
|
const int totloop,
|
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 MPoly *mpoly,
|
2022-12-17 12:47:43 +11:00
|
|
|
float (*poly_normals)[3],
|
2022-03-28 12:29:47 +11:00
|
|
|
const int polys_num)
|
2016-02-28 15:48:08 +01: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 MPoly *mp;
|
2023-01-13 17:21:20 -06:00
|
|
|
MDisps *mdisp = static_cast<MDisps *>(CustomData_get_layer_for_write(ldata, CD_MDISPS, totloop));
|
2016-02-28 15:48:08 +01:00
|
|
|
int i;
|
|
|
|
|
bool flipped = false;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-03-28 12:29:47 +11:00
|
|
|
for (i = 0, mp = mpoly; i < polys_num; i++, mp++) {
|
2016-02-28 15:48:08 +01:00
|
|
|
float norsum[3] = {0.0f};
|
|
|
|
|
float(*no)[3];
|
|
|
|
|
int j;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-02-28 15:48:08 +01:00
|
|
|
for (j = 0, no = &nos[mp->loopstart]; j < mp->totloop; j++, no++) {
|
|
|
|
|
add_v3_v3(norsum, *no);
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-02-28 15:48:08 +01:00
|
|
|
if (!normalize_v3(norsum)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-02-28 15:48:08 +01:00
|
|
|
/* If average of new loop normals is opposed to polygon normal, flip polygon. */
|
2022-12-17 12:47:43 +11:00
|
|
|
if (dot_v3v3(poly_normals[i], norsum) < 0.0f) {
|
2016-06-07 13:04:05 +02:00
|
|
|
BKE_mesh_polygon_flip_ex(mp, mloop, ldata, nos, mdisp, true);
|
2022-12-17 12:47:43 +11:00
|
|
|
negate_v3(poly_normals[i]);
|
2016-02-28 15:48:08 +01:00
|
|
|
flipped = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-02-28 15:48:08 +01:00
|
|
|
return flipped;
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-05 14:49:44 +01:00
|
|
|
static void normalEditModifier_do_radial(NormalEditModifierData *enmd,
|
2022-10-03 17:37:25 -05:00
|
|
|
const ModifierEvalContext * /*ctx*/,
|
2018-12-07 15:45:53 +01:00
|
|
|
Object *ob,
|
|
|
|
|
Mesh *mesh,
|
2015-02-05 14:49:44 +01:00
|
|
|
short (*clnors)[2],
|
2022-12-17 12:47:43 +11:00
|
|
|
float (*loop_normals)[3],
|
|
|
|
|
const float (*poly_normals)[3],
|
2016-06-06 21:41:17 +02:00
|
|
|
const short mix_mode,
|
|
|
|
|
const float mix_factor,
|
|
|
|
|
const float mix_limit,
|
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,
|
2015-02-05 14:49:44 +01:00
|
|
|
const int defgrp_index,
|
|
|
|
|
const bool use_invert_vgroup,
|
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
|
|
|
const float (*vert_positions)[3],
|
2022-03-28 12:29:47 +11:00
|
|
|
const int verts_num,
|
2023-01-10 16:12:14 -05:00
|
|
|
const MEdge *medge,
|
2022-03-28 12:29:47 +11:00
|
|
|
const int edges_num,
|
2023-01-10 16:12:14 -05:00
|
|
|
bool *sharp_edges,
|
2015-02-05 14:49:44 +01:00
|
|
|
MLoop *mloop,
|
2022-03-28 12:29:47 +11:00
|
|
|
const int loops_num,
|
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 MPoly *mpoly,
|
2022-03-28 12:29:47 +11:00
|
|
|
const int polys_num)
|
2015-02-05 14:49:44 +01:00
|
|
|
{
|
2019-03-26 11:25:07 +01:00
|
|
|
Object *ob_target = enmd->target;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-06-14 11:41:12 +02:00
|
|
|
const bool do_polynors_fix = (enmd->flag & MOD_NORMALEDIT_NO_POLYNORS_FIX) == 0;
|
2015-02-05 14:49:44 +01:00
|
|
|
int i;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-10-02 11:16:14 -05:00
|
|
|
float(*cos)[3] = static_cast<float(*)[3]>(
|
2022-10-03 10:24:06 +11:00
|
|
|
MEM_malloc_arrayN(size_t(verts_num), sizeof(*cos), __func__));
|
2022-10-02 11:16:14 -05:00
|
|
|
float(*nos)[3] = static_cast<float(*)[3]>(
|
2022-10-03 10:24:06 +11:00
|
|
|
MEM_malloc_arrayN(size_t(loops_num), sizeof(*nos), __func__));
|
2015-02-05 14:49:44 +01:00
|
|
|
float size[3];
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-10-03 10:24:06 +11:00
|
|
|
BLI_bitmap *done_verts = BLI_BITMAP_NEW(size_t(verts_num), __func__);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-03-28 12:29:47 +11:00
|
|
|
generate_vert_coordinates(mesh, ob, ob_target, enmd->offset, verts_num, cos, size);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-02-06 13:54:38 +11:00
|
|
|
/**
|
2021-07-20 22:52:31 +10:00
|
|
|
* size gives us our spheroid coefficients `(A, B, C)`.
|
2015-02-05 14:49:44 +01:00
|
|
|
* Then, we want to find out for each vert its (a, b, c) triple (proportional to (A, B, C) one).
|
|
|
|
|
*
|
2021-07-20 22:52:31 +10:00
|
|
|
* Ellipsoid basic equation: `(x^2/a^2) + (y^2/b^2) + (z^2/c^2) = 1`.
|
2019-05-01 07:40:07 +10:00
|
|
|
* Since we want to find (a, b, c) matching this equation and proportional to (A, B, C),
|
|
|
|
|
* we can do:
|
2015-02-06 13:54:38 +11:00
|
|
|
* <pre>
|
2015-02-05 14:49:44 +01:00
|
|
|
* m = B / A
|
|
|
|
|
* n = C / A
|
2015-02-06 13:54:38 +11:00
|
|
|
* </pre>
|
|
|
|
|
*
|
2015-02-05 14:49:44 +01:00
|
|
|
* hence:
|
2015-02-06 13:54:38 +11:00
|
|
|
* <pre>
|
2015-02-05 14:49:44 +01:00
|
|
|
* (x^2/a^2) + (y^2/b^2) + (z^2/c^2) = 1
|
|
|
|
|
* -> b^2*c^2*x^2 + a^2*c^2*y^2 + a^2*b^2*z^2 = a^2*b^2*c^2
|
|
|
|
|
* b = ma
|
|
|
|
|
* c = na
|
|
|
|
|
* -> m^2*a^2*n^2*a^2*x^2 + a^2*n^2*a^2*y^2 + a^2*m^2*a^2*z^2 = a^2*m^2*a^2*n^2*a^2
|
|
|
|
|
* -> m^2*n^2*a^4*x^2 + n^2*a^4*y^2 + m^2*a^4*z^2 = m^2*n^2*a^6
|
|
|
|
|
* -> a^2 = (m^2*n^2*x^2 + n^2y^2 + m^2z^2) / (m^2*n^2) = x^2 + (y^2 / m^2) + (z^2 / n^2)
|
|
|
|
|
* -> b^2 = (m^2*n^2*x^2 + n^2y^2 + m^2z^2) / (n^2) = (m^2 * x^2) + y^2 + (m^2 * z^2 / n^2)
|
|
|
|
|
* -> c^2 = (m^2*n^2*x^2 + n^2y^2 + m^2z^2) / (m^2) = (n^2 * x^2) + (n^2 * y^2 / m^2) + z^2
|
2015-02-06 13:54:38 +11:00
|
|
|
* </pre>
|
2015-02-05 14:49:44 +01:00
|
|
|
*
|
|
|
|
|
* All we have to do now is compute normal of the spheroid at that point:
|
2015-02-06 13:54:38 +11:00
|
|
|
* <pre>
|
2015-02-05 14:49:44 +01:00
|
|
|
* n = (x / a^2, y / b^2, z / c^2)
|
2015-02-06 13:54:38 +11:00
|
|
|
* </pre>
|
2015-02-05 14:49:44 +01:00
|
|
|
* And we are done!
|
|
|
|
|
*/
|
|
|
|
|
{
|
|
|
|
|
const float a = size[0], b = size[1], c = size[2];
|
|
|
|
|
const float m2 = (b * b) / (a * a);
|
|
|
|
|
const float n2 = (c * c) / (a * a);
|
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 MLoop *ml;
|
2015-02-05 14:49:44 +01:00
|
|
|
float(*no)[3];
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-02-05 14:49:44 +01:00
|
|
|
/* We reuse cos to now store the ellipsoid-normal of the verts! */
|
2022-03-28 12:29:47 +11:00
|
|
|
for (i = loops_num, ml = mloop, no = nos; i--; ml++, no++) {
|
2015-02-05 14:49:44 +01:00
|
|
|
const int vidx = ml->v;
|
|
|
|
|
float *co = cos[vidx];
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-02-05 14:49:44 +01:00
|
|
|
if (!BLI_BITMAP_TEST(done_verts, vidx)) {
|
|
|
|
|
const float x2 = co[0] * co[0];
|
|
|
|
|
const float y2 = co[1] * co[1];
|
|
|
|
|
const float z2 = co[2] * co[2];
|
|
|
|
|
const float a2 = x2 + (y2 / m2) + (z2 / n2);
|
|
|
|
|
const float b2 = (m2 * x2) + y2 + (m2 * z2 / n2);
|
|
|
|
|
const float c2 = (n2 * x2) + (n2 * y2 / m2) + z2;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-02-05 14:49:44 +01:00
|
|
|
co[0] /= a2;
|
|
|
|
|
co[1] /= b2;
|
|
|
|
|
co[2] /= c2;
|
|
|
|
|
normalize_v3(co);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-02-05 14:49:44 +01:00
|
|
|
BLI_BITMAP_ENABLE(done_verts, vidx);
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2015-02-05 14:49:44 +01:00
|
|
|
copy_v3_v3(*no, co);
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-17 12:47:43 +11:00
|
|
|
if (loop_normals) {
|
2015-02-05 14:49:44 +01:00
|
|
|
mix_normals(mix_factor,
|
|
|
|
|
dvert,
|
|
|
|
|
defgrp_index,
|
|
|
|
|
use_invert_vgroup,
|
|
|
|
|
mix_limit,
|
2016-06-06 21:41:17 +02:00
|
|
|
mix_mode,
|
2022-03-28 12:29:47 +11:00
|
|
|
verts_num,
|
2016-06-06 21:41:17 +02:00
|
|
|
mloop,
|
2022-12-17 12:47:43 +11:00
|
|
|
loop_normals,
|
2016-06-06 21:41:17 +02:00
|
|
|
nos,
|
2022-03-28 12:29:47 +11:00
|
|
|
loops_num);
|
2015-02-05 14:49:44 +01:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-01-13 17:21:20 -06:00
|
|
|
if (do_polynors_fix && polygons_check_flip(mloop,
|
|
|
|
|
nos,
|
|
|
|
|
&mesh->ldata,
|
|
|
|
|
mesh->totloop,
|
|
|
|
|
mpoly,
|
|
|
|
|
BKE_mesh_poly_normals_for_write(mesh),
|
|
|
|
|
polys_num)) {
|
2016-06-07 13:04:05 +02:00
|
|
|
/* We need to recompute vertex normals! */
|
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 14:37:58 -06:00
|
|
|
BKE_mesh_normals_tag_dirty(mesh);
|
2016-02-28 15:48:08 +01:00
|
|
|
}
|
2019-04-17 06:17:24 +02: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
|
|
|
BKE_mesh_normals_loop_custom_set(vert_positions,
|
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 14:37:58 -06:00
|
|
|
BKE_mesh_vertex_normals_ensure(mesh),
|
2022-03-28 12:29:47 +11:00
|
|
|
verts_num,
|
2015-02-05 14:49:44 +01:00
|
|
|
medge,
|
2022-03-28 12:29:47 +11:00
|
|
|
edges_num,
|
2015-02-05 14:49:44 +01:00
|
|
|
mloop,
|
|
|
|
|
nos,
|
2022-03-28 12:29:47 +11:00
|
|
|
loops_num,
|
2015-02-05 14:49:44 +01:00
|
|
|
mpoly,
|
2022-12-17 12:47:43 +11:00
|
|
|
poly_normals,
|
2022-03-28 12:29:47 +11:00
|
|
|
polys_num,
|
2023-01-10 16:12:14 -05:00
|
|
|
sharp_edges,
|
2015-02-05 14:49:44 +01:00
|
|
|
clnors);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-02-05 14:49:44 +01:00
|
|
|
MEM_freeN(cos);
|
|
|
|
|
MEM_freeN(nos);
|
|
|
|
|
MEM_freeN(done_verts);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void normalEditModifier_do_directional(NormalEditModifierData *enmd,
|
2022-10-03 17:37:25 -05:00
|
|
|
const ModifierEvalContext * /*ctx*/,
|
2018-12-07 15:45:53 +01:00
|
|
|
Object *ob,
|
|
|
|
|
Mesh *mesh,
|
2015-02-05 14:49:44 +01:00
|
|
|
short (*clnors)[2],
|
2022-12-17 12:47:43 +11:00
|
|
|
float (*loop_normals)[3],
|
|
|
|
|
const float (*poly_normals)[3],
|
2016-06-06 21:41:17 +02:00
|
|
|
const short mix_mode,
|
|
|
|
|
const float mix_factor,
|
|
|
|
|
const float mix_limit,
|
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,
|
2015-02-05 14:49:44 +01:00
|
|
|
const int defgrp_index,
|
|
|
|
|
const bool use_invert_vgroup,
|
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
|
|
|
const float (*positions)[3],
|
2022-03-28 12:29:47 +11:00
|
|
|
const int verts_num,
|
2023-01-10 16:12:14 -05:00
|
|
|
const MEdge *medge,
|
2022-03-28 12:29:47 +11:00
|
|
|
const int edges_num,
|
2023-01-10 16:12:14 -05:00
|
|
|
bool *sharp_edges,
|
2015-02-05 14:49:44 +01:00
|
|
|
MLoop *mloop,
|
2022-03-28 12:29:47 +11:00
|
|
|
const int loops_num,
|
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 MPoly *mpoly,
|
2022-03-28 12:29:47 +11:00
|
|
|
const int polys_num)
|
2015-02-05 14:49:44 +01:00
|
|
|
{
|
2019-03-26 11:25:07 +01:00
|
|
|
Object *ob_target = enmd->target;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-06-14 11:41:12 +02:00
|
|
|
const bool do_polynors_fix = (enmd->flag & MOD_NORMALEDIT_NO_POLYNORS_FIX) == 0;
|
2016-09-30 12:15:18 +02:00
|
|
|
const bool use_parallel_normals = (enmd->flag & MOD_NORMALEDIT_USE_DIRECTION_PARALLEL) != 0;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-10-02 11:16:14 -05:00
|
|
|
float(*nos)[3] = static_cast<float(*)[3]>(
|
2022-10-03 10:24:06 +11:00
|
|
|
MEM_malloc_arrayN(size_t(loops_num), sizeof(*nos), __func__));
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-02-05 14:49:44 +01:00
|
|
|
float target_co[3];
|
|
|
|
|
int i;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-02-05 14:49:44 +01:00
|
|
|
/* Get target's center coordinates in ob local coordinates. */
|
2018-05-07 14:36:00 +02:00
|
|
|
float mat[4][4];
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-10-24 14:16:37 +02:00
|
|
|
invert_m4_m4(mat, ob->object_to_world);
|
|
|
|
|
mul_m4_m4m4(mat, mat, ob_target->object_to_world);
|
2018-05-07 14:36:00 +02:00
|
|
|
copy_v3_v3(target_co, mat[3]);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-09-30 12:15:18 +02:00
|
|
|
if (use_parallel_normals) {
|
2015-02-05 14:49:44 +01:00
|
|
|
float no[3];
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-02-05 14:49:44 +01:00
|
|
|
sub_v3_v3v3(no, target_co, enmd->offset);
|
|
|
|
|
normalize_v3(no);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-03-28 12:29:47 +11:00
|
|
|
for (i = loops_num; i--;) {
|
2015-02-05 14:49:44 +01:00
|
|
|
copy_v3_v3(nos[i], no);
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
|
|
|
|
}
|
2015-02-05 14:49:44 +01:00
|
|
|
else {
|
2022-10-02 11:16:14 -05:00
|
|
|
float(*cos)[3] = static_cast<float(*)[3]>(
|
2022-10-03 10:24:06 +11:00
|
|
|
MEM_malloc_arrayN(size_t(verts_num), sizeof(*cos), __func__));
|
2022-10-02 11:16:14 -05:00
|
|
|
generate_vert_coordinates(mesh, ob, ob_target, nullptr, verts_num, cos, nullptr);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-10-03 10:24:06 +11:00
|
|
|
BLI_bitmap *done_verts = BLI_BITMAP_NEW(size_t(verts_num), __func__);
|
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 MLoop *ml;
|
2015-02-05 14:49:44 +01:00
|
|
|
float(*no)[3];
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-09-30 12:15:18 +02:00
|
|
|
/* We reuse cos to now store the 'to target' normal of the verts! */
|
2022-03-28 12:29:47 +11:00
|
|
|
for (i = loops_num, no = nos, ml = mloop; i--; no++, ml++) {
|
2016-09-30 12:15:18 +02:00
|
|
|
const int vidx = ml->v;
|
2015-02-05 14:49:44 +01:00
|
|
|
float *co = cos[vidx];
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-02-05 14:49:44 +01:00
|
|
|
if (!BLI_BITMAP_TEST(done_verts, vidx)) {
|
2016-09-30 12:15:18 +02:00
|
|
|
sub_v3_v3v3(co, target_co, co);
|
2015-02-05 14:49:44 +01:00
|
|
|
normalize_v3(co);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-02-05 14:49:44 +01:00
|
|
|
BLI_BITMAP_ENABLE(done_verts, vidx);
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
|
|
|
|
|
2015-02-05 14:49:44 +01:00
|
|
|
copy_v3_v3(*no, co);
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
|
|
|
|
|
2015-02-05 14:49:44 +01:00
|
|
|
MEM_freeN(done_verts);
|
|
|
|
|
MEM_freeN(cos);
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-12-17 12:47:43 +11:00
|
|
|
if (loop_normals) {
|
2018-05-07 14:36:00 +02:00
|
|
|
mix_normals(mix_factor,
|
2019-04-17 06:17:24 +02:00
|
|
|
dvert,
|
2016-09-30 12:15:18 +02:00
|
|
|
defgrp_index,
|
2018-05-07 14:36:00 +02:00
|
|
|
use_invert_vgroup,
|
2016-06-06 21:41:17 +02:00
|
|
|
mix_limit,
|
2018-05-07 14:36:00 +02:00
|
|
|
mix_mode,
|
2022-03-28 12:29:47 +11:00
|
|
|
verts_num,
|
2019-04-17 06:17:24 +02:00
|
|
|
mloop,
|
2022-12-17 12:47:43 +11:00
|
|
|
loop_normals,
|
2018-05-07 14:36:00 +02:00
|
|
|
nos,
|
2022-03-28 12:29:47 +11:00
|
|
|
loops_num);
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
|
|
|
|
|
2023-01-13 17:21:20 -06:00
|
|
|
if (do_polynors_fix && polygons_check_flip(mloop,
|
|
|
|
|
nos,
|
|
|
|
|
&mesh->ldata,
|
|
|
|
|
mesh->totloop,
|
|
|
|
|
mpoly,
|
|
|
|
|
BKE_mesh_poly_normals_for_write(mesh),
|
|
|
|
|
polys_num)) {
|
2021-09-15 14:44:56 -05:00
|
|
|
BKE_mesh_normals_tag_dirty(mesh);
|
2019-04-17 06:17:24 +02: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
|
|
|
BKE_mesh_normals_loop_custom_set(positions,
|
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 14:37:58 -06:00
|
|
|
BKE_mesh_vertex_normals_ensure(mesh),
|
2022-03-28 12:29:47 +11:00
|
|
|
verts_num,
|
2018-12-07 15:45:53 +01:00
|
|
|
medge,
|
2022-03-28 12:29:47 +11:00
|
|
|
edges_num,
|
2019-04-17 06:17:24 +02:00
|
|
|
mloop,
|
|
|
|
|
nos,
|
2022-03-28 12:29:47 +11:00
|
|
|
loops_num,
|
2019-04-17 06:17:24 +02:00
|
|
|
mpoly,
|
2022-12-17 12:47:43 +11:00
|
|
|
poly_normals,
|
2022-03-28 12:29:47 +11:00
|
|
|
polys_num,
|
2023-01-10 16:12:14 -05:00
|
|
|
sharp_edges,
|
2019-04-17 06:17:24 +02:00
|
|
|
clnors);
|
|
|
|
|
|
2018-12-07 15:45:53 +01:00
|
|
|
MEM_freeN(nos);
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2018-05-07 14:36:00 +02:00
|
|
|
|
2015-02-05 14:49:44 +01:00
|
|
|
static bool is_valid_target(NormalEditModifierData *enmd)
|
2019-04-17 06:17:24 +02:00
|
|
|
{
|
2015-02-05 14:49:44 +01:00
|
|
|
if (enmd->mode == MOD_NORMALEDIT_MODE_RADIAL) {
|
|
|
|
|
return true;
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2020-08-07 12:40:29 +02:00
|
|
|
if ((enmd->mode == MOD_NORMALEDIT_MODE_DIRECTIONAL) && enmd->target) {
|
2015-02-05 14:49:44 +01:00
|
|
|
return true;
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2020-10-26 17:07:58 +11:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool is_valid_target_with_error(const Object *ob, NormalEditModifierData *enmd)
|
|
|
|
|
{
|
|
|
|
|
if (is_valid_target(enmd)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
BKE_modifier_set_error(ob, (ModifierData *)enmd, "Invalid target settings");
|
2015-02-05 14:49:44 +01:00
|
|
|
return false;
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2015-02-05 14:49:44 +01:00
|
|
|
|
2018-12-07 15:45:53 +01:00
|
|
|
static Mesh *normalEditModifier_do(NormalEditModifierData *enmd,
|
2015-02-05 14:49:44 +01:00
|
|
|
const ModifierEvalContext *ctx,
|
|
|
|
|
Object *ob,
|
2018-05-07 14:36:00 +02:00
|
|
|
Mesh *mesh)
|
2019-04-17 06:17:24 +02:00
|
|
|
{
|
2023-01-10 16:12:14 -05:00
|
|
|
using namespace blender;
|
2015-02-05 14:49:44 +01:00
|
|
|
const bool use_invert_vgroup = ((enmd->flag & MOD_NORMALEDIT_INVERT_VGROUP) != 0);
|
2016-09-30 12:15:18 +02:00
|
|
|
const bool use_current_clnors = !((enmd->mix_mode == MOD_NORMALEDIT_MIX_COPY) &&
|
2015-02-05 14:49:44 +01:00
|
|
|
(enmd->mix_factor == 1.0f) && (enmd->defgrp_name[0] == '\0') &&
|
2022-10-03 10:24:06 +11:00
|
|
|
(enmd->mix_limit == float(M_PI)));
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-02-05 14:49:44 +01:00
|
|
|
/* Do not run that modifier at all if autosmooth is disabled! */
|
2020-10-26 17:07:58 +11:00
|
|
|
if (!is_valid_target_with_error(ctx->object, enmd) || mesh->totloop == 0) {
|
2018-05-07 14:36:00 +02:00
|
|
|
return mesh;
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
|
|
|
|
|
2019-05-01 07:40:07 +10:00
|
|
|
/* XXX TODO(Rohan Rathi):
|
|
|
|
|
* Once we fully switch to Mesh evaluation of modifiers,
|
|
|
|
|
* we can expect to get that flag from the COW copy.
|
|
|
|
|
* But for now, it is lost in the DM intermediate step,
|
|
|
|
|
* so we need to directly check orig object's data. */
|
2019-04-17 06:17:24 +02:00
|
|
|
#if 0
|
2019-04-18 07:52:45 +02:00
|
|
|
if (!(mesh->flag & ME_AUTOSMOOTH))
|
2019-04-17 06:17:24 +02:00
|
|
|
#else
|
2019-04-18 07:52:45 +02:00
|
|
|
if (!(((Mesh *)ob->data)->flag & ME_AUTOSMOOTH))
|
2019-04-17 06:17:24 +02:00
|
|
|
#endif
|
2019-04-18 07:52:45 +02:00
|
|
|
{
|
2020-10-26 17:07:58 +11:00
|
|
|
BKE_modifier_set_error(
|
|
|
|
|
ob, (ModifierData *)enmd, "Enable 'Auto Smooth' in Object Data Properties");
|
2019-04-18 07:52:45 +02:00
|
|
|
return mesh;
|
|
|
|
|
}
|
2015-02-05 14:49:44 +01:00
|
|
|
|
2019-04-18 07:52:45 +02:00
|
|
|
Mesh *result;
|
2022-09-25 15:14:13 +10:00
|
|
|
if (BKE_mesh_edges(mesh) == BKE_mesh_edges((Mesh *)ob->data)) {
|
2019-05-01 07:40:07 +10:00
|
|
|
/* We need to duplicate data here, otherwise setting custom normals
|
|
|
|
|
* (which may also affect sharp edges) could
|
2023-02-12 14:37:16 +11:00
|
|
|
* modify original mesh, see #43671. */
|
2022-10-02 11:16:14 -05:00
|
|
|
result = (Mesh *)BKE_id_copy_ex(nullptr, &mesh->id, nullptr, LIB_ID_COPY_LOCALIZE);
|
2019-04-18 07:52:45 +02:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
result = mesh;
|
|
|
|
|
}
|
2015-02-05 14:49:44 +01:00
|
|
|
|
2022-03-28 12:29:47 +11:00
|
|
|
const int verts_num = result->totvert;
|
|
|
|
|
const int edges_num = result->totedge;
|
|
|
|
|
const int loops_num = result->totloop;
|
|
|
|
|
const int polys_num = result->totpoly;
|
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
|
|
|
const float(*positions)[3] = BKE_mesh_vert_positions(result);
|
2023-01-10 16:12:14 -05:00
|
|
|
const MEdge *edges = BKE_mesh_edges(result);
|
2022-09-07 00:06:31 -05:00
|
|
|
const MPoly *polys = BKE_mesh_polys(result);
|
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
|
|
|
MLoop *loops = BKE_mesh_loops_for_write(result);
|
2015-02-05 14:49:44 +01:00
|
|
|
|
2019-04-18 07:52:45 +02:00
|
|
|
int defgrp_index;
|
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;
|
2015-02-05 14:49:44 +01:00
|
|
|
|
2022-12-17 12:47:43 +11:00
|
|
|
float(*loop_normals)[3] = nullptr;
|
2015-02-05 14:49:44 +01:00
|
|
|
|
2019-04-18 07:52:45 +02:00
|
|
|
CustomData *ldata = &result->ldata;
|
2016-02-28 15:48:08 +01:00
|
|
|
|
2022-02-25 17:18:07 -05:00
|
|
|
const float(*vert_normals)[3] = BKE_mesh_vertex_normals_ensure(result);
|
|
|
|
|
const float(*poly_normals)[3] = BKE_mesh_poly_normals_ensure(result);
|
2019-04-18 07:52:45 +02: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>(
|
|
|
|
|
"sharp_edge", ATTR_DOMAIN_EDGE);
|
|
|
|
|
|
2023-01-13 17:21:20 -06:00
|
|
|
short(*clnors)[2] = static_cast<short(*)[2]>(
|
|
|
|
|
CustomData_get_layer_for_write(ldata, CD_CUSTOMLOOPNORMAL, loops_num));
|
2019-04-18 07:52:45 +02:00
|
|
|
if (use_current_clnors) {
|
2022-10-02 11:16:14 -05:00
|
|
|
clnors = static_cast<short(*)[2]>(
|
2023-01-13 17:21:20 -06:00
|
|
|
CustomData_get_layer_for_write(ldata, CD_CUSTOMLOOPNORMAL, loops_num));
|
2022-12-17 12:47:43 +11:00
|
|
|
loop_normals = static_cast<float(*)[3]>(
|
|
|
|
|
MEM_malloc_arrayN(size_t(loops_num), sizeof(*loop_normals), __func__));
|
2019-04-18 07:52:45 +02: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
|
|
|
BKE_mesh_normals_loop_split(positions,
|
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 14:37:58 -06:00
|
|
|
vert_normals,
|
2022-03-28 12:29:47 +11:00
|
|
|
verts_num,
|
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
|
|
|
edges,
|
2022-03-28 12:29:47 +11:00
|
|
|
edges_num,
|
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
|
|
|
loops,
|
2022-12-17 12:47:43 +11:00
|
|
|
loop_normals,
|
2022-03-28 12:29:47 +11:00
|
|
|
loops_num,
|
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
|
|
|
polys,
|
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 14:37:58 -06:00
|
|
|
poly_normals,
|
2022-03-28 12:29:47 +11:00
|
|
|
polys_num,
|
2019-04-18 07:52:45 +02:00
|
|
|
true,
|
|
|
|
|
result->smoothresh,
|
2023-01-10 16:12:14 -05:00
|
|
|
sharp_edges.span.data(),
|
2022-10-02 11:16:14 -05:00
|
|
|
nullptr,
|
2022-11-11 22:56:44 -06:00
|
|
|
nullptr,
|
|
|
|
|
clnors);
|
2019-04-18 07:52:45 +02:00
|
|
|
}
|
2015-02-05 14:49:44 +01:00
|
|
|
|
2022-10-02 11:16:14 -05:00
|
|
|
if (clnors == nullptr) {
|
|
|
|
|
clnors = static_cast<short(*)[2]>(
|
|
|
|
|
CustomData_add_layer(ldata, CD_CUSTOMLOOPNORMAL, CD_SET_DEFAULT, nullptr, loops_num));
|
2019-04-18 07:52:45 +02:00
|
|
|
}
|
2015-02-05 14:49:44 +01:00
|
|
|
|
2019-04-18 07:52:45 +02:00
|
|
|
MOD_get_vgroup(ob, result, enmd->defgrp_name, &dvert, &defgrp_index);
|
|
|
|
|
|
|
|
|
|
if (enmd->mode == MOD_NORMALEDIT_MODE_RADIAL) {
|
|
|
|
|
normalEditModifier_do_radial(enmd,
|
|
|
|
|
ctx,
|
|
|
|
|
ob,
|
|
|
|
|
result,
|
|
|
|
|
clnors,
|
2022-12-17 12:47:43 +11:00
|
|
|
loop_normals,
|
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 14:37:58 -06:00
|
|
|
poly_normals,
|
2019-04-18 07:52:45 +02:00
|
|
|
enmd->mix_mode,
|
|
|
|
|
enmd->mix_factor,
|
|
|
|
|
enmd->mix_limit,
|
|
|
|
|
dvert,
|
|
|
|
|
defgrp_index,
|
|
|
|
|
use_invert_vgroup,
|
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
|
|
|
positions,
|
2022-03-28 12:29:47 +11:00
|
|
|
verts_num,
|
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
|
|
|
edges,
|
2022-03-28 12:29:47 +11:00
|
|
|
edges_num,
|
2023-01-10 16:12:14 -05:00
|
|
|
sharp_edges.span.data(),
|
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
|
|
|
loops,
|
2022-03-28 12:29:47 +11:00
|
|
|
loops_num,
|
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
|
|
|
polys,
|
2022-03-28 12:29:47 +11:00
|
|
|
polys_num);
|
2019-04-18 07:52:45 +02:00
|
|
|
}
|
|
|
|
|
else if (enmd->mode == MOD_NORMALEDIT_MODE_DIRECTIONAL) {
|
|
|
|
|
normalEditModifier_do_directional(enmd,
|
|
|
|
|
ctx,
|
|
|
|
|
ob,
|
|
|
|
|
result,
|
|
|
|
|
clnors,
|
2022-12-17 12:47:43 +11:00
|
|
|
loop_normals,
|
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 14:37:58 -06:00
|
|
|
poly_normals,
|
2019-04-18 07:52:45 +02:00
|
|
|
enmd->mix_mode,
|
|
|
|
|
enmd->mix_factor,
|
|
|
|
|
enmd->mix_limit,
|
|
|
|
|
dvert,
|
|
|
|
|
defgrp_index,
|
|
|
|
|
use_invert_vgroup,
|
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
|
|
|
positions,
|
2022-03-28 12:29:47 +11:00
|
|
|
verts_num,
|
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
|
|
|
edges,
|
2022-03-28 12:29:47 +11:00
|
|
|
edges_num,
|
2023-01-10 16:12:14 -05:00
|
|
|
sharp_edges.span.data(),
|
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
|
|
|
loops,
|
2022-03-28 12:29:47 +11:00
|
|
|
loops_num,
|
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
|
|
|
polys,
|
2022-03-28 12:29:47 +11:00
|
|
|
polys_num);
|
2019-04-18 07:52:45 +02:00
|
|
|
}
|
2015-02-05 14:49:44 +01:00
|
|
|
|
2022-12-17 12:47:43 +11:00
|
|
|
MEM_SAFE_FREE(loop_normals);
|
2020-02-19 12:22:58 +01: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-04-18 07:52:45 +02:00
|
|
|
return result;
|
2015-02-05 14:49:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void initData(ModifierData *md)
|
|
|
|
|
{
|
2016-09-30 12:15:18 +02:00
|
|
|
NormalEditModifierData *enmd = (NormalEditModifierData *)md;
|
2015-02-05 14:49:44 +01:00
|
|
|
|
2020-10-01 09:38:00 -05:00
|
|
|
BLI_assert(MEMCMP_STRUCT_AFTER_IS_ZERO(enmd, modifier));
|
2015-02-05 14:49:44 +01:00
|
|
|
|
2020-10-01 09:38:00 -05:00
|
|
|
MEMCPY_STRUCT_AFTER(enmd, DNA_struct_default_get(NormalEditModifierData), modifier);
|
2015-02-05 14:49:44 +01:00
|
|
|
}
|
|
|
|
|
|
2022-09-14 14:49:40 -05:00
|
|
|
static void requiredDataMask(ModifierData *md, CustomData_MeshMasks *r_cddata_masks)
|
2015-02-05 14:49:44 +01:00
|
|
|
{
|
2016-09-30 12:15:18 +02:00
|
|
|
NormalEditModifierData *enmd = (NormalEditModifierData *)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;
|
2015-02-05 14:49:44 +01:00
|
|
|
|
|
|
|
|
/* Ask for vertexgroups if we need them. */
|
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
|
|
|
if (enmd->defgrp_name[0] != '\0') {
|
|
|
|
|
r_cddata_masks->vmask |= CD_MASK_MDEFORMVERT;
|
2015-02-05 14:49:44 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-03 17:37:25 -05:00
|
|
|
static bool dependsOnNormals(ModifierData * /*md*/)
|
2015-02-05 14:49:44 +01:00
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-01 18:05:23 +02:00
|
|
|
static void foreachIDLink(ModifierData *md, Object *ob, IDWalkFunc walk, void *userData)
|
2015-02-05 14:49:44 +01:00
|
|
|
{
|
2016-09-30 12:15:18 +02:00
|
|
|
NormalEditModifierData *enmd = (NormalEditModifierData *)md;
|
2015-02-05 14:49:44 +01:00
|
|
|
|
2020-10-01 18:05:23 +02:00
|
|
|
walk(userData, ob, (ID **)&enmd->target, IDWALK_CB_NOP);
|
2015-02-05 14:49:44 +01:00
|
|
|
}
|
|
|
|
|
|
2022-10-03 17:37:25 -05:00
|
|
|
static bool isDisabled(const struct Scene * /*scene*/, ModifierData *md, bool /*useRenderParams*/)
|
2015-02-05 14:49:44 +01:00
|
|
|
{
|
2016-09-30 12:15:18 +02:00
|
|
|
NormalEditModifierData *enmd = (NormalEditModifierData *)md;
|
2015-02-05 14:49:44 +01:00
|
|
|
|
2016-09-30 12:15:18 +02:00
|
|
|
return !is_valid_target(enmd);
|
2015-02-05 14:49:44 +01:00
|
|
|
}
|
|
|
|
|
|
2018-02-22 12:54:06 +01:00
|
|
|
static void updateDepsgraph(ModifierData *md, const ModifierUpdateDepsgraphContext *ctx)
|
Depsgraph: New dependency graph integration commit
This commit integrates the work done so far on the new dependency graph system,
where goal was to replace legacy depsgraph with the new one, supporting loads of
neat features like:
- More granular dependency relation nature, which solves issues with fake cycles
in the dependencies.
- Move towards all-animatable, by better integration of drivers into the system.
- Lay down some basis for upcoming copy-on-write, overrides and so on.
The new system is living side-by-side with the previous one and disabled by
default, so nothing will become suddenly broken. The way to enable new depsgraph
is to pass `--new-depsgraph` command line argument.
It's a bit early to consider the system production-ready, there are some TODOs
and issues were discovered during the merge period, they'll be addressed ASAP.
But it's important to merge, because it's the only way to attract artists to
really start testing this system.
There are number of assorted documents related on the design of the new system:
* http://wiki.blender.org/index.php/User:Aligorith/GSoC2013_Depsgraph#Design_Documents
* http://wiki.blender.org/index.php/User:Nazg-gul/DependencyGraph
There are also some user-related information online:
* http://code.blender.org/2015/02/blender-dependency-graph-branch-for-users/
* http://code.blender.org/2015/03/more-dependency-graph-tricks/
Kudos to everyone who was involved into the project:
- Joshua "Aligorith" Leung -- design specification, initial code
- Lukas "lukas_t" Toenne -- integrating code into blender, with further fixes
- Sergey "Sergey" "Sharybin" -- some mocking around, trying to wrap up the
project and so
- Bassam "slikdigit" Kurdali -- stressing the new system, reporting all the
issues and recording/writing documentation.
- Everyone else who i forgot to mention here :)
2015-05-12 15:05:57 +05:00
|
|
|
{
|
2016-09-30 12:15:18 +02:00
|
|
|
NormalEditModifierData *enmd = (NormalEditModifierData *)md;
|
|
|
|
|
if (enmd->target) {
|
2018-02-22 12:54:06 +01:00
|
|
|
DEG_add_object_relation(ctx->node, enmd->target, DEG_OB_COMP_TRANSFORM, "NormalEdit Modifier");
|
2022-08-04 12:11:31 +02:00
|
|
|
DEG_add_depends_on_transform_relation(ctx->node, "NormalEdit Modifier");
|
Depsgraph: New dependency graph integration commit
This commit integrates the work done so far on the new dependency graph system,
where goal was to replace legacy depsgraph with the new one, supporting loads of
neat features like:
- More granular dependency relation nature, which solves issues with fake cycles
in the dependencies.
- Move towards all-animatable, by better integration of drivers into the system.
- Lay down some basis for upcoming copy-on-write, overrides and so on.
The new system is living side-by-side with the previous one and disabled by
default, so nothing will become suddenly broken. The way to enable new depsgraph
is to pass `--new-depsgraph` command line argument.
It's a bit early to consider the system production-ready, there are some TODOs
and issues were discovered during the merge period, they'll be addressed ASAP.
But it's important to merge, because it's the only way to attract artists to
really start testing this system.
There are number of assorted documents related on the design of the new system:
* http://wiki.blender.org/index.php/User:Aligorith/GSoC2013_Depsgraph#Design_Documents
* http://wiki.blender.org/index.php/User:Nazg-gul/DependencyGraph
There are also some user-related information online:
* http://code.blender.org/2015/02/blender-dependency-graph-branch-for-users/
* http://code.blender.org/2015/03/more-dependency-graph-tricks/
Kudos to everyone who was involved into the project:
- Joshua "Aligorith" Leung -- design specification, initial code
- Lukas "lukas_t" Toenne -- integrating code into blender, with further fixes
- Sergey "Sergey" "Sharybin" -- some mocking around, trying to wrap up the
project and so
- Bassam "slikdigit" Kurdali -- stressing the new system, reporting all the
issues and recording/writing documentation.
- Everyone else who i forgot to mention here :)
2015-05-12 15:05:57 +05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-21 13:09:41 +02:00
|
|
|
static Mesh *modifyMesh(ModifierData *md, const ModifierEvalContext *ctx, Mesh *mesh)
|
2015-02-05 14:49:44 +01:00
|
|
|
{
|
2018-12-07 15:45:53 +01:00
|
|
|
return normalEditModifier_do((NormalEditModifierData *)md, ctx, ctx->object, mesh);
|
2015-02-05 14:49:44 +01:00
|
|
|
}
|
|
|
|
|
|
2022-10-03 17:37:25 -05:00
|
|
|
static void panel_draw(const bContext * /*C*/, Panel *panel)
|
2020-06-05 10:41:03 -04:00
|
|
|
{
|
|
|
|
|
uiLayout *col;
|
|
|
|
|
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
|
|
|
|
2020-09-02 14:13:26 -05:00
|
|
|
int mode = RNA_enum_get(ptr, "mode");
|
2020-06-05 10:41:03 -04:00
|
|
|
|
2022-10-02 11:16:14 -05:00
|
|
|
uiItemR(layout, ptr, "mode", UI_ITEM_R_EXPAND, nullptr, ICON_NONE);
|
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
|
|
|
|
|
|
|
|
uiLayoutSetPropSep(layout, true);
|
|
|
|
|
|
2022-10-02 11:16:14 -05:00
|
|
|
uiItemR(layout, ptr, "target", 0, nullptr, ICON_NONE);
|
2020-06-05 10:41:03 -04:00
|
|
|
|
|
|
|
|
col = uiLayoutColumn(layout, false);
|
|
|
|
|
uiLayoutSetActive(col, mode == MOD_NORMALEDIT_MODE_DIRECTIONAL);
|
2022-10-02 11:16:14 -05:00
|
|
|
uiItemR(col, ptr, "use_direction_parallel", 0, nullptr, ICON_NONE);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* This panel could be open by default, but it isn't currently. */
|
2022-10-03 17:37:25 -05:00
|
|
|
static void mix_mode_panel_draw(const bContext * /*C*/, Panel *panel)
|
2020-06-05 10:41:03 -04:00
|
|
|
{
|
|
|
|
|
uiLayout *row;
|
|
|
|
|
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);
|
|
|
|
|
|
2022-10-02 11:16:14 -05:00
|
|
|
uiItemR(layout, ptr, "mix_mode", 0, nullptr, ICON_NONE);
|
|
|
|
|
uiItemR(layout, ptr, "mix_factor", 0, nullptr, ICON_NONE);
|
2020-06-05 10:41:03 -04:00
|
|
|
|
2022-10-02 11:16:14 -05:00
|
|
|
modifier_vgroup_ui(layout, ptr, &ob_ptr, "vertex_group", "invert_vertex_group", nullptr);
|
2020-06-05 10:41:03 -04:00
|
|
|
|
|
|
|
|
row = uiLayoutRow(layout, true);
|
2022-10-02 11:16:14 -05:00
|
|
|
uiItemR(row, ptr, "mix_limit", 0, nullptr, ICON_NONE);
|
2020-06-05 10:41:03 -04:00
|
|
|
uiItemR(row,
|
2020-09-02 14:13:26 -05:00
|
|
|
ptr,
|
2020-06-05 10:41:03 -04:00
|
|
|
"no_polynors_fix",
|
|
|
|
|
0,
|
|
|
|
|
"",
|
2020-09-02 14:13:26 -05:00
|
|
|
(RNA_boolean_get(ptr, "no_polynors_fix") ? ICON_LOCKED : ICON_UNLOCKED));
|
2020-06-05 10:41:03 -04:00
|
|
|
}
|
|
|
|
|
|
2022-10-03 17:37:25 -05:00
|
|
|
static void offset_panel_draw(const bContext * /*C*/, Panel *panel)
|
2020-06-05 10:41:03 -04:00
|
|
|
{
|
|
|
|
|
uiLayout *layout = panel->layout;
|
|
|
|
|
|
2022-10-02 11:16:14 -05:00
|
|
|
PointerRNA *ptr = modifier_panel_get_property_pointers(panel, nullptr);
|
2020-06-05 10:41:03 -04:00
|
|
|
|
2020-09-02 14:13:26 -05:00
|
|
|
int mode = RNA_enum_get(ptr, "mode");
|
|
|
|
|
PointerRNA target_ptr = RNA_pointer_get(ptr, "target");
|
2020-06-05 10:41:03 -04:00
|
|
|
bool needs_object_offset = (mode == MOD_NORMALEDIT_MODE_RADIAL &&
|
|
|
|
|
RNA_pointer_is_null(&target_ptr)) ||
|
|
|
|
|
(mode == MOD_NORMALEDIT_MODE_DIRECTIONAL &&
|
2020-09-02 14:13:26 -05:00
|
|
|
RNA_boolean_get(ptr, "use_direction_parallel"));
|
2020-06-05 10:41:03 -04:00
|
|
|
|
|
|
|
|
uiLayoutSetPropSep(layout, true);
|
|
|
|
|
|
|
|
|
|
uiLayoutSetActive(layout, needs_object_offset);
|
2022-10-02 11:16:14 -05:00
|
|
|
uiItemR(layout, ptr, "offset", 0, nullptr, ICON_NONE);
|
2020-06-05 10:41:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void panelRegister(ARegionType *region_type)
|
|
|
|
|
{
|
|
|
|
|
PanelType *panel_type = modifier_panel_register(
|
|
|
|
|
region_type, eModifierType_NormalEdit, panel_draw);
|
2022-10-02 11:16:14 -05:00
|
|
|
modifier_subpanel_register(region_type, "mix", "Mix", nullptr, mix_mode_panel_draw, panel_type);
|
|
|
|
|
modifier_subpanel_register(
|
|
|
|
|
region_type, "offset", "Offset", nullptr, offset_panel_draw, panel_type);
|
2020-06-05 10:41:03 -04:00
|
|
|
}
|
|
|
|
|
|
2015-02-05 14:49:44 +01:00
|
|
|
ModifierTypeInfo modifierType_NormalEdit = {
|
2023-01-16 12:41:11 +11:00
|
|
|
/*name*/ N_("NormalEdit"),
|
|
|
|
|
/*structName*/ "NormalEditModifierData",
|
|
|
|
|
/*structSize*/ sizeof(NormalEditModifierData),
|
|
|
|
|
/*srna*/ &RNA_NormalEditModifier,
|
|
|
|
|
/*type*/ eModifierTypeType_Constructive,
|
|
|
|
|
/*flags*/ eModifierTypeFlag_AcceptsMesh | eModifierTypeFlag_SupportsMapping |
|
2015-02-05 14:49:44 +01:00
|
|
|
eModifierTypeFlag_SupportsEditmode | eModifierTypeFlag_EnableInEditmode,
|
2023-01-16 12:41:11 +11:00
|
|
|
/*icon*/ ICON_MOD_NORMALEDIT,
|
|
|
|
|
|
|
|
|
|
/*copyData*/ BKE_modifier_copydata_generic,
|
|
|
|
|
|
|
|
|
|
/*deformVerts*/ nullptr,
|
|
|
|
|
/*deformMatrices*/ nullptr,
|
|
|
|
|
/*deformVertsEM*/ nullptr,
|
|
|
|
|
/*deformMatricesEM*/ nullptr,
|
|
|
|
|
/*modifyMesh*/ modifyMesh,
|
|
|
|
|
/*modifyGeometrySet*/ nullptr,
|
|
|
|
|
|
|
|
|
|
/*initData*/ initData,
|
|
|
|
|
/*requiredDataMask*/ requiredDataMask,
|
|
|
|
|
/*freeData*/ nullptr,
|
|
|
|
|
/*isDisabled*/ isDisabled,
|
|
|
|
|
/*updateDepsgraph*/ updateDepsgraph,
|
|
|
|
|
/*dependsOnTime*/ nullptr,
|
|
|
|
|
/*dependsOnNormals*/ dependsOnNormals,
|
|
|
|
|
/*foreachIDLink*/ foreachIDLink,
|
|
|
|
|
/*foreachTexLink*/ nullptr,
|
|
|
|
|
/*freeRuntimeData*/ nullptr,
|
|
|
|
|
/*panelRegister*/ panelRegister,
|
|
|
|
|
/*blendWrite*/ nullptr,
|
|
|
|
|
/*blendRead*/ nullptr,
|
2015-02-05 14:49:44 +01:00
|
|
|
};
|