2023-08-16 00:20:26 +10:00
|
|
|
/* SPDX-FileCopyrightText: 2009 Blender Authors
|
2023-05-31 16:19:06 +02:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
2009-07-01 22:25:49 +00:00
|
|
|
|
2019-02-18 08:08:12 +11:00
|
|
|
/** \file
|
|
|
|
|
* \ingroup edmesh
|
2011-02-27 20:29:51 +00:00
|
|
|
*/
|
|
|
|
|
|
2009-07-01 22:25:49 +00:00
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
|
|
|
|
|
|
#include "DNA_object_types.h"
|
|
|
|
|
#include "DNA_scene_types.h"
|
2009-11-10 19:54:59 +00:00
|
|
|
#include "DNA_view3d_types.h"
|
2009-07-01 22:25:49 +00:00
|
|
|
|
2022-05-15 21:59:10 +02:00
|
|
|
#include "BLI_array.hh"
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "BLI_utildefines.h"
|
2011-01-07 18:36:47 +00: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
|
|
|
#include "BKE_attribute.hh"
|
2023-11-16 11:41:55 +01:00
|
|
|
#include "BKE_context.hh"
|
|
|
|
|
#include "BKE_customdata.hh"
|
|
|
|
|
#include "BKE_editmesh.hh"
|
2024-01-30 14:42:07 -05:00
|
|
|
#include "BKE_key.hh"
|
2023-03-12 22:29:15 +01:00
|
|
|
#include "BKE_mesh.hh"
|
2023-08-02 22:14:18 +02:00
|
|
|
#include "BKE_mesh_runtime.hh"
|
2024-02-10 18:34:29 +01:00
|
|
|
#include "BKE_report.hh"
|
2009-07-01 22:25:49 +00:00
|
|
|
|
2023-09-22 03:18:17 +02:00
|
|
|
#include "DEG_depsgraph.hh"
|
2017-06-08 10:14:53 +02:00
|
|
|
|
2022-03-14 16:54:46 +01:00
|
|
|
#include "RNA_prototypes.h"
|
2009-07-01 22:25:49 +00:00
|
|
|
|
2023-08-04 23:11:22 +02:00
|
|
|
#include "WM_api.hh"
|
|
|
|
|
#include "WM_types.hh"
|
2009-07-01 22:25:49 +00:00
|
|
|
|
2024-02-09 18:59:42 +01:00
|
|
|
#include "BLT_translation.hh"
|
Mesh: Move UV layers to generic attributes
Currently the `MLoopUV` struct stores UV coordinates and flags related
to editing UV maps in the UV editor. This patch changes the coordinates
to use the generic 2D vector type, and moves the flags into three
separate boolean attributes. This follows the design in T95965, with
the ultimate intention of simplifying code and improving performance.
Importantly, the change allows exporters and renderers to use UVs
"touched" by geometry nodes, which only creates generic attributes.
It also allows geometry nodes to create "proper" UV maps from scratch,
though only with the Store Named Attribute node for now.
The new design considers any 2D vector attribute on the corner domain
to be a UV map. In the future, they might be distinguished from regular
2D vectors with attribute metadata, which may be helpful because they
are often interpolated differently.
Most of the code changes deal with passing around UV BMesh custom data
offsets and tracking the boolean "sublayers". The boolean layers are
use the following prefixes for attribute names: vert selection: `.vs.`,
edge selection: `.es.`, pinning: `.pn.`. Currently these are short to
avoid using up the maximum length of attribute names. To accommodate
for these 4 extra characters, the name length limit is enlarged to 68
bytes, while the maximum user settable name length is still 64 bytes.
Unfortunately Python/RNA API access to the UV flag data becomes slower.
Accessing the boolean layers directly is be better for performance in
general.
Like the other mesh SoA refactors, backward and forward compatibility
aren't affected, and won't be changed until 4.0. We pay for that by
making mesh reading and writing more expensive with conversions.
Resolves T85962
Differential Revision: https://developer.blender.org/D14365
2023-01-10 00:47:04 -05:00
|
|
|
|
2023-08-05 02:57:52 +02:00
|
|
|
#include "ED_mesh.hh"
|
|
|
|
|
#include "ED_object.hh"
|
|
|
|
|
#include "ED_paint.hh"
|
2023-08-04 23:11:22 +02:00
|
|
|
#include "ED_screen.hh"
|
2009-07-01 22:25:49 +00:00
|
|
|
|
2022-12-13 18:39:36 -06:00
|
|
|
#include "GEO_mesh_split_edges.hh"
|
|
|
|
|
|
2024-01-24 11:30:39 -05:00
|
|
|
#include "mesh_intern.hh" /* own include */
|
2012-12-19 04:49:32 +00:00
|
|
|
|
2022-05-15 21:59:10 +02:00
|
|
|
using blender::Array;
|
Mesh: Move UV layers to generic attributes
Currently the `MLoopUV` struct stores UV coordinates and flags related
to editing UV maps in the UV editor. This patch changes the coordinates
to use the generic 2D vector type, and moves the flags into three
separate boolean attributes. This follows the design in T95965, with
the ultimate intention of simplifying code and improving performance.
Importantly, the change allows exporters and renderers to use UVs
"touched" by geometry nodes, which only creates generic attributes.
It also allows geometry nodes to create "proper" UV maps from scratch,
though only with the Store Named Attribute node for now.
The new design considers any 2D vector attribute on the corner domain
to be a UV map. In the future, they might be distinguished from regular
2D vectors with attribute metadata, which may be helpful because they
are often interpolated differently.
Most of the code changes deal with passing around UV BMesh custom data
offsets and tracking the boolean "sublayers". The boolean layers are
use the following prefixes for attribute names: vert selection: `.vs.`,
edge selection: `.es.`, pinning: `.pn.`. Currently these are short to
avoid using up the maximum length of attribute names. To accommodate
for these 4 extra characters, the name length limit is enlarged to 68
bytes, while the maximum user settable name length is still 64 bytes.
Unfortunately Python/RNA API access to the UV flag data becomes slower.
Accessing the boolean layers directly is be better for performance in
general.
Like the other mesh SoA refactors, backward and forward compatibility
aren't affected, and won't be changed until 4.0. We pay for that by
making mesh reading and writing more expensive with conversions.
Resolves T85962
Differential Revision: https://developer.blender.org/D14365
2023-01-10 00:47:04 -05:00
|
|
|
using blender::float2;
|
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 blender::float3;
|
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
|
|
|
using blender::MutableSpan;
|
|
|
|
|
using blender::Span;
|
2022-05-15 21:59:10 +02:00
|
|
|
|
2023-12-08 16:40:06 -05:00
|
|
|
static CustomData *mesh_customdata_get_type(Mesh *mesh, const char htype, int *r_tot)
|
2012-09-21 03:41:59 +00:00
|
|
|
{
|
|
|
|
|
CustomData *data;
|
2024-03-21 23:18:49 +01:00
|
|
|
BMesh *bm = (mesh->runtime->edit_mesh) ? mesh->runtime->edit_mesh->bm : nullptr;
|
2012-09-21 03:41:59 +00:00
|
|
|
int tot;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-09-21 03:41:59 +00:00
|
|
|
switch (htype) {
|
|
|
|
|
case BM_VERT:
|
|
|
|
|
if (bm) {
|
|
|
|
|
data = &bm->vdata;
|
|
|
|
|
tot = bm->totvert;
|
|
|
|
|
}
|
|
|
|
|
else {
|
2023-12-08 16:40:06 -05:00
|
|
|
data = &mesh->vert_data;
|
2023-12-20 02:21:48 +01:00
|
|
|
tot = mesh->verts_num;
|
2012-09-21 03:41:59 +00:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case BM_EDGE:
|
|
|
|
|
if (bm) {
|
|
|
|
|
data = &bm->edata;
|
|
|
|
|
tot = bm->totedge;
|
|
|
|
|
}
|
|
|
|
|
else {
|
2023-12-08 16:40:06 -05:00
|
|
|
data = &mesh->edge_data;
|
2023-12-20 02:21:48 +01:00
|
|
|
tot = mesh->edges_num;
|
2012-09-21 03:41:59 +00:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case BM_LOOP:
|
|
|
|
|
if (bm) {
|
|
|
|
|
data = &bm->ldata;
|
|
|
|
|
tot = bm->totloop;
|
|
|
|
|
}
|
|
|
|
|
else {
|
2023-12-19 20:38:59 -05:00
|
|
|
data = &mesh->corner_data;
|
2023-12-20 02:21:48 +01:00
|
|
|
tot = mesh->corners_num;
|
2012-09-21 03:41:59 +00:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case BM_FACE:
|
|
|
|
|
if (bm) {
|
|
|
|
|
data = &bm->pdata;
|
|
|
|
|
tot = bm->totface;
|
|
|
|
|
}
|
|
|
|
|
else {
|
2023-12-08 16:40:06 -05:00
|
|
|
data = &mesh->face_data;
|
|
|
|
|
tot = mesh->faces_num;
|
2012-09-21 03:41:59 +00:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
BLI_assert(0);
|
|
|
|
|
tot = 0;
|
2022-05-15 20:41:11 +02:00
|
|
|
data = nullptr;
|
2013-07-21 08:16:37 +00:00
|
|
|
break;
|
2012-09-21 03:41:59 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-25 21:15:52 +02:00
|
|
|
if (r_tot) {
|
|
|
|
|
*r_tot = tot;
|
|
|
|
|
}
|
2012-09-21 03:41:59 +00:00
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-10 08:08:18 +00:00
|
|
|
static void mesh_uv_reset_array(float **fuv, const int len)
|
2011-11-17 05:03:07 +00:00
|
|
|
{
|
2013-05-10 08:08:18 +00:00
|
|
|
if (len == 3) {
|
|
|
|
|
fuv[0][0] = 0.0;
|
|
|
|
|
fuv[0][1] = 0.0;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-05-10 08:08:18 +00:00
|
|
|
fuv[1][0] = 1.0;
|
|
|
|
|
fuv[1][1] = 0.0;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-05-10 08:08:18 +00:00
|
|
|
fuv[2][0] = 1.0;
|
|
|
|
|
fuv[2][1] = 1.0;
|
|
|
|
|
}
|
|
|
|
|
else if (len == 4) {
|
|
|
|
|
fuv[0][0] = 0.0;
|
|
|
|
|
fuv[0][1] = 0.0;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-05-10 08:08:18 +00:00
|
|
|
fuv[1][0] = 1.0;
|
|
|
|
|
fuv[1][1] = 0.0;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-05-10 08:08:18 +00:00
|
|
|
fuv[2][0] = 1.0;
|
|
|
|
|
fuv[2][1] = 1.0;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-05-10 08:08:18 +00:00
|
|
|
fuv[3][0] = 0.0;
|
|
|
|
|
fuv[3][1] = 1.0;
|
2021-06-26 21:35:18 +10:00
|
|
|
/* Make sure we ignore 2-sided faces. */
|
2013-05-10 08:08:18 +00:00
|
|
|
}
|
|
|
|
|
else if (len > 2) {
|
2022-09-25 18:33:28 +10:00
|
|
|
float fac = 0.0f, dfac = 1.0f / float(len);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-09-25 18:33:28 +10:00
|
|
|
dfac *= float(M_PI) * 2.0f;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2020-09-09 18:41:07 +02:00
|
|
|
for (int i = 0; i < len; i++) {
|
2013-05-10 08:08:18 +00:00
|
|
|
fuv[i][0] = 0.5f * sinf(fac) + 0.5f;
|
|
|
|
|
fuv[i][1] = 0.5f * cosf(fac) + 0.5f;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-05-10 08:08:18 +00:00
|
|
|
fac += dfac;
|
2011-11-17 05:03:07 +00:00
|
|
|
}
|
|
|
|
|
}
|
2013-05-10 08:08:18 +00:00
|
|
|
}
|
2011-11-17 05:03:07 +00:00
|
|
|
|
2013-05-10 08:08:18 +00:00
|
|
|
static void mesh_uv_reset_bmface(BMFace *f, const int cd_loop_uv_offset)
|
|
|
|
|
{
|
2022-05-15 21:59:10 +02:00
|
|
|
Array<float *, BM_DEFAULT_NGON_STACK_SIZE> fuv(f->len);
|
2013-05-10 08:08:18 +00:00
|
|
|
BMIter liter;
|
|
|
|
|
BMLoop *l;
|
|
|
|
|
int i;
|
2011-11-17 05:03:07 +00:00
|
|
|
|
2019-04-21 04:40:16 +10:00
|
|
|
BM_ITER_ELEM_INDEX (l, &liter, f, BM_LOOPS_OF_FACE, i) {
|
Mesh: Move UV layers to generic attributes
Currently the `MLoopUV` struct stores UV coordinates and flags related
to editing UV maps in the UV editor. This patch changes the coordinates
to use the generic 2D vector type, and moves the flags into three
separate boolean attributes. This follows the design in T95965, with
the ultimate intention of simplifying code and improving performance.
Importantly, the change allows exporters and renderers to use UVs
"touched" by geometry nodes, which only creates generic attributes.
It also allows geometry nodes to create "proper" UV maps from scratch,
though only with the Store Named Attribute node for now.
The new design considers any 2D vector attribute on the corner domain
to be a UV map. In the future, they might be distinguished from regular
2D vectors with attribute metadata, which may be helpful because they
are often interpolated differently.
Most of the code changes deal with passing around UV BMesh custom data
offsets and tracking the boolean "sublayers". The boolean layers are
use the following prefixes for attribute names: vert selection: `.vs.`,
edge selection: `.es.`, pinning: `.pn.`. Currently these are short to
avoid using up the maximum length of attribute names. To accommodate
for these 4 extra characters, the name length limit is enlarged to 68
bytes, while the maximum user settable name length is still 64 bytes.
Unfortunately Python/RNA API access to the UV flag data becomes slower.
Accessing the boolean layers directly is be better for performance in
general.
Like the other mesh SoA refactors, backward and forward compatibility
aren't affected, and won't be changed until 4.0. We pay for that by
making mesh reading and writing more expensive with conversions.
Resolves T85962
Differential Revision: https://developer.blender.org/D14365
2023-01-10 00:47:04 -05:00
|
|
|
fuv[i] = ((float *)BM_ELEM_CD_GET_VOID_P(l, cd_loop_uv_offset));
|
2013-05-10 08:08:18 +00:00
|
|
|
}
|
2011-11-17 05:03:07 +00:00
|
|
|
|
2022-05-15 21:59:10 +02:00
|
|
|
mesh_uv_reset_array(fuv.data(), f->len);
|
2013-05-10 08:08:18 +00:00
|
|
|
}
|
2011-11-17 05:03:07 +00:00
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
static void mesh_uv_reset_mface(const blender::IndexRange face, float2 *mloopuv)
|
2013-05-10 08:08:18 +00:00
|
|
|
{
|
2023-07-24 22:06:55 +02:00
|
|
|
Array<float *, BM_DEFAULT_NGON_STACK_SIZE> fuv(face.size());
|
2011-11-17 05:03:07 +00:00
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
for (int i = 0; i < face.size(); i++) {
|
|
|
|
|
fuv[i] = mloopuv[face[i]];
|
2011-11-17 05:03:07 +00:00
|
|
|
}
|
|
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
mesh_uv_reset_array(fuv.data(), face.size());
|
2013-05-10 08:08:18 +00:00
|
|
|
}
|
2011-11-17 05:03:07 +00:00
|
|
|
|
2023-12-08 16:40:06 -05:00
|
|
|
void ED_mesh_uv_loop_reset_ex(Mesh *mesh, const int layernum)
|
2013-05-10 08:08:18 +00:00
|
|
|
{
|
2024-03-21 23:18:49 +01:00
|
|
|
BMEditMesh *em = mesh->runtime->edit_mesh;
|
2011-11-17 05:03:07 +00:00
|
|
|
|
2013-05-10 08:08:18 +00:00
|
|
|
if (em) {
|
|
|
|
|
/* Collect BMesh UVs */
|
Mesh: Move UV layers to generic attributes
Currently the `MLoopUV` struct stores UV coordinates and flags related
to editing UV maps in the UV editor. This patch changes the coordinates
to use the generic 2D vector type, and moves the flags into three
separate boolean attributes. This follows the design in T95965, with
the ultimate intention of simplifying code and improving performance.
Importantly, the change allows exporters and renderers to use UVs
"touched" by geometry nodes, which only creates generic attributes.
It also allows geometry nodes to create "proper" UV maps from scratch,
though only with the Store Named Attribute node for now.
The new design considers any 2D vector attribute on the corner domain
to be a UV map. In the future, they might be distinguished from regular
2D vectors with attribute metadata, which may be helpful because they
are often interpolated differently.
Most of the code changes deal with passing around UV BMesh custom data
offsets and tracking the boolean "sublayers". The boolean layers are
use the following prefixes for attribute names: vert selection: `.vs.`,
edge selection: `.es.`, pinning: `.pn.`. Currently these are short to
avoid using up the maximum length of attribute names. To accommodate
for these 4 extra characters, the name length limit is enlarged to 68
bytes, while the maximum user settable name length is still 64 bytes.
Unfortunately Python/RNA API access to the UV flag data becomes slower.
Accessing the boolean layers directly is be better for performance in
general.
Like the other mesh SoA refactors, backward and forward compatibility
aren't affected, and won't be changed until 4.0. We pay for that by
making mesh reading and writing more expensive with conversions.
Resolves T85962
Differential Revision: https://developer.blender.org/D14365
2023-01-10 00:47:04 -05:00
|
|
|
const int cd_loop_uv_offset = CustomData_get_n_offset(
|
|
|
|
|
&em->bm->ldata, CD_PROP_FLOAT2, layernum);
|
2011-11-17 05:03:07 +00:00
|
|
|
|
2013-05-10 08:08:18 +00:00
|
|
|
BMFace *efa;
|
|
|
|
|
BMIter iter;
|
2011-11-17 05:03:07 +00:00
|
|
|
|
2022-08-19 14:19:13 +12:00
|
|
|
BLI_assert(cd_loop_uv_offset >= 0);
|
2011-11-17 05:03:07 +00:00
|
|
|
|
2013-05-10 08:08:18 +00:00
|
|
|
BM_ITER_MESH (efa, &iter, em->bm, BM_FACES_OF_MESH) {
|
2019-04-22 09:19:45 +10:00
|
|
|
if (!BM_elem_flag_test(efa, BM_ELEM_SELECT)) {
|
2013-05-10 08:08:18 +00:00
|
|
|
continue;
|
2019-04-22 09:19:45 +10:00
|
|
|
}
|
2011-11-17 05:03:07 +00:00
|
|
|
|
2013-05-10 08:08:18 +00:00
|
|
|
mesh_uv_reset_bmface(efa, cd_loop_uv_offset);
|
|
|
|
|
}
|
2011-11-17 05:03:07 +00:00
|
|
|
}
|
2013-05-10 08:08:18 +00:00
|
|
|
else {
|
|
|
|
|
/* Collect Mesh UVs */
|
2023-12-19 20:38:59 -05:00
|
|
|
BLI_assert(CustomData_has_layer(&mesh->corner_data, CD_PROP_FLOAT2));
|
2023-12-08 16:40:06 -05:00
|
|
|
float2 *mloopuv = static_cast<float2 *>(CustomData_get_layer_n_for_write(
|
2023-12-19 20:38:59 -05:00
|
|
|
&mesh->corner_data, CD_PROP_FLOAT2, layernum, mesh->corners_num));
|
2011-11-17 05:03:07 +00:00
|
|
|
|
2023-12-08 16:40:06 -05:00
|
|
|
const blender::OffsetIndices polys = mesh->faces();
|
2023-02-23 10:39:51 -05:00
|
|
|
for (const int i : polys.index_range()) {
|
Mesh: Replace MPoly struct with offset indices
Implements #95967.
Currently the `MPoly` struct is 12 bytes, and stores the index of a
face's first corner and the number of corners/verts/edges. Polygons
and corners are always created in order by Blender, meaning each
face's corners will be after the previous face's corners. We can take
advantage of this fact and eliminate the redundancy in mesh face
storage by only storing a single integer corner offset for each face.
The size of the face is then encoded by the offset of the next face.
The size of a single integer is 4 bytes, so this reduces memory
usage by 3 times.
The same method is used for `CurvesGeometry`, so Blender already has
an abstraction to simplify using these offsets called `OffsetIndices`.
This class is used to easily retrieve a range of corner indices for
each face. This also gives the opportunity for sharing some logic with
curves.
Another benefit of the change is that the offsets and sizes stored in
`MPoly` can no longer disagree with each other. Storing faces in the
order of their corners can simplify some code too.
Face/polygon variables now use the `IndexRange` type, which comes with
quite a few utilities that can simplify code.
Some:
- The offset integer array has to be one longer than the face count to
avoid a branch for every face, which means the data is no longer part
of the mesh's `CustomData`.
- We lose the ability to "reference" an original mesh's offset array
until more reusable CoW from #104478 is committed. That will be added
in a separate commit.
- Since they aren't part of `CustomData`, poly offsets often have to be
copied manually.
- To simplify using `OffsetIndices` in many places, some functions and
structs in headers were moved to only compile in C++.
- All meshes created by Blender use the same order for faces and face
corners, but just in case, meshes with mismatched order are fixed by
versioning code.
- `MeshPolygon.totloop` is no longer editable in RNA. This API break is
necessary here unfortunately. It should be worth it in 3.6, since
that's the best way to allow loading meshes from 4.0, which is
important for an LTS version.
Pull Request: https://projects.blender.org/blender/blender/pulls/105938
2023-04-04 20:39:28 +02:00
|
|
|
mesh_uv_reset_mface(polys[i], mloopuv);
|
2013-05-10 08:08:18 +00:00
|
|
|
}
|
|
|
|
|
}
|
2011-11-17 05:03:07 +00:00
|
|
|
|
2023-12-08 16:40:06 -05:00
|
|
|
DEG_id_tag_update(&mesh->id, 0);
|
2011-11-17 05:03:07 +00:00
|
|
|
}
|
|
|
|
|
|
2023-12-08 16:40:06 -05:00
|
|
|
void ED_mesh_uv_loop_reset(bContext *C, Mesh *mesh)
|
2012-03-26 02:39:05 +00:00
|
|
|
{
|
|
|
|
|
/* could be ldata or pdata */
|
2023-12-08 16:40:06 -05:00
|
|
|
CustomData *ldata = mesh_customdata_get_type(mesh, BM_LOOP, nullptr);
|
Mesh: Move UV layers to generic attributes
Currently the `MLoopUV` struct stores UV coordinates and flags related
to editing UV maps in the UV editor. This patch changes the coordinates
to use the generic 2D vector type, and moves the flags into three
separate boolean attributes. This follows the design in T95965, with
the ultimate intention of simplifying code and improving performance.
Importantly, the change allows exporters and renderers to use UVs
"touched" by geometry nodes, which only creates generic attributes.
It also allows geometry nodes to create "proper" UV maps from scratch,
though only with the Store Named Attribute node for now.
The new design considers any 2D vector attribute on the corner domain
to be a UV map. In the future, they might be distinguished from regular
2D vectors with attribute metadata, which may be helpful because they
are often interpolated differently.
Most of the code changes deal with passing around UV BMesh custom data
offsets and tracking the boolean "sublayers". The boolean layers are
use the following prefixes for attribute names: vert selection: `.vs.`,
edge selection: `.es.`, pinning: `.pn.`. Currently these are short to
avoid using up the maximum length of attribute names. To accommodate
for these 4 extra characters, the name length limit is enlarged to 68
bytes, while the maximum user settable name length is still 64 bytes.
Unfortunately Python/RNA API access to the UV flag data becomes slower.
Accessing the boolean layers directly is be better for performance in
general.
Like the other mesh SoA refactors, backward and forward compatibility
aren't affected, and won't be changed until 4.0. We pay for that by
making mesh reading and writing more expensive with conversions.
Resolves T85962
Differential Revision: https://developer.blender.org/D14365
2023-01-10 00:47:04 -05:00
|
|
|
const int layernum = CustomData_get_active_layer(ldata, CD_PROP_FLOAT2);
|
2023-12-08 16:40:06 -05:00
|
|
|
ED_mesh_uv_loop_reset_ex(mesh, layernum);
|
2018-06-04 09:31:30 +02:00
|
|
|
|
2023-12-08 16:40:06 -05:00
|
|
|
WM_event_add_notifier(C, NC_GEOM | ND_DATA, mesh);
|
2012-03-26 02:39:05 +00:00
|
|
|
}
|
|
|
|
|
|
2022-05-23 12:43:28 +10:00
|
|
|
int ED_mesh_uv_add(
|
2023-12-08 16:40:06 -05:00
|
|
|
Mesh *mesh, const char *name, const bool active_set, const bool do_init, ReportList *reports)
|
2009-09-28 14:28:45 +00:00
|
|
|
{
|
2021-12-09 00:55:11 +11:00
|
|
|
/* NOTE: keep in sync with #ED_mesh_color_add. */
|
|
|
|
|
|
2011-07-25 09:53:36 +00:00
|
|
|
BMEditMesh *em;
|
2012-05-22 12:03:56 +00:00
|
|
|
int layernum_dst;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
Mesh: Move UV layers to generic attributes
Currently the `MLoopUV` struct stores UV coordinates and flags related
to editing UV maps in the UV editor. This patch changes the coordinates
to use the generic 2D vector type, and moves the flags into three
separate boolean attributes. This follows the design in T95965, with
the ultimate intention of simplifying code and improving performance.
Importantly, the change allows exporters and renderers to use UVs
"touched" by geometry nodes, which only creates generic attributes.
It also allows geometry nodes to create "proper" UV maps from scratch,
though only with the Store Named Attribute node for now.
The new design considers any 2D vector attribute on the corner domain
to be a UV map. In the future, they might be distinguished from regular
2D vectors with attribute metadata, which may be helpful because they
are often interpolated differently.
Most of the code changes deal with passing around UV BMesh custom data
offsets and tracking the boolean "sublayers". The boolean layers are
use the following prefixes for attribute names: vert selection: `.vs.`,
edge selection: `.es.`, pinning: `.pn.`. Currently these are short to
avoid using up the maximum length of attribute names. To accommodate
for these 4 extra characters, the name length limit is enlarged to 68
bytes, while the maximum user settable name length is still 64 bytes.
Unfortunately Python/RNA API access to the UV flag data becomes slower.
Accessing the boolean layers directly is be better for performance in
general.
Like the other mesh SoA refactors, backward and forward compatibility
aren't affected, and won't be changed until 4.0. We pay for that by
making mesh reading and writing more expensive with conversions.
Resolves T85962
Differential Revision: https://developer.blender.org/D14365
2023-01-10 00:47:04 -05:00
|
|
|
if (!name) {
|
|
|
|
|
name = DATA_("UVMap");
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-29 13:35:35 -05:00
|
|
|
const std::string unique_name = BKE_id_attribute_calc_unique_name(mesh->id, name);
|
2013-03-19 23:17:44 +00:00
|
|
|
bool is_init = false;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2024-03-21 23:18:49 +01:00
|
|
|
if (mesh->runtime->edit_mesh) {
|
|
|
|
|
em = mesh->runtime->edit_mesh;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
Mesh: Move UV layers to generic attributes
Currently the `MLoopUV` struct stores UV coordinates and flags related
to editing UV maps in the UV editor. This patch changes the coordinates
to use the generic 2D vector type, and moves the flags into three
separate boolean attributes. This follows the design in T95965, with
the ultimate intention of simplifying code and improving performance.
Importantly, the change allows exporters and renderers to use UVs
"touched" by geometry nodes, which only creates generic attributes.
It also allows geometry nodes to create "proper" UV maps from scratch,
though only with the Store Named Attribute node for now.
The new design considers any 2D vector attribute on the corner domain
to be a UV map. In the future, they might be distinguished from regular
2D vectors with attribute metadata, which may be helpful because they
are often interpolated differently.
Most of the code changes deal with passing around UV BMesh custom data
offsets and tracking the boolean "sublayers". The boolean layers are
use the following prefixes for attribute names: vert selection: `.vs.`,
edge selection: `.es.`, pinning: `.pn.`. Currently these are short to
avoid using up the maximum length of attribute names. To accommodate
for these 4 extra characters, the name length limit is enlarged to 68
bytes, while the maximum user settable name length is still 64 bytes.
Unfortunately Python/RNA API access to the UV flag data becomes slower.
Accessing the boolean layers directly is be better for performance in
general.
Like the other mesh SoA refactors, backward and forward compatibility
aren't affected, and won't be changed until 4.0. We pay for that by
making mesh reading and writing more expensive with conversions.
Resolves T85962
Differential Revision: https://developer.blender.org/D14365
2023-01-10 00:47:04 -05:00
|
|
|
layernum_dst = CustomData_number_of_layers(&em->bm->ldata, CD_PROP_FLOAT2);
|
2019-04-22 09:19:45 +10:00
|
|
|
if (layernum_dst >= MAX_MTFACE) {
|
2021-12-03 17:01:10 +11:00
|
|
|
BKE_reportf(reports, RPT_WARNING, "Cannot add more than %i UV maps", MAX_MTFACE);
|
2012-02-01 18:25:13 +00:00
|
|
|
return -1;
|
2019-04-22 09:19:45 +10:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-12-29 13:35:35 -05:00
|
|
|
BM_data_layer_add_named(em->bm, &em->bm->ldata, CD_PROP_FLOAT2, unique_name.c_str());
|
Mesh: Move UV layers to generic attributes
Currently the `MLoopUV` struct stores UV coordinates and flags related
to editing UV maps in the UV editor. This patch changes the coordinates
to use the generic 2D vector type, and moves the flags into three
separate boolean attributes. This follows the design in T95965, with
the ultimate intention of simplifying code and improving performance.
Importantly, the change allows exporters and renderers to use UVs
"touched" by geometry nodes, which only creates generic attributes.
It also allows geometry nodes to create "proper" UV maps from scratch,
though only with the Store Named Attribute node for now.
The new design considers any 2D vector attribute on the corner domain
to be a UV map. In the future, they might be distinguished from regular
2D vectors with attribute metadata, which may be helpful because they
are often interpolated differently.
Most of the code changes deal with passing around UV BMesh custom data
offsets and tracking the boolean "sublayers". The boolean layers are
use the following prefixes for attribute names: vert selection: `.vs.`,
edge selection: `.es.`, pinning: `.pn.`. Currently these are short to
avoid using up the maximum length of attribute names. To accommodate
for these 4 extra characters, the name length limit is enlarged to 68
bytes, while the maximum user settable name length is still 64 bytes.
Unfortunately Python/RNA API access to the UV flag data becomes slower.
Accessing the boolean layers directly is be better for performance in
general.
Like the other mesh SoA refactors, backward and forward compatibility
aren't affected, and won't be changed until 4.0. We pay for that by
making mesh reading and writing more expensive with conversions.
Resolves T85962
Differential Revision: https://developer.blender.org/D14365
2023-01-10 00:47:04 -05:00
|
|
|
BM_uv_map_ensure_select_and_pin_attrs(em->bm);
|
2012-03-26 05:28:00 +00:00
|
|
|
/* copy data from active UV */
|
2019-03-05 21:34:48 +01:00
|
|
|
if (layernum_dst && do_init) {
|
Mesh: Move UV layers to generic attributes
Currently the `MLoopUV` struct stores UV coordinates and flags related
to editing UV maps in the UV editor. This patch changes the coordinates
to use the generic 2D vector type, and moves the flags into three
separate boolean attributes. This follows the design in T95965, with
the ultimate intention of simplifying code and improving performance.
Importantly, the change allows exporters and renderers to use UVs
"touched" by geometry nodes, which only creates generic attributes.
It also allows geometry nodes to create "proper" UV maps from scratch,
though only with the Store Named Attribute node for now.
The new design considers any 2D vector attribute on the corner domain
to be a UV map. In the future, they might be distinguished from regular
2D vectors with attribute metadata, which may be helpful because they
are often interpolated differently.
Most of the code changes deal with passing around UV BMesh custom data
offsets and tracking the boolean "sublayers". The boolean layers are
use the following prefixes for attribute names: vert selection: `.vs.`,
edge selection: `.es.`, pinning: `.pn.`. Currently these are short to
avoid using up the maximum length of attribute names. To accommodate
for these 4 extra characters, the name length limit is enlarged to 68
bytes, while the maximum user settable name length is still 64 bytes.
Unfortunately Python/RNA API access to the UV flag data becomes slower.
Accessing the boolean layers directly is be better for performance in
general.
Like the other mesh SoA refactors, backward and forward compatibility
aren't affected, and won't be changed until 4.0. We pay for that by
making mesh reading and writing more expensive with conversions.
Resolves T85962
Differential Revision: https://developer.blender.org/D14365
2023-01-10 00:47:04 -05:00
|
|
|
const int layernum_src = CustomData_get_active_layer(&em->bm->ldata, CD_PROP_FLOAT2);
|
|
|
|
|
BM_data_layer_copy(em->bm, &em->bm->ldata, CD_PROP_FLOAT2, layernum_src, layernum_dst);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-03-19 23:17:44 +00:00
|
|
|
is_init = true;
|
2012-03-26 05:28:00 +00:00
|
|
|
}
|
2012-05-22 12:03:56 +00:00
|
|
|
if (active_set || layernum_dst == 0) {
|
Mesh: Move UV layers to generic attributes
Currently the `MLoopUV` struct stores UV coordinates and flags related
to editing UV maps in the UV editor. This patch changes the coordinates
to use the generic 2D vector type, and moves the flags into three
separate boolean attributes. This follows the design in T95965, with
the ultimate intention of simplifying code and improving performance.
Importantly, the change allows exporters and renderers to use UVs
"touched" by geometry nodes, which only creates generic attributes.
It also allows geometry nodes to create "proper" UV maps from scratch,
though only with the Store Named Attribute node for now.
The new design considers any 2D vector attribute on the corner domain
to be a UV map. In the future, they might be distinguished from regular
2D vectors with attribute metadata, which may be helpful because they
are often interpolated differently.
Most of the code changes deal with passing around UV BMesh custom data
offsets and tracking the boolean "sublayers". The boolean layers are
use the following prefixes for attribute names: vert selection: `.vs.`,
edge selection: `.es.`, pinning: `.pn.`. Currently these are short to
avoid using up the maximum length of attribute names. To accommodate
for these 4 extra characters, the name length limit is enlarged to 68
bytes, while the maximum user settable name length is still 64 bytes.
Unfortunately Python/RNA API access to the UV flag data becomes slower.
Accessing the boolean layers directly is be better for performance in
general.
Like the other mesh SoA refactors, backward and forward compatibility
aren't affected, and won't be changed until 4.0. We pay for that by
making mesh reading and writing more expensive with conversions.
Resolves T85962
Differential Revision: https://developer.blender.org/D14365
2023-01-10 00:47:04 -05:00
|
|
|
CustomData_set_layer_active(&em->bm->ldata, CD_PROP_FLOAT2, layernum_dst);
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2011-11-20 16:21:13 +00:00
|
|
|
}
|
2009-09-28 14:28:45 +00:00
|
|
|
else {
|
2023-12-19 20:38:59 -05:00
|
|
|
layernum_dst = CustomData_number_of_layers(&mesh->corner_data, CD_PROP_FLOAT2);
|
2019-04-22 09:19:45 +10:00
|
|
|
if (layernum_dst >= MAX_MTFACE) {
|
2021-12-03 17:01:10 +11:00
|
|
|
BKE_reportf(reports, RPT_WARNING, "Cannot add more than %i UV maps", MAX_MTFACE);
|
2012-02-01 18:25:13 +00:00
|
|
|
return -1;
|
2019-04-22 09:19:45 +10:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-12-19 20:38:59 -05:00
|
|
|
if (CustomData_has_layer(&mesh->corner_data, CD_PROP_FLOAT2) && do_init) {
|
2023-03-14 15:30:26 +01:00
|
|
|
CustomData_add_layer_named_with_data(
|
2023-12-19 20:38:59 -05:00
|
|
|
&mesh->corner_data,
|
2023-03-14 15:30:26 +01:00
|
|
|
CD_PROP_FLOAT2,
|
2023-12-19 20:38:59 -05:00
|
|
|
MEM_dupallocN(CustomData_get_layer(&mesh->corner_data, CD_PROP_FLOAT2)),
|
2023-12-20 02:21:48 +01:00
|
|
|
mesh->corners_num,
|
2024-02-08 16:56:42 +01:00
|
|
|
unique_name,
|
2023-04-13 14:57:57 +02:00
|
|
|
nullptr);
|
Mesh: Move UV layers to generic attributes
Currently the `MLoopUV` struct stores UV coordinates and flags related
to editing UV maps in the UV editor. This patch changes the coordinates
to use the generic 2D vector type, and moves the flags into three
separate boolean attributes. This follows the design in T95965, with
the ultimate intention of simplifying code and improving performance.
Importantly, the change allows exporters and renderers to use UVs
"touched" by geometry nodes, which only creates generic attributes.
It also allows geometry nodes to create "proper" UV maps from scratch,
though only with the Store Named Attribute node for now.
The new design considers any 2D vector attribute on the corner domain
to be a UV map. In the future, they might be distinguished from regular
2D vectors with attribute metadata, which may be helpful because they
are often interpolated differently.
Most of the code changes deal with passing around UV BMesh custom data
offsets and tracking the boolean "sublayers". The boolean layers are
use the following prefixes for attribute names: vert selection: `.vs.`,
edge selection: `.es.`, pinning: `.pn.`. Currently these are short to
avoid using up the maximum length of attribute names. To accommodate
for these 4 extra characters, the name length limit is enlarged to 68
bytes, while the maximum user settable name length is still 64 bytes.
Unfortunately Python/RNA API access to the UV flag data becomes slower.
Accessing the boolean layers directly is be better for performance in
general.
Like the other mesh SoA refactors, backward and forward compatibility
aren't affected, and won't be changed until 4.0. We pay for that by
making mesh reading and writing more expensive with conversions.
Resolves T85962
Differential Revision: https://developer.blender.org/D14365
2023-01-10 00:47:04 -05:00
|
|
|
|
2013-03-19 23:17:44 +00:00
|
|
|
is_init = true;
|
2012-03-24 06:38:07 +00:00
|
|
|
}
|
|
|
|
|
else {
|
2024-02-08 16:56:42 +01:00
|
|
|
CustomData_add_layer_named(
|
|
|
|
|
&mesh->corner_data, CD_PROP_FLOAT2, CD_SET_DEFAULT, mesh->corners_num, unique_name);
|
2011-07-25 09:53:36 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-05-22 12:03:56 +00:00
|
|
|
if (active_set || layernum_dst == 0) {
|
2023-12-19 20:38:59 -05:00
|
|
|
CustomData_set_layer_active(&mesh->corner_data, CD_PROP_FLOAT2, layernum_dst);
|
2011-07-25 09:53:36 +00:00
|
|
|
}
|
2009-09-28 14:28:45 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-03-26 05:28:00 +00:00
|
|
|
/* don't overwrite our copied coords */
|
2019-03-05 21:34:48 +01:00
|
|
|
if (!is_init && do_init) {
|
2023-12-08 16:40:06 -05:00
|
|
|
ED_mesh_uv_loop_reset_ex(mesh, layernum_dst);
|
2012-03-26 05:28:00 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-12-08 16:40:06 -05:00
|
|
|
DEG_id_tag_update(&mesh->id, 0);
|
|
|
|
|
WM_main_add_notifier(NC_GEOM | ND_DATA, mesh);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-05-22 12:03:56 +00:00
|
|
|
return layernum_dst;
|
2009-09-28 14:28:45 +00:00
|
|
|
}
|
|
|
|
|
|
Mesh: Move UV layers to generic attributes
Currently the `MLoopUV` struct stores UV coordinates and flags related
to editing UV maps in the UV editor. This patch changes the coordinates
to use the generic 2D vector type, and moves the flags into three
separate boolean attributes. This follows the design in T95965, with
the ultimate intention of simplifying code and improving performance.
Importantly, the change allows exporters and renderers to use UVs
"touched" by geometry nodes, which only creates generic attributes.
It also allows geometry nodes to create "proper" UV maps from scratch,
though only with the Store Named Attribute node for now.
The new design considers any 2D vector attribute on the corner domain
to be a UV map. In the future, they might be distinguished from regular
2D vectors with attribute metadata, which may be helpful because they
are often interpolated differently.
Most of the code changes deal with passing around UV BMesh custom data
offsets and tracking the boolean "sublayers". The boolean layers are
use the following prefixes for attribute names: vert selection: `.vs.`,
edge selection: `.es.`, pinning: `.pn.`. Currently these are short to
avoid using up the maximum length of attribute names. To accommodate
for these 4 extra characters, the name length limit is enlarged to 68
bytes, while the maximum user settable name length is still 64 bytes.
Unfortunately Python/RNA API access to the UV flag data becomes slower.
Accessing the boolean layers directly is be better for performance in
general.
Like the other mesh SoA refactors, backward and forward compatibility
aren't affected, and won't be changed until 4.0. We pay for that by
making mesh reading and writing more expensive with conversions.
Resolves T85962
Differential Revision: https://developer.blender.org/D14365
2023-01-10 00:47:04 -05:00
|
|
|
static const bool *mesh_loop_boolean_custom_data_get_by_name(const Mesh &mesh, const char *name)
|
|
|
|
|
{
|
2023-07-25 21:15:52 +02:00
|
|
|
return static_cast<const bool *>(
|
2023-12-19 20:38:59 -05:00
|
|
|
CustomData_get_layer_named(&mesh.corner_data, CD_PROP_BOOL, name));
|
Mesh: Move UV layers to generic attributes
Currently the `MLoopUV` struct stores UV coordinates and flags related
to editing UV maps in the UV editor. This patch changes the coordinates
to use the generic 2D vector type, and moves the flags into three
separate boolean attributes. This follows the design in T95965, with
the ultimate intention of simplifying code and improving performance.
Importantly, the change allows exporters and renderers to use UVs
"touched" by geometry nodes, which only creates generic attributes.
It also allows geometry nodes to create "proper" UV maps from scratch,
though only with the Store Named Attribute node for now.
The new design considers any 2D vector attribute on the corner domain
to be a UV map. In the future, they might be distinguished from regular
2D vectors with attribute metadata, which may be helpful because they
are often interpolated differently.
Most of the code changes deal with passing around UV BMesh custom data
offsets and tracking the boolean "sublayers". The boolean layers are
use the following prefixes for attribute names: vert selection: `.vs.`,
edge selection: `.es.`, pinning: `.pn.`. Currently these are short to
avoid using up the maximum length of attribute names. To accommodate
for these 4 extra characters, the name length limit is enlarged to 68
bytes, while the maximum user settable name length is still 64 bytes.
Unfortunately Python/RNA API access to the UV flag data becomes slower.
Accessing the boolean layers directly is be better for performance in
general.
Like the other mesh SoA refactors, backward and forward compatibility
aren't affected, and won't be changed until 4.0. We pay for that by
making mesh reading and writing more expensive with conversions.
Resolves T85962
Differential Revision: https://developer.blender.org/D14365
2023-01-10 00:47:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const bool *ED_mesh_uv_map_vert_select_layer_get(const Mesh *mesh, const int uv_index)
|
|
|
|
|
{
|
|
|
|
|
using namespace blender::bke;
|
|
|
|
|
char buffer[MAX_CUSTOMDATA_LAYER_NAME];
|
2023-12-19 20:38:59 -05:00
|
|
|
const char *uv_name = CustomData_get_layer_name(&mesh->corner_data, CD_PROP_FLOAT2, uv_index);
|
Mesh: Move UV layers to generic attributes
Currently the `MLoopUV` struct stores UV coordinates and flags related
to editing UV maps in the UV editor. This patch changes the coordinates
to use the generic 2D vector type, and moves the flags into three
separate boolean attributes. This follows the design in T95965, with
the ultimate intention of simplifying code and improving performance.
Importantly, the change allows exporters and renderers to use UVs
"touched" by geometry nodes, which only creates generic attributes.
It also allows geometry nodes to create "proper" UV maps from scratch,
though only with the Store Named Attribute node for now.
The new design considers any 2D vector attribute on the corner domain
to be a UV map. In the future, they might be distinguished from regular
2D vectors with attribute metadata, which may be helpful because they
are often interpolated differently.
Most of the code changes deal with passing around UV BMesh custom data
offsets and tracking the boolean "sublayers". The boolean layers are
use the following prefixes for attribute names: vert selection: `.vs.`,
edge selection: `.es.`, pinning: `.pn.`. Currently these are short to
avoid using up the maximum length of attribute names. To accommodate
for these 4 extra characters, the name length limit is enlarged to 68
bytes, while the maximum user settable name length is still 64 bytes.
Unfortunately Python/RNA API access to the UV flag data becomes slower.
Accessing the boolean layers directly is be better for performance in
general.
Like the other mesh SoA refactors, backward and forward compatibility
aren't affected, and won't be changed until 4.0. We pay for that by
making mesh reading and writing more expensive with conversions.
Resolves T85962
Differential Revision: https://developer.blender.org/D14365
2023-01-10 00:47:04 -05:00
|
|
|
return mesh_loop_boolean_custom_data_get_by_name(
|
|
|
|
|
*mesh, BKE_uv_map_vert_select_name_get(uv_name, buffer));
|
|
|
|
|
}
|
|
|
|
|
const bool *ED_mesh_uv_map_edge_select_layer_get(const Mesh *mesh, const int uv_index)
|
|
|
|
|
{
|
2023-03-29 14:16:31 +11:00
|
|
|
/* UV map edge selections are stored on face corners (loops) and not on edges
|
|
|
|
|
* because we need selections per face edge, even when the edge is split in UV space. */
|
|
|
|
|
|
Mesh: Move UV layers to generic attributes
Currently the `MLoopUV` struct stores UV coordinates and flags related
to editing UV maps in the UV editor. This patch changes the coordinates
to use the generic 2D vector type, and moves the flags into three
separate boolean attributes. This follows the design in T95965, with
the ultimate intention of simplifying code and improving performance.
Importantly, the change allows exporters and renderers to use UVs
"touched" by geometry nodes, which only creates generic attributes.
It also allows geometry nodes to create "proper" UV maps from scratch,
though only with the Store Named Attribute node for now.
The new design considers any 2D vector attribute on the corner domain
to be a UV map. In the future, they might be distinguished from regular
2D vectors with attribute metadata, which may be helpful because they
are often interpolated differently.
Most of the code changes deal with passing around UV BMesh custom data
offsets and tracking the boolean "sublayers". The boolean layers are
use the following prefixes for attribute names: vert selection: `.vs.`,
edge selection: `.es.`, pinning: `.pn.`. Currently these are short to
avoid using up the maximum length of attribute names. To accommodate
for these 4 extra characters, the name length limit is enlarged to 68
bytes, while the maximum user settable name length is still 64 bytes.
Unfortunately Python/RNA API access to the UV flag data becomes slower.
Accessing the boolean layers directly is be better for performance in
general.
Like the other mesh SoA refactors, backward and forward compatibility
aren't affected, and won't be changed until 4.0. We pay for that by
making mesh reading and writing more expensive with conversions.
Resolves T85962
Differential Revision: https://developer.blender.org/D14365
2023-01-10 00:47:04 -05:00
|
|
|
using namespace blender::bke;
|
|
|
|
|
char buffer[MAX_CUSTOMDATA_LAYER_NAME];
|
2023-12-19 20:38:59 -05:00
|
|
|
const char *uv_name = CustomData_get_layer_name(&mesh->corner_data, CD_PROP_FLOAT2, uv_index);
|
Mesh: Move UV layers to generic attributes
Currently the `MLoopUV` struct stores UV coordinates and flags related
to editing UV maps in the UV editor. This patch changes the coordinates
to use the generic 2D vector type, and moves the flags into three
separate boolean attributes. This follows the design in T95965, with
the ultimate intention of simplifying code and improving performance.
Importantly, the change allows exporters and renderers to use UVs
"touched" by geometry nodes, which only creates generic attributes.
It also allows geometry nodes to create "proper" UV maps from scratch,
though only with the Store Named Attribute node for now.
The new design considers any 2D vector attribute on the corner domain
to be a UV map. In the future, they might be distinguished from regular
2D vectors with attribute metadata, which may be helpful because they
are often interpolated differently.
Most of the code changes deal with passing around UV BMesh custom data
offsets and tracking the boolean "sublayers". The boolean layers are
use the following prefixes for attribute names: vert selection: `.vs.`,
edge selection: `.es.`, pinning: `.pn.`. Currently these are short to
avoid using up the maximum length of attribute names. To accommodate
for these 4 extra characters, the name length limit is enlarged to 68
bytes, while the maximum user settable name length is still 64 bytes.
Unfortunately Python/RNA API access to the UV flag data becomes slower.
Accessing the boolean layers directly is be better for performance in
general.
Like the other mesh SoA refactors, backward and forward compatibility
aren't affected, and won't be changed until 4.0. We pay for that by
making mesh reading and writing more expensive with conversions.
Resolves T85962
Differential Revision: https://developer.blender.org/D14365
2023-01-10 00:47:04 -05:00
|
|
|
return mesh_loop_boolean_custom_data_get_by_name(
|
|
|
|
|
*mesh, BKE_uv_map_edge_select_name_get(uv_name, buffer));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const bool *ED_mesh_uv_map_pin_layer_get(const Mesh *mesh, const int uv_index)
|
|
|
|
|
{
|
|
|
|
|
using namespace blender::bke;
|
|
|
|
|
char buffer[MAX_CUSTOMDATA_LAYER_NAME];
|
2023-12-19 20:38:59 -05:00
|
|
|
const char *uv_name = CustomData_get_layer_name(&mesh->corner_data, CD_PROP_FLOAT2, uv_index);
|
Mesh: Move UV layers to generic attributes
Currently the `MLoopUV` struct stores UV coordinates and flags related
to editing UV maps in the UV editor. This patch changes the coordinates
to use the generic 2D vector type, and moves the flags into three
separate boolean attributes. This follows the design in T95965, with
the ultimate intention of simplifying code and improving performance.
Importantly, the change allows exporters and renderers to use UVs
"touched" by geometry nodes, which only creates generic attributes.
It also allows geometry nodes to create "proper" UV maps from scratch,
though only with the Store Named Attribute node for now.
The new design considers any 2D vector attribute on the corner domain
to be a UV map. In the future, they might be distinguished from regular
2D vectors with attribute metadata, which may be helpful because they
are often interpolated differently.
Most of the code changes deal with passing around UV BMesh custom data
offsets and tracking the boolean "sublayers". The boolean layers are
use the following prefixes for attribute names: vert selection: `.vs.`,
edge selection: `.es.`, pinning: `.pn.`. Currently these are short to
avoid using up the maximum length of attribute names. To accommodate
for these 4 extra characters, the name length limit is enlarged to 68
bytes, while the maximum user settable name length is still 64 bytes.
Unfortunately Python/RNA API access to the UV flag data becomes slower.
Accessing the boolean layers directly is be better for performance in
general.
Like the other mesh SoA refactors, backward and forward compatibility
aren't affected, and won't be changed until 4.0. We pay for that by
making mesh reading and writing more expensive with conversions.
Resolves T85962
Differential Revision: https://developer.blender.org/D14365
2023-01-10 00:47:04 -05:00
|
|
|
return mesh_loop_boolean_custom_data_get_by_name(*mesh,
|
|
|
|
|
BKE_uv_map_pin_name_get(uv_name, buffer));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool *ensure_corner_boolean_attribute(Mesh &mesh, const blender::StringRefNull name)
|
|
|
|
|
{
|
2023-07-25 21:15:52 +02:00
|
|
|
bool *data = static_cast<bool *>(CustomData_get_layer_named_for_write(
|
2024-02-08 16:56:42 +01:00
|
|
|
&mesh.corner_data, CD_PROP_BOOL, name, mesh.corners_num));
|
Mesh: Move UV layers to generic attributes
Currently the `MLoopUV` struct stores UV coordinates and flags related
to editing UV maps in the UV editor. This patch changes the coordinates
to use the generic 2D vector type, and moves the flags into three
separate boolean attributes. This follows the design in T95965, with
the ultimate intention of simplifying code and improving performance.
Importantly, the change allows exporters and renderers to use UVs
"touched" by geometry nodes, which only creates generic attributes.
It also allows geometry nodes to create "proper" UV maps from scratch,
though only with the Store Named Attribute node for now.
The new design considers any 2D vector attribute on the corner domain
to be a UV map. In the future, they might be distinguished from regular
2D vectors with attribute metadata, which may be helpful because they
are often interpolated differently.
Most of the code changes deal with passing around UV BMesh custom data
offsets and tracking the boolean "sublayers". The boolean layers are
use the following prefixes for attribute names: vert selection: `.vs.`,
edge selection: `.es.`, pinning: `.pn.`. Currently these are short to
avoid using up the maximum length of attribute names. To accommodate
for these 4 extra characters, the name length limit is enlarged to 68
bytes, while the maximum user settable name length is still 64 bytes.
Unfortunately Python/RNA API access to the UV flag data becomes slower.
Accessing the boolean layers directly is be better for performance in
general.
Like the other mesh SoA refactors, backward and forward compatibility
aren't affected, and won't be changed until 4.0. We pay for that by
making mesh reading and writing more expensive with conversions.
Resolves T85962
Differential Revision: https://developer.blender.org/D14365
2023-01-10 00:47:04 -05:00
|
|
|
if (!data) {
|
|
|
|
|
data = static_cast<bool *>(CustomData_add_layer_named(
|
2024-02-08 16:56:42 +01:00
|
|
|
&mesh.corner_data, CD_PROP_BOOL, CD_SET_DEFAULT, mesh.faces_num, name));
|
Mesh: Move UV layers to generic attributes
Currently the `MLoopUV` struct stores UV coordinates and flags related
to editing UV maps in the UV editor. This patch changes the coordinates
to use the generic 2D vector type, and moves the flags into three
separate boolean attributes. This follows the design in T95965, with
the ultimate intention of simplifying code and improving performance.
Importantly, the change allows exporters and renderers to use UVs
"touched" by geometry nodes, which only creates generic attributes.
It also allows geometry nodes to create "proper" UV maps from scratch,
though only with the Store Named Attribute node for now.
The new design considers any 2D vector attribute on the corner domain
to be a UV map. In the future, they might be distinguished from regular
2D vectors with attribute metadata, which may be helpful because they
are often interpolated differently.
Most of the code changes deal with passing around UV BMesh custom data
offsets and tracking the boolean "sublayers". The boolean layers are
use the following prefixes for attribute names: vert selection: `.vs.`,
edge selection: `.es.`, pinning: `.pn.`. Currently these are short to
avoid using up the maximum length of attribute names. To accommodate
for these 4 extra characters, the name length limit is enlarged to 68
bytes, while the maximum user settable name length is still 64 bytes.
Unfortunately Python/RNA API access to the UV flag data becomes slower.
Accessing the boolean layers directly is be better for performance in
general.
Like the other mesh SoA refactors, backward and forward compatibility
aren't affected, and won't be changed until 4.0. We pay for that by
making mesh reading and writing more expensive with conversions.
Resolves T85962
Differential Revision: https://developer.blender.org/D14365
2023-01-10 00:47:04 -05:00
|
|
|
}
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool *ED_mesh_uv_map_vert_select_layer_ensure(Mesh *mesh, const int uv_index)
|
|
|
|
|
{
|
|
|
|
|
using namespace blender::bke;
|
|
|
|
|
char buffer[MAX_CUSTOMDATA_LAYER_NAME];
|
2023-12-19 20:38:59 -05:00
|
|
|
const char *uv_name = CustomData_get_layer_name(&mesh->corner_data, CD_PROP_FLOAT2, uv_index);
|
Mesh: Move UV layers to generic attributes
Currently the `MLoopUV` struct stores UV coordinates and flags related
to editing UV maps in the UV editor. This patch changes the coordinates
to use the generic 2D vector type, and moves the flags into three
separate boolean attributes. This follows the design in T95965, with
the ultimate intention of simplifying code and improving performance.
Importantly, the change allows exporters and renderers to use UVs
"touched" by geometry nodes, which only creates generic attributes.
It also allows geometry nodes to create "proper" UV maps from scratch,
though only with the Store Named Attribute node for now.
The new design considers any 2D vector attribute on the corner domain
to be a UV map. In the future, they might be distinguished from regular
2D vectors with attribute metadata, which may be helpful because they
are often interpolated differently.
Most of the code changes deal with passing around UV BMesh custom data
offsets and tracking the boolean "sublayers". The boolean layers are
use the following prefixes for attribute names: vert selection: `.vs.`,
edge selection: `.es.`, pinning: `.pn.`. Currently these are short to
avoid using up the maximum length of attribute names. To accommodate
for these 4 extra characters, the name length limit is enlarged to 68
bytes, while the maximum user settable name length is still 64 bytes.
Unfortunately Python/RNA API access to the UV flag data becomes slower.
Accessing the boolean layers directly is be better for performance in
general.
Like the other mesh SoA refactors, backward and forward compatibility
aren't affected, and won't be changed until 4.0. We pay for that by
making mesh reading and writing more expensive with conversions.
Resolves T85962
Differential Revision: https://developer.blender.org/D14365
2023-01-10 00:47:04 -05:00
|
|
|
return ensure_corner_boolean_attribute(*mesh, BKE_uv_map_vert_select_name_get(uv_name, buffer));
|
|
|
|
|
}
|
|
|
|
|
bool *ED_mesh_uv_map_edge_select_layer_ensure(Mesh *mesh, const int uv_index)
|
|
|
|
|
{
|
|
|
|
|
using namespace blender::bke;
|
|
|
|
|
char buffer[MAX_CUSTOMDATA_LAYER_NAME];
|
2023-12-19 20:38:59 -05:00
|
|
|
const char *uv_name = CustomData_get_layer_name(&mesh->corner_data, CD_PROP_FLOAT2, uv_index);
|
Mesh: Move UV layers to generic attributes
Currently the `MLoopUV` struct stores UV coordinates and flags related
to editing UV maps in the UV editor. This patch changes the coordinates
to use the generic 2D vector type, and moves the flags into three
separate boolean attributes. This follows the design in T95965, with
the ultimate intention of simplifying code and improving performance.
Importantly, the change allows exporters and renderers to use UVs
"touched" by geometry nodes, which only creates generic attributes.
It also allows geometry nodes to create "proper" UV maps from scratch,
though only with the Store Named Attribute node for now.
The new design considers any 2D vector attribute on the corner domain
to be a UV map. In the future, they might be distinguished from regular
2D vectors with attribute metadata, which may be helpful because they
are often interpolated differently.
Most of the code changes deal with passing around UV BMesh custom data
offsets and tracking the boolean "sublayers". The boolean layers are
use the following prefixes for attribute names: vert selection: `.vs.`,
edge selection: `.es.`, pinning: `.pn.`. Currently these are short to
avoid using up the maximum length of attribute names. To accommodate
for these 4 extra characters, the name length limit is enlarged to 68
bytes, while the maximum user settable name length is still 64 bytes.
Unfortunately Python/RNA API access to the UV flag data becomes slower.
Accessing the boolean layers directly is be better for performance in
general.
Like the other mesh SoA refactors, backward and forward compatibility
aren't affected, and won't be changed until 4.0. We pay for that by
making mesh reading and writing more expensive with conversions.
Resolves T85962
Differential Revision: https://developer.blender.org/D14365
2023-01-10 00:47:04 -05:00
|
|
|
return ensure_corner_boolean_attribute(*mesh, BKE_uv_map_edge_select_name_get(uv_name, buffer));
|
|
|
|
|
}
|
|
|
|
|
bool *ED_mesh_uv_map_pin_layer_ensure(Mesh *mesh, const int uv_index)
|
|
|
|
|
{
|
|
|
|
|
using namespace blender::bke;
|
|
|
|
|
char buffer[MAX_CUSTOMDATA_LAYER_NAME];
|
2023-12-19 20:38:59 -05:00
|
|
|
const char *uv_name = CustomData_get_layer_name(&mesh->corner_data, CD_PROP_FLOAT2, uv_index);
|
Mesh: Move UV layers to generic attributes
Currently the `MLoopUV` struct stores UV coordinates and flags related
to editing UV maps in the UV editor. This patch changes the coordinates
to use the generic 2D vector type, and moves the flags into three
separate boolean attributes. This follows the design in T95965, with
the ultimate intention of simplifying code and improving performance.
Importantly, the change allows exporters and renderers to use UVs
"touched" by geometry nodes, which only creates generic attributes.
It also allows geometry nodes to create "proper" UV maps from scratch,
though only with the Store Named Attribute node for now.
The new design considers any 2D vector attribute on the corner domain
to be a UV map. In the future, they might be distinguished from regular
2D vectors with attribute metadata, which may be helpful because they
are often interpolated differently.
Most of the code changes deal with passing around UV BMesh custom data
offsets and tracking the boolean "sublayers". The boolean layers are
use the following prefixes for attribute names: vert selection: `.vs.`,
edge selection: `.es.`, pinning: `.pn.`. Currently these are short to
avoid using up the maximum length of attribute names. To accommodate
for these 4 extra characters, the name length limit is enlarged to 68
bytes, while the maximum user settable name length is still 64 bytes.
Unfortunately Python/RNA API access to the UV flag data becomes slower.
Accessing the boolean layers directly is be better for performance in
general.
Like the other mesh SoA refactors, backward and forward compatibility
aren't affected, and won't be changed until 4.0. We pay for that by
making mesh reading and writing more expensive with conversions.
Resolves T85962
Differential Revision: https://developer.blender.org/D14365
2023-01-10 00:47:04 -05:00
|
|
|
return ensure_corner_boolean_attribute(*mesh, BKE_uv_map_pin_name_get(uv_name, buffer));
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-08 16:40:06 -05:00
|
|
|
void ED_mesh_uv_ensure(Mesh *mesh, const char *name)
|
2014-10-31 14:37:36 +01:00
|
|
|
{
|
|
|
|
|
BMEditMesh *em;
|
|
|
|
|
int layernum_dst;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2024-03-21 23:18:49 +01:00
|
|
|
if (mesh->runtime->edit_mesh) {
|
|
|
|
|
em = mesh->runtime->edit_mesh;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
Mesh: Move UV layers to generic attributes
Currently the `MLoopUV` struct stores UV coordinates and flags related
to editing UV maps in the UV editor. This patch changes the coordinates
to use the generic 2D vector type, and moves the flags into three
separate boolean attributes. This follows the design in T95965, with
the ultimate intention of simplifying code and improving performance.
Importantly, the change allows exporters and renderers to use UVs
"touched" by geometry nodes, which only creates generic attributes.
It also allows geometry nodes to create "proper" UV maps from scratch,
though only with the Store Named Attribute node for now.
The new design considers any 2D vector attribute on the corner domain
to be a UV map. In the future, they might be distinguished from regular
2D vectors with attribute metadata, which may be helpful because they
are often interpolated differently.
Most of the code changes deal with passing around UV BMesh custom data
offsets and tracking the boolean "sublayers". The boolean layers are
use the following prefixes for attribute names: vert selection: `.vs.`,
edge selection: `.es.`, pinning: `.pn.`. Currently these are short to
avoid using up the maximum length of attribute names. To accommodate
for these 4 extra characters, the name length limit is enlarged to 68
bytes, while the maximum user settable name length is still 64 bytes.
Unfortunately Python/RNA API access to the UV flag data becomes slower.
Accessing the boolean layers directly is be better for performance in
general.
Like the other mesh SoA refactors, backward and forward compatibility
aren't affected, and won't be changed until 4.0. We pay for that by
making mesh reading and writing more expensive with conversions.
Resolves T85962
Differential Revision: https://developer.blender.org/D14365
2023-01-10 00:47:04 -05:00
|
|
|
layernum_dst = CustomData_number_of_layers(&em->bm->ldata, CD_PROP_FLOAT2);
|
2019-04-22 09:19:45 +10:00
|
|
|
if (layernum_dst == 0) {
|
2023-12-08 16:40:06 -05:00
|
|
|
ED_mesh_uv_add(mesh, name, true, true, nullptr);
|
2019-04-22 09:19:45 +10:00
|
|
|
}
|
2014-10-31 14:37:36 +01:00
|
|
|
}
|
|
|
|
|
else {
|
2023-12-19 20:38:59 -05:00
|
|
|
layernum_dst = CustomData_number_of_layers(&mesh->corner_data, CD_PROP_FLOAT2);
|
2019-04-22 09:19:45 +10:00
|
|
|
if (layernum_dst == 0) {
|
2023-12-08 16:40:06 -05:00
|
|
|
ED_mesh_uv_add(mesh, name, true, true, nullptr);
|
2019-04-22 09:19:45 +10:00
|
|
|
}
|
2014-10-31 14:37:36 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-15 14:18:57 -06:00
|
|
|
int ED_mesh_color_add(
|
2023-12-08 16:40:06 -05:00
|
|
|
Mesh *mesh, const char *name, const bool active_set, const bool do_init, ReportList *reports)
|
2009-07-01 22:25:49 +00:00
|
|
|
{
|
2023-12-20 13:13:16 -05:00
|
|
|
using namespace blender;
|
2022-12-15 14:18:57 -06:00
|
|
|
/* If no name is supplied, provide a backwards compatible default. */
|
|
|
|
|
if (!name) {
|
|
|
|
|
name = "Col";
|
2009-07-01 22:25:49 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-12-15 14:18:57 -06:00
|
|
|
CustomDataLayer *layer = BKE_id_attribute_new(
|
2023-12-20 13:13:16 -05:00
|
|
|
&mesh->id, name, CD_PROP_BYTE_COLOR, bke::AttrDomain::Corner, reports);
|
2022-12-15 14:18:57 -06:00
|
|
|
|
|
|
|
|
if (do_init) {
|
2023-12-08 16:40:06 -05:00
|
|
|
const char *active_name = mesh->active_color_attribute;
|
|
|
|
|
if (const CustomDataLayer *active_layer = BKE_id_attributes_color_find(&mesh->id, active_name))
|
|
|
|
|
{
|
2024-03-21 23:18:49 +01:00
|
|
|
if (const BMEditMesh *em = mesh->runtime->edit_mesh) {
|
2022-12-15 14:18:57 -06:00
|
|
|
BMesh &bm = *em->bm;
|
|
|
|
|
const int src_i = CustomData_get_named_layer(&bm.ldata, CD_PROP_BYTE_COLOR, active_name);
|
|
|
|
|
const int dst_i = CustomData_get_named_layer(&bm.ldata, CD_PROP_BYTE_COLOR, layer->name);
|
|
|
|
|
BM_data_layer_copy(&bm, &bm.ldata, CD_PROP_BYTE_COLOR, src_i, dst_i);
|
|
|
|
|
}
|
|
|
|
|
else {
|
2023-12-20 02:21:48 +01:00
|
|
|
memcpy(
|
|
|
|
|
layer->data, active_layer->data, CustomData_get_elem_size(layer) * mesh->corners_num);
|
2022-12-15 14:18:57 -06:00
|
|
|
}
|
2011-11-17 05:03:07 +00:00
|
|
|
}
|
2022-12-15 14:18:57 -06:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-12-15 14:18:57 -06:00
|
|
|
if (active_set) {
|
2023-12-08 16:40:06 -05:00
|
|
|
BKE_id_attributes_active_color_set(&mesh->id, layer->name);
|
2009-07-01 22:25:49 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-12-08 16:40:06 -05:00
|
|
|
DEG_id_tag_update(&mesh->id, 0);
|
|
|
|
|
WM_main_add_notifier(NC_GEOM | ND_DATA, mesh);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-12-15 14:18:57 -06:00
|
|
|
int dummy;
|
2023-12-08 16:40:06 -05:00
|
|
|
const CustomData *data = mesh_customdata_get_type(mesh, BM_LOOP, &dummy);
|
2022-12-15 14:18:57 -06:00
|
|
|
return CustomData_get_named_layer(data, CD_PROP_BYTE_COLOR, layer->name);
|
2009-09-28 14:28:45 +00:00
|
|
|
}
|
|
|
|
|
|
2023-12-08 16:40:06 -05:00
|
|
|
bool ED_mesh_color_ensure(Mesh *mesh, const char *name)
|
2017-09-29 17:10:15 +10:00
|
|
|
{
|
2022-12-15 14:18:57 -06:00
|
|
|
using namespace blender;
|
2024-03-21 23:18:49 +01:00
|
|
|
BLI_assert(mesh->runtime->edit_mesh == nullptr);
|
2024-03-27 12:28:16 +01:00
|
|
|
if (BKE_color_attribute_supported(*mesh, mesh->active_color_attribute)) {
|
|
|
|
|
return true;
|
2022-12-15 14:18:57 -06:00
|
|
|
}
|
2022-04-05 11:42:55 -07:00
|
|
|
|
2023-12-29 13:35:35 -05:00
|
|
|
const std::string unique_name = BKE_id_attribute_calc_unique_name(mesh->id, name);
|
2023-12-20 13:13:16 -05:00
|
|
|
if (!mesh->attributes_for_write().add(unique_name,
|
|
|
|
|
bke::AttrDomain::Corner,
|
|
|
|
|
CD_PROP_BYTE_COLOR,
|
|
|
|
|
bke::AttributeInitDefaultValue()))
|
2022-12-15 14:18:57 -06:00
|
|
|
{
|
|
|
|
|
return false;
|
2017-09-29 17:10:15 +10:00
|
|
|
}
|
|
|
|
|
|
2023-12-29 13:35:35 -05:00
|
|
|
BKE_id_attributes_active_color_set(&mesh->id, unique_name.c_str());
|
|
|
|
|
BKE_id_attributes_default_color_set(&mesh->id, unique_name.c_str());
|
2023-12-08 16:40:06 -05:00
|
|
|
BKE_mesh_tessface_clear(mesh);
|
|
|
|
|
DEG_id_tag_update(&mesh->id, 0);
|
2017-09-29 17:10:15 +10:00
|
|
|
|
2022-12-15 14:18:57 -06:00
|
|
|
return true;
|
2017-09-29 17:10:15 +10:00
|
|
|
}
|
|
|
|
|
|
2021-07-22 10:10:57 +02:00
|
|
|
/*********************** General poll ************************/
|
|
|
|
|
|
|
|
|
|
static bool layers_poll(bContext *C)
|
|
|
|
|
{
|
2024-03-28 01:30:38 +01:00
|
|
|
Object *ob = blender::ed::object::context_object(C);
|
2022-05-15 20:41:11 +02:00
|
|
|
ID *data = (ob) ? static_cast<ID *>(ob->data) : nullptr;
|
2022-03-28 17:34:36 +02:00
|
|
|
return (ob && !ID_IS_LINKED(ob) && !ID_IS_OVERRIDE_LIBRARY(ob) && ob->type == OB_MESH && data &&
|
|
|
|
|
!ID_IS_LINKED(data) && !ID_IS_OVERRIDE_LIBRARY(data));
|
2021-07-22 10:10:57 +02:00
|
|
|
}
|
|
|
|
|
|
2009-09-28 14:28:45 +00:00
|
|
|
/*********************** UV texture operators ************************/
|
|
|
|
|
|
2021-07-22 10:10:57 +02:00
|
|
|
static bool uv_texture_remove_poll(bContext *C)
|
2009-09-28 14:28:45 +00:00
|
|
|
{
|
2021-07-22 10:10:57 +02:00
|
|
|
if (!layers_poll(C)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-28 01:30:38 +01:00
|
|
|
Object *ob = blender::ed::object::context_object(C);
|
2023-12-08 16:40:06 -05:00
|
|
|
Mesh *mesh = static_cast<Mesh *>(ob->data);
|
|
|
|
|
CustomData *ldata = mesh_customdata_get_type(mesh, BM_LOOP, nullptr);
|
Mesh: Move UV layers to generic attributes
Currently the `MLoopUV` struct stores UV coordinates and flags related
to editing UV maps in the UV editor. This patch changes the coordinates
to use the generic 2D vector type, and moves the flags into three
separate boolean attributes. This follows the design in T95965, with
the ultimate intention of simplifying code and improving performance.
Importantly, the change allows exporters and renderers to use UVs
"touched" by geometry nodes, which only creates generic attributes.
It also allows geometry nodes to create "proper" UV maps from scratch,
though only with the Store Named Attribute node for now.
The new design considers any 2D vector attribute on the corner domain
to be a UV map. In the future, they might be distinguished from regular
2D vectors with attribute metadata, which may be helpful because they
are often interpolated differently.
Most of the code changes deal with passing around UV BMesh custom data
offsets and tracking the boolean "sublayers". The boolean layers are
use the following prefixes for attribute names: vert selection: `.vs.`,
edge selection: `.es.`, pinning: `.pn.`. Currently these are short to
avoid using up the maximum length of attribute names. To accommodate
for these 4 extra characters, the name length limit is enlarged to 68
bytes, while the maximum user settable name length is still 64 bytes.
Unfortunately Python/RNA API access to the UV flag data becomes slower.
Accessing the boolean layers directly is be better for performance in
general.
Like the other mesh SoA refactors, backward and forward compatibility
aren't affected, and won't be changed until 4.0. We pay for that by
making mesh reading and writing more expensive with conversions.
Resolves T85962
Differential Revision: https://developer.blender.org/D14365
2023-01-10 00:47:04 -05:00
|
|
|
const int active = CustomData_get_active_layer(ldata, CD_PROP_FLOAT2);
|
2021-07-22 10:10:57 +02:00
|
|
|
if (active != -1) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
2009-09-28 14:28:45 +00:00
|
|
|
}
|
|
|
|
|
|
2021-11-08 17:05:10 +01:00
|
|
|
static int mesh_uv_texture_add_exec(bContext *C, wmOperator *op)
|
2009-09-28 14:28:45 +00:00
|
|
|
{
|
2024-03-28 01:30:38 +01:00
|
|
|
Object *ob = blender::ed::object::context_object(C);
|
2023-12-08 16:40:06 -05:00
|
|
|
Mesh *mesh = static_cast<Mesh *>(ob->data);
|
2009-09-28 14:28:45 +00:00
|
|
|
|
2023-12-08 16:40:06 -05:00
|
|
|
if (ED_mesh_uv_add(mesh, nullptr, true, true, op->reports) == -1) {
|
2009-09-28 14:28:45 +00:00
|
|
|
return OPERATOR_CANCELLED;
|
2019-04-22 09:19:45 +10:00
|
|
|
}
|
2009-09-28 14:28:45 +00:00
|
|
|
|
2018-04-05 18:20:27 +02:00
|
|
|
if (ob->mode & OB_MODE_TEXTURE_PAINT) {
|
2014-10-06 15:12:06 +02:00
|
|
|
Scene *scene = CTX_data_scene(C);
|
2022-05-15 20:41:11 +02:00
|
|
|
ED_paint_proj_mesh_data_check(scene, ob, nullptr, nullptr, nullptr, nullptr);
|
|
|
|
|
WM_event_add_notifier(C, NC_SCENE | ND_TOOLSETTINGS, nullptr);
|
2014-10-06 15:12:06 +02:00
|
|
|
}
|
2018-06-04 09:31:30 +02:00
|
|
|
|
2009-07-01 22:25:49 +00:00
|
|
|
return OPERATOR_FINISHED;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MESH_OT_uv_texture_add(wmOperatorType *ot)
|
|
|
|
|
{
|
|
|
|
|
/* identifiers */
|
2012-03-22 07:26:09 +00:00
|
|
|
ot->name = "Add UV Map";
|
2020-12-24 11:07:32 -06:00
|
|
|
ot->description = "Add UV map";
|
2012-03-22 07:26:09 +00:00
|
|
|
ot->idname = "MESH_OT_uv_texture_add";
|
2018-06-04 09:31:30 +02:00
|
|
|
|
2009-07-01 22:25:49 +00:00
|
|
|
/* api callbacks */
|
2012-03-22 07:26:09 +00:00
|
|
|
ot->poll = layers_poll;
|
2012-03-23 21:25:07 +00:00
|
|
|
ot->exec = mesh_uv_texture_add_exec;
|
2009-07-01 22:25:49 +00:00
|
|
|
|
|
|
|
|
/* flags */
|
2012-03-26 02:56:48 +00:00
|
|
|
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
|
2009-07-01 22:25:49 +00:00
|
|
|
}
|
|
|
|
|
|
Mesh: Move UV layers to generic attributes
Currently the `MLoopUV` struct stores UV coordinates and flags related
to editing UV maps in the UV editor. This patch changes the coordinates
to use the generic 2D vector type, and moves the flags into three
separate boolean attributes. This follows the design in T95965, with
the ultimate intention of simplifying code and improving performance.
Importantly, the change allows exporters and renderers to use UVs
"touched" by geometry nodes, which only creates generic attributes.
It also allows geometry nodes to create "proper" UV maps from scratch,
though only with the Store Named Attribute node for now.
The new design considers any 2D vector attribute on the corner domain
to be a UV map. In the future, they might be distinguished from regular
2D vectors with attribute metadata, which may be helpful because they
are often interpolated differently.
Most of the code changes deal with passing around UV BMesh custom data
offsets and tracking the boolean "sublayers". The boolean layers are
use the following prefixes for attribute names: vert selection: `.vs.`,
edge selection: `.es.`, pinning: `.pn.`. Currently these are short to
avoid using up the maximum length of attribute names. To accommodate
for these 4 extra characters, the name length limit is enlarged to 68
bytes, while the maximum user settable name length is still 64 bytes.
Unfortunately Python/RNA API access to the UV flag data becomes slower.
Accessing the boolean layers directly is be better for performance in
general.
Like the other mesh SoA refactors, backward and forward compatibility
aren't affected, and won't be changed until 4.0. We pay for that by
making mesh reading and writing more expensive with conversions.
Resolves T85962
Differential Revision: https://developer.blender.org/D14365
2023-01-10 00:47:04 -05:00
|
|
|
static int mesh_uv_texture_remove_exec(bContext *C, wmOperator *op)
|
2009-07-01 22:25:49 +00:00
|
|
|
{
|
2024-03-28 01:30:38 +01:00
|
|
|
Object *ob = blender::ed::object::context_object(C);
|
2023-12-08 16:40:06 -05:00
|
|
|
Mesh *mesh = static_cast<Mesh *>(ob->data);
|
2009-07-01 22:25:49 +00:00
|
|
|
|
2023-12-08 16:40:06 -05:00
|
|
|
CustomData *ldata = mesh_customdata_get_type(mesh, BM_LOOP, nullptr);
|
Mesh: Move UV layers to generic attributes
Currently the `MLoopUV` struct stores UV coordinates and flags related
to editing UV maps in the UV editor. This patch changes the coordinates
to use the generic 2D vector type, and moves the flags into three
separate boolean attributes. This follows the design in T95965, with
the ultimate intention of simplifying code and improving performance.
Importantly, the change allows exporters and renderers to use UVs
"touched" by geometry nodes, which only creates generic attributes.
It also allows geometry nodes to create "proper" UV maps from scratch,
though only with the Store Named Attribute node for now.
The new design considers any 2D vector attribute on the corner domain
to be a UV map. In the future, they might be distinguished from regular
2D vectors with attribute metadata, which may be helpful because they
are often interpolated differently.
Most of the code changes deal with passing around UV BMesh custom data
offsets and tracking the boolean "sublayers". The boolean layers are
use the following prefixes for attribute names: vert selection: `.vs.`,
edge selection: `.es.`, pinning: `.pn.`. Currently these are short to
avoid using up the maximum length of attribute names. To accommodate
for these 4 extra characters, the name length limit is enlarged to 68
bytes, while the maximum user settable name length is still 64 bytes.
Unfortunately Python/RNA API access to the UV flag data becomes slower.
Accessing the boolean layers directly is be better for performance in
general.
Like the other mesh SoA refactors, backward and forward compatibility
aren't affected, and won't be changed until 4.0. We pay for that by
making mesh reading and writing more expensive with conversions.
Resolves T85962
Differential Revision: https://developer.blender.org/D14365
2023-01-10 00:47:04 -05:00
|
|
|
const char *name = CustomData_get_active_layer_name(ldata, CD_PROP_FLOAT2);
|
2023-12-08 16:40:06 -05:00
|
|
|
if (!BKE_id_attribute_remove(&mesh->id, name, op->reports)) {
|
2009-07-01 22:25:49 +00:00
|
|
|
return OPERATOR_CANCELLED;
|
2019-04-22 09:19:45 +10:00
|
|
|
}
|
2009-07-01 22:25:49 +00:00
|
|
|
|
2018-04-05 18:20:27 +02:00
|
|
|
if (ob->mode & OB_MODE_TEXTURE_PAINT) {
|
2014-10-06 15:12:06 +02:00
|
|
|
Scene *scene = CTX_data_scene(C);
|
2022-05-15 20:41:11 +02:00
|
|
|
ED_paint_proj_mesh_data_check(scene, ob, nullptr, nullptr, nullptr, nullptr);
|
|
|
|
|
WM_event_add_notifier(C, NC_SCENE | ND_TOOLSETTINGS, nullptr);
|
2014-10-06 15:12:06 +02:00
|
|
|
}
|
2018-06-04 09:31:30 +02:00
|
|
|
|
2023-12-08 16:40:06 -05:00
|
|
|
DEG_id_tag_update(&mesh->id, ID_RECALC_GEOMETRY);
|
|
|
|
|
WM_main_add_notifier(NC_GEOM | ND_DATA, mesh);
|
2023-02-21 19:27:23 +01:00
|
|
|
|
2009-07-01 22:25:49 +00:00
|
|
|
return OPERATOR_FINISHED;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MESH_OT_uv_texture_remove(wmOperatorType *ot)
|
|
|
|
|
{
|
|
|
|
|
/* identifiers */
|
2012-03-22 07:26:09 +00:00
|
|
|
ot->name = "Remove UV Map";
|
2020-12-24 11:07:32 -06:00
|
|
|
ot->description = "Remove UV map";
|
2012-03-22 07:26:09 +00:00
|
|
|
ot->idname = "MESH_OT_uv_texture_remove";
|
2018-06-04 09:31:30 +02:00
|
|
|
|
2009-07-01 22:25:49 +00:00
|
|
|
/* api callbacks */
|
2021-07-22 10:10:57 +02:00
|
|
|
ot->poll = uv_texture_remove_poll;
|
2012-03-23 21:25:07 +00:00
|
|
|
ot->exec = mesh_uv_texture_remove_exec;
|
2009-07-01 22:25:49 +00:00
|
|
|
|
|
|
|
|
/* flags */
|
2012-03-26 02:56:48 +00:00
|
|
|
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
|
2009-07-01 22:25:49 +00:00
|
|
|
}
|
|
|
|
|
|
2012-09-21 03:41:59 +00:00
|
|
|
/* *** CustomData clear functions, we need an operator for each *** */
|
|
|
|
|
|
2023-03-29 17:10:49 +02:00
|
|
|
static int mesh_customdata_clear_exec__internal(bContext *C,
|
|
|
|
|
char htype,
|
|
|
|
|
const eCustomDataType type)
|
2009-07-01 22:25:49 +00:00
|
|
|
{
|
2023-12-08 16:40:06 -05:00
|
|
|
Mesh *mesh = ED_mesh_context(C);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-09-21 03:41:59 +00:00
|
|
|
int tot;
|
2023-12-08 16:40:06 -05:00
|
|
|
CustomData *data = mesh_customdata_get_type(mesh, htype, &tot);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-03-19 23:17:44 +00:00
|
|
|
BLI_assert(CustomData_layertype_is_singleton(type) == true);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-09-24 10:57:44 +00:00
|
|
|
if (CustomData_has_layer(data, type)) {
|
2024-03-21 23:18:49 +01:00
|
|
|
if (mesh->runtime->edit_mesh) {
|
|
|
|
|
BM_data_layer_free(mesh->runtime->edit_mesh->bm, data, type);
|
2012-10-01 04:00:41 +00:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
CustomData_free_layers(data, type, tot);
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2024-01-26 10:02:06 -05:00
|
|
|
DEG_id_tag_update(&mesh->id, ID_RECALC_GEOMETRY);
|
2023-12-08 16:40:06 -05:00
|
|
|
WM_event_add_notifier(C, NC_GEOM | ND_DATA, mesh);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-09-24 10:57:44 +00:00
|
|
|
return OPERATOR_FINISHED;
|
|
|
|
|
}
|
2020-07-03 15:19:52 +02:00
|
|
|
return OPERATOR_CANCELLED;
|
2009-07-01 22:25:49 +00:00
|
|
|
}
|
|
|
|
|
|
2012-09-21 03:41:59 +00:00
|
|
|
/* Clear Mask */
|
2018-07-02 11:47:00 +02:00
|
|
|
static bool mesh_customdata_mask_clear_poll(bContext *C)
|
2012-09-21 03:41:59 +00:00
|
|
|
{
|
2024-03-28 01:30:38 +01:00
|
|
|
Object *ob = blender::ed::object::context_object(C);
|
2012-09-21 03:41:59 +00:00
|
|
|
if (ob && ob->type == OB_MESH) {
|
2023-12-08 16:40:06 -05:00
|
|
|
Mesh *mesh = static_cast<Mesh *>(ob->data);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-09-23 02:31:30 +00:00
|
|
|
/* special case - can't run this if we're in sculpt mode */
|
2018-04-05 18:20:27 +02:00
|
|
|
if (ob->mode & OB_MODE_SCULPT) {
|
2013-03-19 23:17:44 +00:00
|
|
|
return false;
|
2012-09-23 02:31:30 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-12-08 16:40:06 -05:00
|
|
|
if (!ID_IS_LINKED(mesh) && !ID_IS_OVERRIDE_LIBRARY(mesh)) {
|
|
|
|
|
CustomData *data = mesh_customdata_get_type(mesh, BM_VERT, nullptr);
|
2023-11-20 17:42:01 +01:00
|
|
|
if (CustomData_has_layer_named(data, CD_PROP_FLOAT, ".sculpt_mask")) {
|
2013-03-19 23:17:44 +00:00
|
|
|
return true;
|
2012-09-21 03:41:59 +00:00
|
|
|
}
|
2023-12-08 16:40:06 -05:00
|
|
|
data = mesh_customdata_get_type(mesh, BM_LOOP, nullptr);
|
2012-09-24 10:57:44 +00:00
|
|
|
if (CustomData_has_layer(data, CD_GRID_PAINT_MASK)) {
|
2013-03-19 23:17:44 +00:00
|
|
|
return true;
|
2012-09-24 10:57:44 +00:00
|
|
|
}
|
2012-09-21 03:41:59 +00:00
|
|
|
}
|
|
|
|
|
}
|
2013-03-19 23:17:44 +00:00
|
|
|
return false;
|
2012-09-21 03:41:59 +00:00
|
|
|
}
|
2023-11-20 17:42:01 +01:00
|
|
|
static int mesh_customdata_mask_clear_exec(bContext *C, wmOperator *op)
|
2012-09-21 03:41:59 +00:00
|
|
|
{
|
2024-03-28 01:30:38 +01:00
|
|
|
Object *object = blender::ed::object::context_object(C);
|
2023-11-20 17:42:01 +01:00
|
|
|
Mesh *mesh = static_cast<Mesh *>(object->data);
|
|
|
|
|
const bool ret_a = BKE_id_attribute_remove(&mesh->id, ".sculpt_mask", op->reports);
|
2012-09-24 10:57:44 +00:00
|
|
|
int ret_b = mesh_customdata_clear_exec__internal(C, BM_LOOP, CD_GRID_PAINT_MASK);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-11-20 17:42:01 +01:00
|
|
|
if (ret_a || ret_b == OPERATOR_FINISHED) {
|
2012-09-24 10:57:44 +00:00
|
|
|
return OPERATOR_FINISHED;
|
|
|
|
|
}
|
2020-07-03 15:19:52 +02:00
|
|
|
return OPERATOR_CANCELLED;
|
2012-09-21 03:41:59 +00:00
|
|
|
}
|
|
|
|
|
|
2015-05-03 15:18:27 +02:00
|
|
|
void MESH_OT_customdata_mask_clear(wmOperatorType *ot)
|
2009-07-01 22:25:49 +00:00
|
|
|
{
|
2021-12-09 00:55:11 +11:00
|
|
|
/* NOTE: no create_mask yet */
|
2012-09-21 03:41:59 +00:00
|
|
|
|
2009-07-01 22:25:49 +00:00
|
|
|
/* identifiers */
|
2020-12-24 13:11:22 -06:00
|
|
|
ot->name = "Clear Sculpt Mask Data";
|
2015-05-03 15:18:27 +02:00
|
|
|
ot->idname = "MESH_OT_customdata_mask_clear";
|
2012-09-21 03:41:59 +00:00
|
|
|
ot->description = "Clear vertex sculpt masking data from the mesh";
|
|
|
|
|
|
2009-07-01 22:25:49 +00:00
|
|
|
/* api callbacks */
|
2015-05-03 15:18:27 +02:00
|
|
|
ot->exec = mesh_customdata_mask_clear_exec;
|
|
|
|
|
ot->poll = mesh_customdata_mask_clear_poll;
|
2012-09-21 03:41:59 +00:00
|
|
|
|
|
|
|
|
/* flags */
|
|
|
|
|
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-25 00:02:35 +10:00
|
|
|
/**
|
|
|
|
|
* Clear Skin
|
|
|
|
|
* \return -1 invalid state, 0 no skin, 1 has skin.
|
|
|
|
|
*/
|
|
|
|
|
static int mesh_customdata_skin_state(bContext *C)
|
2012-09-21 21:43:56 +00:00
|
|
|
{
|
2024-03-28 01:30:38 +01:00
|
|
|
Object *ob = blender::ed::object::context_object(C);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-09-21 21:43:56 +00:00
|
|
|
if (ob && ob->type == OB_MESH) {
|
2023-12-08 16:40:06 -05:00
|
|
|
Mesh *mesh = static_cast<Mesh *>(ob->data);
|
|
|
|
|
if (!ID_IS_LINKED(mesh) && !ID_IS_OVERRIDE_LIBRARY(mesh)) {
|
|
|
|
|
CustomData *data = mesh_customdata_get_type(mesh, BM_VERT, nullptr);
|
2015-05-25 00:02:35 +10:00
|
|
|
return CustomData_has_layer(data, CD_MVERT_SKIN);
|
2012-09-21 21:43:56 +00:00
|
|
|
}
|
|
|
|
|
}
|
2015-05-25 00:02:35 +10:00
|
|
|
return -1;
|
2012-09-21 21:43:56 +00:00
|
|
|
}
|
2015-05-03 15:09:48 +02:00
|
|
|
|
2018-07-02 11:47:00 +02:00
|
|
|
static bool mesh_customdata_skin_add_poll(bContext *C)
|
2015-05-03 15:09:48 +02:00
|
|
|
{
|
2015-05-25 00:02:35 +10:00
|
|
|
return (mesh_customdata_skin_state(C) == 0);
|
2015-05-03 15:09:48 +02:00
|
|
|
}
|
|
|
|
|
|
2022-10-03 17:37:25 -05:00
|
|
|
static int mesh_customdata_skin_add_exec(bContext *C, wmOperator * /*op*/)
|
2015-05-03 15:09:48 +02:00
|
|
|
{
|
2024-03-28 01:30:38 +01:00
|
|
|
Object *ob = blender::ed::object::context_object(C);
|
2023-12-08 16:40:06 -05:00
|
|
|
Mesh *mesh = static_cast<Mesh *>(ob->data);
|
2015-05-03 15:09:48 +02:00
|
|
|
|
2023-12-08 16:40:06 -05:00
|
|
|
BKE_mesh_ensure_skin_customdata(mesh);
|
2015-05-03 15:09:48 +02:00
|
|
|
|
2023-12-08 16:40:06 -05:00
|
|
|
DEG_id_tag_update(&mesh->id, 0);
|
|
|
|
|
WM_event_add_notifier(C, NC_GEOM | ND_DATA, mesh);
|
2015-05-03 15:09:48 +02:00
|
|
|
|
|
|
|
|
return OPERATOR_FINISHED;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MESH_OT_customdata_skin_add(wmOperatorType *ot)
|
|
|
|
|
{
|
|
|
|
|
/* identifiers */
|
|
|
|
|
ot->name = "Add Skin Data";
|
|
|
|
|
ot->idname = "MESH_OT_customdata_skin_add";
|
|
|
|
|
ot->description = "Add a vertex skin layer";
|
|
|
|
|
|
|
|
|
|
/* api callbacks */
|
|
|
|
|
ot->exec = mesh_customdata_skin_add_exec;
|
|
|
|
|
ot->poll = mesh_customdata_skin_add_poll;
|
|
|
|
|
|
|
|
|
|
/* flags */
|
|
|
|
|
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-02 11:47:00 +02:00
|
|
|
static bool mesh_customdata_skin_clear_poll(bContext *C)
|
2015-05-03 15:09:48 +02:00
|
|
|
{
|
2015-05-25 00:02:35 +10:00
|
|
|
return (mesh_customdata_skin_state(C) == 1);
|
2015-05-03 15:09:48 +02:00
|
|
|
}
|
|
|
|
|
|
2022-10-03 17:37:25 -05:00
|
|
|
static int mesh_customdata_skin_clear_exec(bContext *C, wmOperator * /*op*/)
|
2012-09-21 21:43:56 +00:00
|
|
|
{
|
|
|
|
|
return mesh_customdata_clear_exec__internal(C, BM_VERT, CD_MVERT_SKIN);
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-03 15:18:27 +02:00
|
|
|
void MESH_OT_customdata_skin_clear(wmOperatorType *ot)
|
2012-09-21 21:43:56 +00:00
|
|
|
{
|
|
|
|
|
/* identifiers */
|
|
|
|
|
ot->name = "Clear Skin Data";
|
2015-05-03 15:18:27 +02:00
|
|
|
ot->idname = "MESH_OT_customdata_skin_clear";
|
2012-09-21 21:43:56 +00:00
|
|
|
ot->description = "Clear vertex skin layer";
|
|
|
|
|
|
|
|
|
|
/* api callbacks */
|
2015-05-03 15:18:27 +02:00
|
|
|
ot->exec = mesh_customdata_skin_clear_exec;
|
|
|
|
|
ot->poll = mesh_customdata_skin_clear_poll;
|
2012-09-21 21:43:56 +00:00
|
|
|
|
|
|
|
|
/* flags */
|
|
|
|
|
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
|
|
|
|
|
}
|
|
|
|
|
|
Add Custom Loop Normals.
This is the core code for it, tools (datatransfer and modifier) will come in next commits).
RNA api is already there, though.
See the code for details, but basically, we define, for each 'smooth fan'
(which is a set of adjacent loops around a same vertex that are smooth, i.e. have a single same normal),
a 'loop normal space' (or lnor space), using auto-computed normal and relevant edges, and store
custom normal as two angular factors inside that space. This allows to have custom normals
'following' deformations of the geometry, and to only save two shorts per loop in new clnor CDLayer.
Normal manipulation (editing, mixing, interpolating, etc.) shall always happen with plain 3D vectors normals,
and be converted back into storage format at the end.
Clnor computation has also been threaded (at least for Mesh case, not for BMesh), since the process can
be rather heavy with high poly meshes.
Also, bumping subversion, and fix mess in 2.70 versioning code.
2015-02-05 14:24:48 +01:00
|
|
|
/* Clear custom loop normals */
|
2022-10-03 17:37:25 -05:00
|
|
|
static int mesh_customdata_custom_splitnormals_add_exec(bContext *C, wmOperator * /*op*/)
|
Add Custom Loop Normals.
This is the core code for it, tools (datatransfer and modifier) will come in next commits).
RNA api is already there, though.
See the code for details, but basically, we define, for each 'smooth fan'
(which is a set of adjacent loops around a same vertex that are smooth, i.e. have a single same normal),
a 'loop normal space' (or lnor space), using auto-computed normal and relevant edges, and store
custom normal as two angular factors inside that space. This allows to have custom normals
'following' deformations of the geometry, and to only save two shorts per loop in new clnor CDLayer.
Normal manipulation (editing, mixing, interpolating, etc.) shall always happen with plain 3D vectors normals,
and be converted back into storage format at the end.
Clnor computation has also been threaded (at least for Mesh case, not for BMesh), since the process can
be rather heavy with high poly meshes.
Also, bumping subversion, and fix mess in 2.70 versioning code.
2015-02-05 14:24:48 +01:00
|
|
|
{
|
2023-01-10 16:12:14 -05:00
|
|
|
using namespace blender;
|
2023-12-08 16:40:06 -05:00
|
|
|
Mesh *mesh = ED_mesh_context(C);
|
|
|
|
|
if (BKE_mesh_has_custom_loop_normals(mesh)) {
|
2022-12-08 15:22:21 -06:00
|
|
|
return OPERATOR_CANCELLED;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2024-03-21 23:18:49 +01:00
|
|
|
if (mesh->runtime->edit_mesh) {
|
|
|
|
|
BMesh &bm = *mesh->runtime->edit_mesh->bm;
|
2022-12-08 15:22:21 -06:00
|
|
|
BM_data_layer_add(&bm, &bm.ldata, CD_CUSTOMLOOPNORMAL);
|
|
|
|
|
}
|
|
|
|
|
else {
|
2023-12-19 20:38:59 -05:00
|
|
|
CustomData_add_layer(
|
|
|
|
|
&mesh->corner_data, CD_CUSTOMLOOPNORMAL, CD_SET_DEFAULT, mesh->corners_num);
|
Add Custom Loop Normals.
This is the core code for it, tools (datatransfer and modifier) will come in next commits).
RNA api is already there, though.
See the code for details, but basically, we define, for each 'smooth fan'
(which is a set of adjacent loops around a same vertex that are smooth, i.e. have a single same normal),
a 'loop normal space' (or lnor space), using auto-computed normal and relevant edges, and store
custom normal as two angular factors inside that space. This allows to have custom normals
'following' deformations of the geometry, and to only save two shorts per loop in new clnor CDLayer.
Normal manipulation (editing, mixing, interpolating, etc.) shall always happen with plain 3D vectors normals,
and be converted back into storage format at the end.
Clnor computation has also been threaded (at least for Mesh case, not for BMesh), since the process can
be rather heavy with high poly meshes.
Also, bumping subversion, and fix mess in 2.70 versioning code.
2015-02-05 14:24:48 +01:00
|
|
|
}
|
2022-12-08 15:22:21 -06:00
|
|
|
|
2023-12-08 16:40:06 -05:00
|
|
|
DEG_id_tag_update(&mesh->id, 0);
|
|
|
|
|
WM_event_add_notifier(C, NC_GEOM | ND_DATA, mesh);
|
2022-12-08 15:22:21 -06:00
|
|
|
|
|
|
|
|
return OPERATOR_FINISHED;
|
Add Custom Loop Normals.
This is the core code for it, tools (datatransfer and modifier) will come in next commits).
RNA api is already there, though.
See the code for details, but basically, we define, for each 'smooth fan'
(which is a set of adjacent loops around a same vertex that are smooth, i.e. have a single same normal),
a 'loop normal space' (or lnor space), using auto-computed normal and relevant edges, and store
custom normal as two angular factors inside that space. This allows to have custom normals
'following' deformations of the geometry, and to only save two shorts per loop in new clnor CDLayer.
Normal manipulation (editing, mixing, interpolating, etc.) shall always happen with plain 3D vectors normals,
and be converted back into storage format at the end.
Clnor computation has also been threaded (at least for Mesh case, not for BMesh), since the process can
be rather heavy with high poly meshes.
Also, bumping subversion, and fix mess in 2.70 versioning code.
2015-02-05 14:24:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MESH_OT_customdata_custom_splitnormals_add(wmOperatorType *ot)
|
|
|
|
|
{
|
|
|
|
|
/* identifiers */
|
|
|
|
|
ot->name = "Add Custom Split Normals Data";
|
|
|
|
|
ot->idname = "MESH_OT_customdata_custom_splitnormals_add";
|
|
|
|
|
ot->description = "Add a custom split normals layer, if none exists yet";
|
|
|
|
|
|
|
|
|
|
/* api callbacks */
|
|
|
|
|
ot->exec = mesh_customdata_custom_splitnormals_add_exec;
|
2019-07-16 15:06:25 +02:00
|
|
|
ot->poll = ED_operator_editable_mesh;
|
Add Custom Loop Normals.
This is the core code for it, tools (datatransfer and modifier) will come in next commits).
RNA api is already there, though.
See the code for details, but basically, we define, for each 'smooth fan'
(which is a set of adjacent loops around a same vertex that are smooth, i.e. have a single same normal),
a 'loop normal space' (or lnor space), using auto-computed normal and relevant edges, and store
custom normal as two angular factors inside that space. This allows to have custom normals
'following' deformations of the geometry, and to only save two shorts per loop in new clnor CDLayer.
Normal manipulation (editing, mixing, interpolating, etc.) shall always happen with plain 3D vectors normals,
and be converted back into storage format at the end.
Clnor computation has also been threaded (at least for Mesh case, not for BMesh), since the process can
be rather heavy with high poly meshes.
Also, bumping subversion, and fix mess in 2.70 versioning code.
2015-02-05 14:24:48 +01:00
|
|
|
|
|
|
|
|
/* flags */
|
|
|
|
|
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-03 17:37:25 -05:00
|
|
|
static int mesh_customdata_custom_splitnormals_clear_exec(bContext *C, wmOperator * /*op*/)
|
Add Custom Loop Normals.
This is the core code for it, tools (datatransfer and modifier) will come in next commits).
RNA api is already there, though.
See the code for details, but basically, we define, for each 'smooth fan'
(which is a set of adjacent loops around a same vertex that are smooth, i.e. have a single same normal),
a 'loop normal space' (or lnor space), using auto-computed normal and relevant edges, and store
custom normal as two angular factors inside that space. This allows to have custom normals
'following' deformations of the geometry, and to only save two shorts per loop in new clnor CDLayer.
Normal manipulation (editing, mixing, interpolating, etc.) shall always happen with plain 3D vectors normals,
and be converted back into storage format at the end.
Clnor computation has also been threaded (at least for Mesh case, not for BMesh), since the process can
be rather heavy with high poly meshes.
Also, bumping subversion, and fix mess in 2.70 versioning code.
2015-02-05 14:24:48 +01:00
|
|
|
{
|
2023-12-08 16:40:06 -05:00
|
|
|
Mesh *mesh = ED_mesh_context(C);
|
Add Custom Loop Normals.
This is the core code for it, tools (datatransfer and modifier) will come in next commits).
RNA api is already there, though.
See the code for details, but basically, we define, for each 'smooth fan'
(which is a set of adjacent loops around a same vertex that are smooth, i.e. have a single same normal),
a 'loop normal space' (or lnor space), using auto-computed normal and relevant edges, and store
custom normal as two angular factors inside that space. This allows to have custom normals
'following' deformations of the geometry, and to only save two shorts per loop in new clnor CDLayer.
Normal manipulation (editing, mixing, interpolating, etc.) shall always happen with plain 3D vectors normals,
and be converted back into storage format at the end.
Clnor computation has also been threaded (at least for Mesh case, not for BMesh), since the process can
be rather heavy with high poly meshes.
Also, bumping subversion, and fix mess in 2.70 versioning code.
2015-02-05 14:24:48 +01:00
|
|
|
|
2024-03-21 23:18:49 +01:00
|
|
|
if (BMEditMesh *em = mesh->runtime->edit_mesh) {
|
2024-01-26 10:02:06 -05:00
|
|
|
BMesh &bm = *em->bm;
|
|
|
|
|
if (!CustomData_has_layer(&bm.ldata, CD_CUSTOMLOOPNORMAL)) {
|
|
|
|
|
return OPERATOR_CANCELLED;
|
|
|
|
|
}
|
|
|
|
|
BM_data_layer_free(&bm, &bm.ldata, CD_CUSTOMLOOPNORMAL);
|
|
|
|
|
if (bm.lnor_spacearr) {
|
|
|
|
|
BKE_lnor_spacearr_clear(bm.lnor_spacearr);
|
2020-08-27 15:47:13 +02:00
|
|
|
}
|
Add Custom Loop Normals.
This is the core code for it, tools (datatransfer and modifier) will come in next commits).
RNA api is already there, though.
See the code for details, but basically, we define, for each 'smooth fan'
(which is a set of adjacent loops around a same vertex that are smooth, i.e. have a single same normal),
a 'loop normal space' (or lnor space), using auto-computed normal and relevant edges, and store
custom normal as two angular factors inside that space. This allows to have custom normals
'following' deformations of the geometry, and to only save two shorts per loop in new clnor CDLayer.
Normal manipulation (editing, mixing, interpolating, etc.) shall always happen with plain 3D vectors normals,
and be converted back into storage format at the end.
Clnor computation has also been threaded (at least for Mesh case, not for BMesh), since the process can
be rather heavy with high poly meshes.
Also, bumping subversion, and fix mess in 2.70 versioning code.
2015-02-05 14:24:48 +01:00
|
|
|
}
|
2024-01-26 10:02:06 -05:00
|
|
|
else {
|
|
|
|
|
if (!CustomData_has_layer(&mesh->corner_data, CD_CUSTOMLOOPNORMAL)) {
|
|
|
|
|
return OPERATOR_CANCELLED;
|
|
|
|
|
}
|
|
|
|
|
CustomData_free_layers(&mesh->corner_data, CD_CUSTOMLOOPNORMAL, mesh->corners_num);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mesh->tag_custom_normals_changed();
|
|
|
|
|
DEG_id_tag_update(&mesh->id, ID_RECALC_GEOMETRY);
|
|
|
|
|
WM_event_add_notifier(C, NC_GEOM | ND_DATA, mesh);
|
|
|
|
|
|
|
|
|
|
return OPERATOR_FINISHED;
|
Add Custom Loop Normals.
This is the core code for it, tools (datatransfer and modifier) will come in next commits).
RNA api is already there, though.
See the code for details, but basically, we define, for each 'smooth fan'
(which is a set of adjacent loops around a same vertex that are smooth, i.e. have a single same normal),
a 'loop normal space' (or lnor space), using auto-computed normal and relevant edges, and store
custom normal as two angular factors inside that space. This allows to have custom normals
'following' deformations of the geometry, and to only save two shorts per loop in new clnor CDLayer.
Normal manipulation (editing, mixing, interpolating, etc.) shall always happen with plain 3D vectors normals,
and be converted back into storage format at the end.
Clnor computation has also been threaded (at least for Mesh case, not for BMesh), since the process can
be rather heavy with high poly meshes.
Also, bumping subversion, and fix mess in 2.70 versioning code.
2015-02-05 14:24:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MESH_OT_customdata_custom_splitnormals_clear(wmOperatorType *ot)
|
|
|
|
|
{
|
|
|
|
|
/* identifiers */
|
|
|
|
|
ot->name = "Clear Custom Split Normals Data";
|
|
|
|
|
ot->idname = "MESH_OT_customdata_custom_splitnormals_clear";
|
|
|
|
|
ot->description = "Remove the custom split normals layer, if it exists";
|
|
|
|
|
|
|
|
|
|
/* api callbacks */
|
|
|
|
|
ot->exec = mesh_customdata_custom_splitnormals_clear_exec;
|
2019-07-16 15:06:25 +02:00
|
|
|
ot->poll = ED_operator_editable_mesh;
|
Add Custom Loop Normals.
This is the core code for it, tools (datatransfer and modifier) will come in next commits).
RNA api is already there, though.
See the code for details, but basically, we define, for each 'smooth fan'
(which is a set of adjacent loops around a same vertex that are smooth, i.e. have a single same normal),
a 'loop normal space' (or lnor space), using auto-computed normal and relevant edges, and store
custom normal as two angular factors inside that space. This allows to have custom normals
'following' deformations of the geometry, and to only save two shorts per loop in new clnor CDLayer.
Normal manipulation (editing, mixing, interpolating, etc.) shall always happen with plain 3D vectors normals,
and be converted back into storage format at the end.
Clnor computation has also been threaded (at least for Mesh case, not for BMesh), since the process can
be rather heavy with high poly meshes.
Also, bumping subversion, and fix mess in 2.70 versioning code.
2015-02-05 14:24:48 +01:00
|
|
|
|
|
|
|
|
/* flags */
|
|
|
|
|
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
|
|
|
|
|
}
|
|
|
|
|
|
2009-09-28 14:28:45 +00:00
|
|
|
static void mesh_add_verts(Mesh *mesh, int len)
|
|
|
|
|
{
|
Mesh: Move selection flags to generic attributes
Using the attribute name semantics from T97452, this patch moves the
selection status of mesh elements from the `SELECT` of vertices, and
edges, and the `ME_FACE_SEL` of faces to generic boolean attribute
Storing this data as generic attributes can significantly simplify and
improve code, as described in T95965.
The attributes are called `.select_vert`, `.select_edge`, and
`.select_poly`. The `.` prefix means they are "UI attributes",so they
still contain original data edited by users, but they aren't meant to
be accessed procedurally by the user in arbitrary situations. They are
also be hidden in the spreadsheet and the attribute list.
Until 4.0, the attributes are still written to and read from the mesh
in the old way, so neither forward nor backward compatibility are
affected. This means memory requirements will be increased by one byte
per element when selection is used. When the flags are removed
completely, requirements will decrease.
Further notes:
* The `MVert` flag is empty at runtime now, so it can be ignored.
* `BMesh` is unchanged, otherwise the change would be much larger.
* Many tests have slightly different results, since the selection
attribute uses more generic propagation. Previously you couldn't
really rely on edit mode selections being propagated procedurally.
Now it mostly works as expected.
Similar to 2480b55f216c
Ref T95965
Differential Revision: https://developer.blender.org/D15795
2022-09-23 09:38:37 -05:00
|
|
|
using namespace blender;
|
2019-04-22 09:19:45 +10:00
|
|
|
if (len == 0) {
|
2009-09-28 14:28:45 +00:00
|
|
|
return;
|
2019-04-22 09:19:45 +10:00
|
|
|
}
|
2009-09-28 14:28:45 +00:00
|
|
|
|
2023-12-20 02:21:48 +01:00
|
|
|
int totvert = mesh->verts_num + len;
|
2023-07-25 21:15:52 +02:00
|
|
|
CustomData vert_data;
|
|
|
|
|
CustomData_copy_layout(
|
|
|
|
|
&mesh->vert_data, &vert_data, CD_MASK_MESH.vmask, CD_SET_DEFAULT, totvert);
|
2023-12-20 02:21:48 +01:00
|
|
|
CustomData_copy_data(&mesh->vert_data, &vert_data, 0, 0, mesh->verts_num);
|
2009-09-28 14:28:45 +00:00
|
|
|
|
2023-07-25 21:15:52 +02:00
|
|
|
if (!CustomData_has_layer_named(&vert_data, CD_PROP_FLOAT3, "position")) {
|
|
|
|
|
CustomData_add_layer_named(&vert_data, CD_PROP_FLOAT3, CD_SET_DEFAULT, totvert, "position");
|
2019-04-22 09:19:45 +10:00
|
|
|
}
|
2009-09-28 14:28:45 +00:00
|
|
|
|
2023-12-20 02:21:48 +01:00
|
|
|
CustomData_free(&mesh->vert_data, mesh->verts_num);
|
2023-07-25 21:15:52 +02:00
|
|
|
mesh->vert_data = vert_data;
|
2009-09-28 14:28:45 +00:00
|
|
|
|
2022-02-22 11:14:50 -05:00
|
|
|
BKE_mesh_runtime_clear_cache(mesh);
|
|
|
|
|
|
2023-12-20 02:21:48 +01:00
|
|
|
mesh->verts_num = totvert;
|
2009-09-28 14:28:45 +00:00
|
|
|
|
Mesh: Move selection flags to generic attributes
Using the attribute name semantics from T97452, this patch moves the
selection status of mesh elements from the `SELECT` of vertices, and
edges, and the `ME_FACE_SEL` of faces to generic boolean attribute
Storing this data as generic attributes can significantly simplify and
improve code, as described in T95965.
The attributes are called `.select_vert`, `.select_edge`, and
`.select_poly`. The `.` prefix means they are "UI attributes",so they
still contain original data edited by users, but they aren't meant to
be accessed procedurally by the user in arbitrary situations. They are
also be hidden in the spreadsheet and the attribute list.
Until 4.0, the attributes are still written to and read from the mesh
in the old way, so neither forward nor backward compatibility are
affected. This means memory requirements will be increased by one byte
per element when selection is used. When the flags are removed
completely, requirements will decrease.
Further notes:
* The `MVert` flag is empty at runtime now, so it can be ignored.
* `BMesh` is unchanged, otherwise the change would be much larger.
* Many tests have slightly different results, since the selection
attribute uses more generic propagation. Previously you couldn't
really rely on edit mode selections being propagated procedurally.
Now it mostly works as expected.
Similar to 2480b55f216c
Ref T95965
Differential Revision: https://developer.blender.org/D15795
2022-09-23 09:38:37 -05:00
|
|
|
bke::MutableAttributeAccessor attributes = mesh->attributes_for_write();
|
|
|
|
|
bke::SpanAttributeWriter<bool> select_vert = attributes.lookup_or_add_for_write_span<bool>(
|
2023-12-20 13:13:16 -05:00
|
|
|
".select_vert", bke::AttrDomain::Point);
|
Mesh: Move selection flags to generic attributes
Using the attribute name semantics from T97452, this patch moves the
selection status of mesh elements from the `SELECT` of vertices, and
edges, and the `ME_FACE_SEL` of faces to generic boolean attribute
Storing this data as generic attributes can significantly simplify and
improve code, as described in T95965.
The attributes are called `.select_vert`, `.select_edge`, and
`.select_poly`. The `.` prefix means they are "UI attributes",so they
still contain original data edited by users, but they aren't meant to
be accessed procedurally by the user in arbitrary situations. They are
also be hidden in the spreadsheet and the attribute list.
Until 4.0, the attributes are still written to and read from the mesh
in the old way, so neither forward nor backward compatibility are
affected. This means memory requirements will be increased by one byte
per element when selection is used. When the flags are removed
completely, requirements will decrease.
Further notes:
* The `MVert` flag is empty at runtime now, so it can be ignored.
* `BMesh` is unchanged, otherwise the change would be much larger.
* Many tests have slightly different results, since the selection
attribute uses more generic propagation. Previously you couldn't
really rely on edit mode selections being propagated procedurally.
Now it mostly works as expected.
Similar to 2480b55f216c
Ref T95965
Differential Revision: https://developer.blender.org/D15795
2022-09-23 09:38:37 -05:00
|
|
|
select_vert.span.take_back(len).fill(true);
|
|
|
|
|
select_vert.finish();
|
2009-09-28 14:28:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void mesh_add_edges(Mesh *mesh, int len)
|
|
|
|
|
{
|
Mesh: Move selection flags to generic attributes
Using the attribute name semantics from T97452, this patch moves the
selection status of mesh elements from the `SELECT` of vertices, and
edges, and the `ME_FACE_SEL` of faces to generic boolean attribute
Storing this data as generic attributes can significantly simplify and
improve code, as described in T95965.
The attributes are called `.select_vert`, `.select_edge`, and
`.select_poly`. The `.` prefix means they are "UI attributes",so they
still contain original data edited by users, but they aren't meant to
be accessed procedurally by the user in arbitrary situations. They are
also be hidden in the spreadsheet and the attribute list.
Until 4.0, the attributes are still written to and read from the mesh
in the old way, so neither forward nor backward compatibility are
affected. This means memory requirements will be increased by one byte
per element when selection is used. When the flags are removed
completely, requirements will decrease.
Further notes:
* The `MVert` flag is empty at runtime now, so it can be ignored.
* `BMesh` is unchanged, otherwise the change would be much larger.
* Many tests have slightly different results, since the selection
attribute uses more generic propagation. Previously you couldn't
really rely on edit mode selections being propagated procedurally.
Now it mostly works as expected.
Similar to 2480b55f216c
Ref T95965
Differential Revision: https://developer.blender.org/D15795
2022-09-23 09:38:37 -05:00
|
|
|
using namespace blender;
|
2023-07-25 21:15:52 +02:00
|
|
|
CustomData edge_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
|
|
|
int totedge;
|
2009-09-28 14:28:45 +00:00
|
|
|
|
2019-04-22 09:19:45 +10:00
|
|
|
if (len == 0) {
|
2009-09-28 14:28:45 +00:00
|
|
|
return;
|
2019-04-22 09:19:45 +10:00
|
|
|
}
|
2009-09-28 14:28:45 +00:00
|
|
|
|
2023-12-20 02:21:48 +01:00
|
|
|
totedge = mesh->edges_num + len;
|
2009-09-28 14:28:45 +00:00
|
|
|
|
2021-06-24 15:56:58 +10:00
|
|
|
/* Update custom-data. */
|
2023-07-25 21:15:52 +02:00
|
|
|
CustomData_copy_layout(
|
|
|
|
|
&mesh->edge_data, &edge_data, CD_MASK_MESH.emask, CD_SET_DEFAULT, totedge);
|
2023-12-20 02:21:48 +01:00
|
|
|
CustomData_copy_data(&mesh->edge_data, &edge_data, 0, 0, mesh->edges_num);
|
2009-09-28 14:28:45 +00:00
|
|
|
|
2023-07-25 21:15:52 +02:00
|
|
|
if (!CustomData_has_layer_named(&edge_data, CD_PROP_INT32_2D, ".edge_verts")) {
|
|
|
|
|
CustomData_add_layer_named(
|
|
|
|
|
&edge_data, CD_PROP_INT32_2D, CD_SET_DEFAULT, totedge, ".edge_verts");
|
2019-04-22 09:19:45 +10:00
|
|
|
}
|
2009-09-28 14:28:45 +00:00
|
|
|
|
2023-12-20 02:21:48 +01:00
|
|
|
CustomData_free(&mesh->edge_data, mesh->edges_num);
|
2023-07-25 21:15:52 +02:00
|
|
|
mesh->edge_data = edge_data;
|
2009-09-28 14:28:45 +00:00
|
|
|
|
2022-02-22 11:14:50 -05:00
|
|
|
BKE_mesh_runtime_clear_cache(mesh);
|
|
|
|
|
|
2023-12-20 02:21:48 +01:00
|
|
|
mesh->edges_num = totedge;
|
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
|
|
|
|
Mesh: Move selection flags to generic attributes
Using the attribute name semantics from T97452, this patch moves the
selection status of mesh elements from the `SELECT` of vertices, and
edges, and the `ME_FACE_SEL` of faces to generic boolean attribute
Storing this data as generic attributes can significantly simplify and
improve code, as described in T95965.
The attributes are called `.select_vert`, `.select_edge`, and
`.select_poly`. The `.` prefix means they are "UI attributes",so they
still contain original data edited by users, but they aren't meant to
be accessed procedurally by the user in arbitrary situations. They are
also be hidden in the spreadsheet and the attribute list.
Until 4.0, the attributes are still written to and read from the mesh
in the old way, so neither forward nor backward compatibility are
affected. This means memory requirements will be increased by one byte
per element when selection is used. When the flags are removed
completely, requirements will decrease.
Further notes:
* The `MVert` flag is empty at runtime now, so it can be ignored.
* `BMesh` is unchanged, otherwise the change would be much larger.
* Many tests have slightly different results, since the selection
attribute uses more generic propagation. Previously you couldn't
really rely on edit mode selections being propagated procedurally.
Now it mostly works as expected.
Similar to 2480b55f216c
Ref T95965
Differential Revision: https://developer.blender.org/D15795
2022-09-23 09:38:37 -05:00
|
|
|
bke::MutableAttributeAccessor attributes = mesh->attributes_for_write();
|
|
|
|
|
bke::SpanAttributeWriter<bool> select_edge = attributes.lookup_or_add_for_write_span<bool>(
|
2023-12-20 13:13:16 -05:00
|
|
|
".select_edge", bke::AttrDomain::Edge);
|
Mesh: Move selection flags to generic attributes
Using the attribute name semantics from T97452, this patch moves the
selection status of mesh elements from the `SELECT` of vertices, and
edges, and the `ME_FACE_SEL` of faces to generic boolean attribute
Storing this data as generic attributes can significantly simplify and
improve code, as described in T95965.
The attributes are called `.select_vert`, `.select_edge`, and
`.select_poly`. The `.` prefix means they are "UI attributes",so they
still contain original data edited by users, but they aren't meant to
be accessed procedurally by the user in arbitrary situations. They are
also be hidden in the spreadsheet and the attribute list.
Until 4.0, the attributes are still written to and read from the mesh
in the old way, so neither forward nor backward compatibility are
affected. This means memory requirements will be increased by one byte
per element when selection is used. When the flags are removed
completely, requirements will decrease.
Further notes:
* The `MVert` flag is empty at runtime now, so it can be ignored.
* `BMesh` is unchanged, otherwise the change would be much larger.
* Many tests have slightly different results, since the selection
attribute uses more generic propagation. Previously you couldn't
really rely on edit mode selections being propagated procedurally.
Now it mostly works as expected.
Similar to 2480b55f216c
Ref T95965
Differential Revision: https://developer.blender.org/D15795
2022-09-23 09:38:37 -05:00
|
|
|
select_edge.span.take_back(len).fill(true);
|
|
|
|
|
select_edge.finish();
|
2009-09-28 14:28:45 +00:00
|
|
|
}
|
|
|
|
|
|
2011-09-01 09:11:00 +00:00
|
|
|
static void mesh_add_loops(Mesh *mesh, int len)
|
|
|
|
|
{
|
|
|
|
|
CustomData ldata;
|
2011-09-07 06:49:20 +00:00
|
|
|
int totloop;
|
2011-09-01 09:11:00 +00:00
|
|
|
|
2019-04-22 09:19:45 +10:00
|
|
|
if (len == 0) {
|
2011-09-01 09:11:00 +00:00
|
|
|
return;
|
2019-04-22 09:19:45 +10:00
|
|
|
}
|
2011-09-01 09:11:00 +00:00
|
|
|
|
2023-12-20 02:21:48 +01:00
|
|
|
totloop = mesh->corners_num + len; /* new face count */
|
2011-09-01 09:11:00 +00:00
|
|
|
|
|
|
|
|
/* update customdata */
|
2023-12-19 20:38:59 -05:00
|
|
|
CustomData_copy_layout(&mesh->corner_data, &ldata, CD_MASK_MESH.lmask, CD_SET_DEFAULT, totloop);
|
|
|
|
|
CustomData_copy_data(&mesh->corner_data, &ldata, 0, 0, mesh->corners_num);
|
2011-09-01 09:11:00 +00:00
|
|
|
|
2023-05-17 16:16:54 -04:00
|
|
|
if (!CustomData_has_layer_named(&ldata, CD_PROP_INT32, ".corner_vert")) {
|
Mesh: Replace MLoop struct with generic attributes
Implements #102359.
Split the `MLoop` struct into two separate integer arrays called
`corner_verts` and `corner_edges`, referring to the vertex each corner
is attached to and the next edge around the face at each corner. These
arrays can be sliced to give access to the edges or vertices in a face.
Then they are often referred to as "poly_verts" or "poly_edges".
The main benefits are halving the necessary memory bandwidth when only
one array is used and simplifications from using regular integer indices
instead of a special-purpose struct.
The commit also starts a renaming from "loop" to "corner" in mesh code.
Like the other mesh struct of array refactors, forward compatibility is
kept by writing files with the older format. This will be done until 4.0
to ease the transition process.
Looking at a small portion of the patch should give a good impression
for the rest of the changes. I tried to make the changes as small as
possible so it's easy to tell the correctness from the diff. Though I
found Blender developers have been very inventive over the last decade
when finding different ways to loop over the corners in a face.
For performance, nearly every piece of code that deals with `Mesh` is
slightly impacted. Any algorithm that is memory bottle-necked should
see an improvement. For example, here is a comparison of interpolating
a vertex float attribute to face corners (Ryzen 3700x):
**Before** (Average: 3.7 ms, Min: 3.4 ms)
```
threading::parallel_for(loops.index_range(), 4096, [&](IndexRange range) {
for (const int64_t i : range) {
dst[i] = src[loops[i].v];
}
});
```
**After** (Average: 2.9 ms, Min: 2.6 ms)
```
array_utils::gather(src, corner_verts, dst);
```
That's an improvement of 28% to the average timings, and it's also a
simplification, since an index-based routine can be used instead.
For more examples using the new arrays, see the design task.
Pull Request: https://projects.blender.org/blender/blender/pulls/104424
2023-03-20 15:55:13 +01:00
|
|
|
CustomData_add_layer_named(&ldata, CD_PROP_INT32, CD_SET_DEFAULT, totloop, ".corner_vert");
|
|
|
|
|
}
|
2023-05-17 16:16:54 -04:00
|
|
|
if (!CustomData_has_layer_named(&ldata, CD_PROP_INT32, ".corner_edge")) {
|
Mesh: Replace MLoop struct with generic attributes
Implements #102359.
Split the `MLoop` struct into two separate integer arrays called
`corner_verts` and `corner_edges`, referring to the vertex each corner
is attached to and the next edge around the face at each corner. These
arrays can be sliced to give access to the edges or vertices in a face.
Then they are often referred to as "poly_verts" or "poly_edges".
The main benefits are halving the necessary memory bandwidth when only
one array is used and simplifications from using regular integer indices
instead of a special-purpose struct.
The commit also starts a renaming from "loop" to "corner" in mesh code.
Like the other mesh struct of array refactors, forward compatibility is
kept by writing files with the older format. This will be done until 4.0
to ease the transition process.
Looking at a small portion of the patch should give a good impression
for the rest of the changes. I tried to make the changes as small as
possible so it's easy to tell the correctness from the diff. Though I
found Blender developers have been very inventive over the last decade
when finding different ways to loop over the corners in a face.
For performance, nearly every piece of code that deals with `Mesh` is
slightly impacted. Any algorithm that is memory bottle-necked should
see an improvement. For example, here is a comparison of interpolating
a vertex float attribute to face corners (Ryzen 3700x):
**Before** (Average: 3.7 ms, Min: 3.4 ms)
```
threading::parallel_for(loops.index_range(), 4096, [&](IndexRange range) {
for (const int64_t i : range) {
dst[i] = src[loops[i].v];
}
});
```
**After** (Average: 2.9 ms, Min: 2.6 ms)
```
array_utils::gather(src, corner_verts, dst);
```
That's an improvement of 28% to the average timings, and it's also a
simplification, since an index-based routine can be used instead.
For more examples using the new arrays, see the design task.
Pull Request: https://projects.blender.org/blender/blender/pulls/104424
2023-03-20 15:55:13 +01:00
|
|
|
CustomData_add_layer_named(&ldata, CD_PROP_INT32, CD_SET_DEFAULT, totloop, ".corner_edge");
|
2019-04-22 09:19:45 +10:00
|
|
|
}
|
2011-09-01 09:11:00 +00:00
|
|
|
|
2022-02-22 11:14:50 -05:00
|
|
|
BKE_mesh_runtime_clear_cache(mesh);
|
|
|
|
|
|
2023-12-19 20:38:59 -05:00
|
|
|
CustomData_free(&mesh->corner_data, mesh->corners_num);
|
|
|
|
|
mesh->corner_data = ldata;
|
2011-09-01 09:11:00 +00:00
|
|
|
|
2023-12-20 02:21:48 +01:00
|
|
|
mesh->corners_num = totloop;
|
Mesh: Replace MPoly struct with offset indices
Implements #95967.
Currently the `MPoly` struct is 12 bytes, and stores the index of a
face's first corner and the number of corners/verts/edges. Polygons
and corners are always created in order by Blender, meaning each
face's corners will be after the previous face's corners. We can take
advantage of this fact and eliminate the redundancy in mesh face
storage by only storing a single integer corner offset for each face.
The size of the face is then encoded by the offset of the next face.
The size of a single integer is 4 bytes, so this reduces memory
usage by 3 times.
The same method is used for `CurvesGeometry`, so Blender already has
an abstraction to simplify using these offsets called `OffsetIndices`.
This class is used to easily retrieve a range of corner indices for
each face. This also gives the opportunity for sharing some logic with
curves.
Another benefit of the change is that the offsets and sizes stored in
`MPoly` can no longer disagree with each other. Storing faces in the
order of their corners can simplify some code too.
Face/polygon variables now use the `IndexRange` type, which comes with
quite a few utilities that can simplify code.
Some:
- The offset integer array has to be one longer than the face count to
avoid a branch for every face, which means the data is no longer part
of the mesh's `CustomData`.
- We lose the ability to "reference" an original mesh's offset array
until more reusable CoW from #104478 is committed. That will be added
in a separate commit.
- Since they aren't part of `CustomData`, poly offsets often have to be
copied manually.
- To simplify using `OffsetIndices` in many places, some functions and
structs in headers were moved to only compile in C++.
- All meshes created by Blender use the same order for faces and face
corners, but just in case, meshes with mismatched order are fixed by
versioning code.
- `MeshPolygon.totloop` is no longer editable in RNA. This API break is
necessary here unfortunately. It should be worth it in 3.6, since
that's the best way to allow loading meshes from 4.0, which is
important for an LTS version.
Pull Request: https://projects.blender.org/blender/blender/pulls/105938
2023-04-04 20:39:28 +02:00
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
/* Keep the last face offset up to date with the corner total (they must be the same). We have
|
Mesh: Replace MPoly struct with offset indices
Implements #95967.
Currently the `MPoly` struct is 12 bytes, and stores the index of a
face's first corner and the number of corners/verts/edges. Polygons
and corners are always created in order by Blender, meaning each
face's corners will be after the previous face's corners. We can take
advantage of this fact and eliminate the redundancy in mesh face
storage by only storing a single integer corner offset for each face.
The size of the face is then encoded by the offset of the next face.
The size of a single integer is 4 bytes, so this reduces memory
usage by 3 times.
The same method is used for `CurvesGeometry`, so Blender already has
an abstraction to simplify using these offsets called `OffsetIndices`.
This class is used to easily retrieve a range of corner indices for
each face. This also gives the opportunity for sharing some logic with
curves.
Another benefit of the change is that the offsets and sizes stored in
`MPoly` can no longer disagree with each other. Storing faces in the
order of their corners can simplify some code too.
Face/polygon variables now use the `IndexRange` type, which comes with
quite a few utilities that can simplify code.
Some:
- The offset integer array has to be one longer than the face count to
avoid a branch for every face, which means the data is no longer part
of the mesh's `CustomData`.
- We lose the ability to "reference" an original mesh's offset array
until more reusable CoW from #104478 is committed. That will be added
in a separate commit.
- Since they aren't part of `CustomData`, poly offsets often have to be
copied manually.
- To simplify using `OffsetIndices` in many places, some functions and
structs in headers were moved to only compile in C++.
- All meshes created by Blender use the same order for faces and face
corners, but just in case, meshes with mismatched order are fixed by
versioning code.
- `MeshPolygon.totloop` is no longer editable in RNA. This API break is
necessary here unfortunately. It should be worth it in 3.6, since
that's the best way to allow loading meshes from 4.0, which is
important for an LTS version.
Pull Request: https://projects.blender.org/blender/blender/pulls/105938
2023-04-04 20:39:28 +02:00
|
|
|
* to be careful here though, since the mesh may not be in a valid state at this point. */
|
2023-07-24 22:06:55 +02:00
|
|
|
if (mesh->face_offset_indices) {
|
2023-12-20 02:21:48 +01:00
|
|
|
mesh->face_offsets_for_write().last() = mesh->corners_num;
|
Mesh: Replace MPoly struct with offset indices
Implements #95967.
Currently the `MPoly` struct is 12 bytes, and stores the index of a
face's first corner and the number of corners/verts/edges. Polygons
and corners are always created in order by Blender, meaning each
face's corners will be after the previous face's corners. We can take
advantage of this fact and eliminate the redundancy in mesh face
storage by only storing a single integer corner offset for each face.
The size of the face is then encoded by the offset of the next face.
The size of a single integer is 4 bytes, so this reduces memory
usage by 3 times.
The same method is used for `CurvesGeometry`, so Blender already has
an abstraction to simplify using these offsets called `OffsetIndices`.
This class is used to easily retrieve a range of corner indices for
each face. This also gives the opportunity for sharing some logic with
curves.
Another benefit of the change is that the offsets and sizes stored in
`MPoly` can no longer disagree with each other. Storing faces in the
order of their corners can simplify some code too.
Face/polygon variables now use the `IndexRange` type, which comes with
quite a few utilities that can simplify code.
Some:
- The offset integer array has to be one longer than the face count to
avoid a branch for every face, which means the data is no longer part
of the mesh's `CustomData`.
- We lose the ability to "reference" an original mesh's offset array
until more reusable CoW from #104478 is committed. That will be added
in a separate commit.
- Since they aren't part of `CustomData`, poly offsets often have to be
copied manually.
- To simplify using `OffsetIndices` in many places, some functions and
structs in headers were moved to only compile in C++.
- All meshes created by Blender use the same order for faces and face
corners, but just in case, meshes with mismatched order are fixed by
versioning code.
- `MeshPolygon.totloop` is no longer editable in RNA. This API break is
necessary here unfortunately. It should be worth it in 3.6, since
that's the best way to allow loading meshes from 4.0, which is
important for an LTS version.
Pull Request: https://projects.blender.org/blender/blender/pulls/105938
2023-04-04 20:39:28 +02:00
|
|
|
}
|
2011-09-01 09:11:00 +00:00
|
|
|
}
|
|
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
static void mesh_add_faces(Mesh *mesh, int len)
|
2011-09-01 09:11:00 +00:00
|
|
|
{
|
Mesh: Move selection flags to generic attributes
Using the attribute name semantics from T97452, this patch moves the
selection status of mesh elements from the `SELECT` of vertices, and
edges, and the `ME_FACE_SEL` of faces to generic boolean attribute
Storing this data as generic attributes can significantly simplify and
improve code, as described in T95965.
The attributes are called `.select_vert`, `.select_edge`, and
`.select_poly`. The `.` prefix means they are "UI attributes",so they
still contain original data edited by users, but they aren't meant to
be accessed procedurally by the user in arbitrary situations. They are
also be hidden in the spreadsheet and the attribute list.
Until 4.0, the attributes are still written to and read from the mesh
in the old way, so neither forward nor backward compatibility are
affected. This means memory requirements will be increased by one byte
per element when selection is used. When the flags are removed
completely, requirements will decrease.
Further notes:
* The `MVert` flag is empty at runtime now, so it can be ignored.
* `BMesh` is unchanged, otherwise the change would be much larger.
* Many tests have slightly different results, since the selection
attribute uses more generic propagation. Previously you couldn't
really rely on edit mode selections being propagated procedurally.
Now it mostly works as expected.
Similar to 2480b55f216c
Ref T95965
Differential Revision: https://developer.blender.org/D15795
2022-09-23 09:38:37 -05:00
|
|
|
using namespace blender;
|
2023-07-25 21:15:52 +02:00
|
|
|
CustomData face_data;
|
2023-07-24 22:06:55 +02:00
|
|
|
int faces_num;
|
2011-09-01 09:11:00 +00:00
|
|
|
|
2019-04-22 09:19:45 +10:00
|
|
|
if (len == 0) {
|
2011-09-01 09:11:00 +00:00
|
|
|
return;
|
2019-04-22 09:19:45 +10:00
|
|
|
}
|
2011-09-01 09:11:00 +00:00
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
faces_num = mesh->faces_num + len; /* new face count */
|
2011-09-01 09:11:00 +00:00
|
|
|
|
|
|
|
|
/* update customdata */
|
2023-07-25 21:15:52 +02:00
|
|
|
CustomData_copy_layout(
|
|
|
|
|
&mesh->face_data, &face_data, CD_MASK_MESH.pmask, CD_SET_DEFAULT, faces_num);
|
|
|
|
|
CustomData_copy_data(&mesh->face_data, &face_data, 0, 0, mesh->faces_num);
|
2023-07-24 22:06:55 +02:00
|
|
|
|
|
|
|
|
implicit_sharing::resize_trivial_array(&mesh->face_offset_indices,
|
|
|
|
|
&mesh->runtime->face_offsets_sharing_info,
|
|
|
|
|
mesh->faces_num == 0 ? 0 : (mesh->faces_num + 1),
|
|
|
|
|
faces_num + 1);
|
2023-04-14 17:58:13 +02:00
|
|
|
/* Set common values for convenience. */
|
2023-07-24 22:06:55 +02:00
|
|
|
mesh->face_offset_indices[0] = 0;
|
2023-12-20 02:21:48 +01:00
|
|
|
mesh->face_offset_indices[faces_num] = mesh->corners_num;
|
2011-09-01 09:11:00 +00:00
|
|
|
|
2023-07-25 21:15:52 +02:00
|
|
|
CustomData_free(&mesh->face_data, mesh->faces_num);
|
|
|
|
|
mesh->face_data = face_data;
|
2011-09-01 09:11:00 +00:00
|
|
|
|
2022-02-22 11:14:50 -05:00
|
|
|
BKE_mesh_runtime_clear_cache(mesh);
|
|
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
mesh->faces_num = faces_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
|
|
|
|
Mesh: Move selection flags to generic attributes
Using the attribute name semantics from T97452, this patch moves the
selection status of mesh elements from the `SELECT` of vertices, and
edges, and the `ME_FACE_SEL` of faces to generic boolean attribute
Storing this data as generic attributes can significantly simplify and
improve code, as described in T95965.
The attributes are called `.select_vert`, `.select_edge`, and
`.select_poly`. The `.` prefix means they are "UI attributes",so they
still contain original data edited by users, but they aren't meant to
be accessed procedurally by the user in arbitrary situations. They are
also be hidden in the spreadsheet and the attribute list.
Until 4.0, the attributes are still written to and read from the mesh
in the old way, so neither forward nor backward compatibility are
affected. This means memory requirements will be increased by one byte
per element when selection is used. When the flags are removed
completely, requirements will decrease.
Further notes:
* The `MVert` flag is empty at runtime now, so it can be ignored.
* `BMesh` is unchanged, otherwise the change would be much larger.
* Many tests have slightly different results, since the selection
attribute uses more generic propagation. Previously you couldn't
really rely on edit mode selections being propagated procedurally.
Now it mostly works as expected.
Similar to 2480b55f216c
Ref T95965
Differential Revision: https://developer.blender.org/D15795
2022-09-23 09:38:37 -05:00
|
|
|
bke::MutableAttributeAccessor attributes = mesh->attributes_for_write();
|
|
|
|
|
bke::SpanAttributeWriter<bool> select_poly = attributes.lookup_or_add_for_write_span<bool>(
|
2023-12-20 13:13:16 -05:00
|
|
|
".select_poly", bke::AttrDomain::Face);
|
Mesh: Move selection flags to generic attributes
Using the attribute name semantics from T97452, this patch moves the
selection status of mesh elements from the `SELECT` of vertices, and
edges, and the `ME_FACE_SEL` of faces to generic boolean attribute
Storing this data as generic attributes can significantly simplify and
improve code, as described in T95965.
The attributes are called `.select_vert`, `.select_edge`, and
`.select_poly`. The `.` prefix means they are "UI attributes",so they
still contain original data edited by users, but they aren't meant to
be accessed procedurally by the user in arbitrary situations. They are
also be hidden in the spreadsheet and the attribute list.
Until 4.0, the attributes are still written to and read from the mesh
in the old way, so neither forward nor backward compatibility are
affected. This means memory requirements will be increased by one byte
per element when selection is used. When the flags are removed
completely, requirements will decrease.
Further notes:
* The `MVert` flag is empty at runtime now, so it can be ignored.
* `BMesh` is unchanged, otherwise the change would be much larger.
* Many tests have slightly different results, since the selection
attribute uses more generic propagation. Previously you couldn't
really rely on edit mode selections being propagated procedurally.
Now it mostly works as expected.
Similar to 2480b55f216c
Ref T95965
Differential Revision: https://developer.blender.org/D15795
2022-09-23 09:38:37 -05:00
|
|
|
select_poly.span.take_back(len).fill(true);
|
|
|
|
|
select_poly.finish();
|
2011-09-01 09:11:00 +00:00
|
|
|
}
|
|
|
|
|
|
2019-10-20 18:09:41 +11:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Add Geometry
|
|
|
|
|
* \{ */
|
|
|
|
|
|
|
|
|
|
void ED_mesh_verts_add(Mesh *mesh, ReportList *reports, int count)
|
2012-01-20 02:10:09 +00:00
|
|
|
{
|
2024-03-21 23:18:49 +01:00
|
|
|
if (mesh->runtime->edit_mesh) {
|
2019-10-20 18:09:41 +11:00
|
|
|
BKE_report(reports, RPT_ERROR, "Cannot add vertices in edit mode");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
mesh_add_verts(mesh, count);
|
|
|
|
|
}
|
2012-01-20 02:10:09 +00:00
|
|
|
|
2019-10-20 18:09:41 +11:00
|
|
|
void ED_mesh_edges_add(Mesh *mesh, ReportList *reports, int count)
|
|
|
|
|
{
|
2024-03-21 23:18:49 +01:00
|
|
|
if (mesh->runtime->edit_mesh) {
|
2019-10-20 18:09:41 +11:00
|
|
|
BKE_report(reports, RPT_ERROR, "Cannot add edges in edit mode");
|
2012-01-20 02:10:09 +00:00
|
|
|
return;
|
2019-04-22 09:19:45 +10:00
|
|
|
}
|
2019-10-20 18:09:41 +11:00
|
|
|
mesh_add_edges(mesh, count);
|
|
|
|
|
}
|
2012-01-20 02:10:09 +00:00
|
|
|
|
2019-10-20 18:09:41 +11:00
|
|
|
void ED_mesh_loops_add(Mesh *mesh, ReportList *reports, int count)
|
|
|
|
|
{
|
2024-03-21 23:18:49 +01:00
|
|
|
if (mesh->runtime->edit_mesh) {
|
2019-10-20 18:09:41 +11:00
|
|
|
BKE_report(reports, RPT_ERROR, "Cannot add loops in edit mode");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
mesh_add_loops(mesh, count);
|
|
|
|
|
}
|
2012-01-20 02:10:09 +00:00
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
void ED_mesh_faces_add(Mesh *mesh, ReportList *reports, int count)
|
2019-10-20 18:09:41 +11:00
|
|
|
{
|
2024-03-21 23:18:49 +01:00
|
|
|
if (mesh->runtime->edit_mesh) {
|
2023-07-24 22:06:55 +02:00
|
|
|
BKE_report(reports, RPT_ERROR, "Cannot add faces in edit mode");
|
2019-10-20 18:09:41 +11:00
|
|
|
return;
|
|
|
|
|
}
|
2023-07-24 22:06:55 +02:00
|
|
|
mesh_add_faces(mesh, count);
|
2019-10-20 18:09:41 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** \} */
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Remove Geometry
|
|
|
|
|
* \{ */
|
|
|
|
|
|
|
|
|
|
static void mesh_remove_verts(Mesh *mesh, int len)
|
|
|
|
|
{
|
|
|
|
|
if (len == 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-12-20 02:21:48 +01:00
|
|
|
CustomData_ensure_layers_are_mutable(&mesh->vert_data, mesh->verts_num);
|
|
|
|
|
const int totvert = mesh->verts_num - len;
|
2023-07-25 21:15:52 +02:00
|
|
|
CustomData_free_elem(&mesh->vert_data, totvert, len);
|
2023-12-20 02:21:48 +01:00
|
|
|
mesh->verts_num = totvert;
|
2012-01-20 02:10:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void mesh_remove_edges(Mesh *mesh, int len)
|
|
|
|
|
{
|
2019-04-22 09:19:45 +10:00
|
|
|
if (len == 0) {
|
2012-01-20 02:10:09 +00:00
|
|
|
return;
|
2019-04-22 09:19:45 +10:00
|
|
|
}
|
2023-12-20 02:21:48 +01:00
|
|
|
CustomData_ensure_layers_are_mutable(&mesh->edge_data, mesh->edges_num);
|
|
|
|
|
const int totedge = mesh->edges_num - len;
|
2023-07-25 21:15:52 +02:00
|
|
|
CustomData_free_elem(&mesh->edge_data, totedge, len);
|
2023-12-20 02:21:48 +01:00
|
|
|
mesh->edges_num = totedge;
|
2012-01-20 02:10:09 +00:00
|
|
|
}
|
|
|
|
|
|
2019-10-20 18:09:41 +11:00
|
|
|
static void mesh_remove_loops(Mesh *mesh, int len)
|
2009-09-28 14:28:45 +00:00
|
|
|
{
|
2019-10-20 18:09:41 +11:00
|
|
|
if (len == 0) {
|
2009-09-28 14:28:45 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2023-12-19 20:38:59 -05:00
|
|
|
CustomData_ensure_layers_are_mutable(&mesh->corner_data, mesh->corners_num);
|
2023-12-20 02:21:48 +01:00
|
|
|
const int totloop = mesh->corners_num - len;
|
2023-12-19 20:38:59 -05:00
|
|
|
CustomData_free_elem(&mesh->corner_data, totloop, len);
|
2023-12-20 02:21:48 +01:00
|
|
|
mesh->corners_num = totloop;
|
2009-09-28 14:28:45 +00:00
|
|
|
}
|
2010-08-26 22:44:05 +00:00
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
static void mesh_remove_faces(Mesh *mesh, int len)
|
2010-08-26 22:44:05 +00:00
|
|
|
{
|
2019-10-20 18:09:41 +11:00
|
|
|
if (len == 0) {
|
2012-03-26 02:56:48 +00:00
|
|
|
return;
|
2010-08-26 22:44:05 +00:00
|
|
|
}
|
2023-10-11 11:34:29 +02:00
|
|
|
CustomData_ensure_layers_are_mutable(&mesh->face_data, mesh->faces_num);
|
2023-07-24 22:06:55 +02:00
|
|
|
const int faces_num = mesh->faces_num - len;
|
2023-07-25 21:15:52 +02:00
|
|
|
CustomData_free_elem(&mesh->face_data, faces_num, len);
|
2023-07-24 22:06:55 +02:00
|
|
|
mesh->faces_num = faces_num;
|
2010-08-26 22:44:05 +00:00
|
|
|
}
|
|
|
|
|
|
2019-10-20 18:09:41 +11:00
|
|
|
void ED_mesh_verts_remove(Mesh *mesh, ReportList *reports, int count)
|
2010-08-26 22:44:05 +00:00
|
|
|
{
|
2024-03-21 23:18:49 +01:00
|
|
|
if (mesh->runtime->edit_mesh) {
|
2019-10-20 18:09:41 +11:00
|
|
|
BKE_report(reports, RPT_ERROR, "Cannot remove vertices in edit mode");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-12-20 02:21:48 +01:00
|
|
|
if (count > mesh->verts_num) {
|
2019-10-20 18:09:41 +11:00
|
|
|
BKE_report(reports, RPT_ERROR, "Cannot remove more vertices than the mesh contains");
|
2010-08-26 22:44:05 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-20 18:09:41 +11:00
|
|
|
mesh_remove_verts(mesh, count);
|
2010-08-26 22:44:05 +00:00
|
|
|
}
|
2009-09-28 14:28:45 +00:00
|
|
|
|
2012-01-20 02:10:09 +00:00
|
|
|
void ED_mesh_edges_remove(Mesh *mesh, ReportList *reports, int count)
|
|
|
|
|
{
|
2024-03-21 23:18:49 +01:00
|
|
|
if (mesh->runtime->edit_mesh) {
|
2012-10-18 16:25:58 +00:00
|
|
|
BKE_report(reports, RPT_ERROR, "Cannot remove edges in edit mode");
|
2012-01-20 02:10:09 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2023-12-20 02:21:48 +01:00
|
|
|
if (count > mesh->edges_num) {
|
2012-10-18 16:25:58 +00:00
|
|
|
BKE_report(reports, RPT_ERROR, "Cannot remove more edges than the mesh contains");
|
2012-01-20 02:10:09 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-01-20 02:10:09 +00:00
|
|
|
mesh_remove_edges(mesh, count);
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-20 18:09:41 +11:00
|
|
|
void ED_mesh_loops_remove(Mesh *mesh, ReportList *reports, int count)
|
2012-01-20 02:10:09 +00:00
|
|
|
{
|
2024-03-21 23:18:49 +01:00
|
|
|
if (mesh->runtime->edit_mesh) {
|
2019-10-20 18:09:41 +11:00
|
|
|
BKE_report(reports, RPT_ERROR, "Cannot remove loops in edit mode");
|
2012-01-20 02:10:09 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2023-12-20 02:21:48 +01:00
|
|
|
if (count > mesh->corners_num) {
|
2019-10-20 18:09:41 +11:00
|
|
|
BKE_report(reports, RPT_ERROR, "Cannot remove more loops than the mesh contains");
|
2012-01-20 02:10:09 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-10-20 18:09:41 +11:00
|
|
|
mesh_remove_loops(mesh, count);
|
2012-01-20 02:10:09 +00:00
|
|
|
}
|
|
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
void ED_mesh_faces_remove(Mesh *mesh, ReportList *reports, int count)
|
2011-09-01 09:11:00 +00:00
|
|
|
{
|
2024-03-21 23:18:49 +01:00
|
|
|
if (mesh->runtime->edit_mesh) {
|
2019-10-20 18:09:41 +11:00
|
|
|
BKE_report(reports, RPT_ERROR, "Cannot remove polys in edit mode");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-07-24 22:06:55 +02:00
|
|
|
if (count > mesh->faces_num) {
|
2019-10-20 18:09:41 +11:00
|
|
|
BKE_report(reports, RPT_ERROR, "Cannot remove more polys than the mesh contains");
|
2012-03-26 02:56:48 +00:00
|
|
|
return;
|
2011-09-01 09:11:00 +00:00
|
|
|
}
|
|
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
mesh_remove_faces(mesh, count);
|
2011-09-01 09:11:00 +00:00
|
|
|
}
|
|
|
|
|
|
2019-10-20 18:09:41 +11:00
|
|
|
void ED_mesh_geometry_clear(Mesh *mesh)
|
2011-09-01 09:11:00 +00:00
|
|
|
{
|
2023-12-20 02:21:48 +01:00
|
|
|
mesh_remove_verts(mesh, mesh->verts_num);
|
|
|
|
|
mesh_remove_edges(mesh, mesh->edges_num);
|
|
|
|
|
mesh_remove_loops(mesh, mesh->corners_num);
|
2023-07-24 22:06:55 +02:00
|
|
|
mesh_remove_faces(mesh, mesh->faces_num);
|
2011-09-01 09:11:00 +00:00
|
|
|
}
|
|
|
|
|
|
2019-10-20 18:09:41 +11:00
|
|
|
/** \} */
|
|
|
|
|
|
2013-07-01 21:56:59 +00:00
|
|
|
void ED_mesh_report_mirror_ex(wmOperator *op, int totmirr, int totfail, char selectmode)
|
2013-06-05 05:58:51 +00:00
|
|
|
{
|
2013-07-01 21:56:59 +00:00
|
|
|
const char *elem_type;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-07-01 21:56:59 +00:00
|
|
|
if (selectmode & SCE_SELECT_VERTEX) {
|
|
|
|
|
elem_type = "vertices";
|
|
|
|
|
}
|
|
|
|
|
else if (selectmode & SCE_SELECT_EDGE) {
|
|
|
|
|
elem_type = "edges";
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
elem_type = "faces";
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-06-05 05:58:51 +00:00
|
|
|
if (totfail) {
|
2013-07-01 21:56:59 +00:00
|
|
|
BKE_reportf(
|
|
|
|
|
op->reports, RPT_WARNING, "%d %s mirrored, %d failed", totmirr, elem_type, totfail);
|
2013-06-05 05:58:51 +00:00
|
|
|
}
|
|
|
|
|
else {
|
2013-07-01 21:56:59 +00:00
|
|
|
BKE_reportf(op->reports, RPT_INFO, "%d %s mirrored", totmirr, elem_type);
|
2013-06-05 05:58:51 +00:00
|
|
|
}
|
|
|
|
|
}
|
2013-07-01 21:56:59 +00:00
|
|
|
|
|
|
|
|
void ED_mesh_report_mirror(wmOperator *op, int totmirr, int totfail)
|
|
|
|
|
{
|
|
|
|
|
ED_mesh_report_mirror_ex(op, totmirr, totfail, SCE_SELECT_VERTEX);
|
|
|
|
|
}
|
2019-07-16 15:06:25 +02:00
|
|
|
|
2023-07-14 20:57:28 +03:00
|
|
|
KeyBlock *ED_mesh_get_edit_shape_key(const Mesh *me)
|
|
|
|
|
{
|
2024-03-21 23:18:49 +01:00
|
|
|
BLI_assert(me->runtime->edit_mesh && me->runtime->edit_mesh->bm);
|
2023-07-14 20:57:28 +03:00
|
|
|
|
2024-03-21 23:18:49 +01:00
|
|
|
return BKE_keyblock_find_by_index(me->key, me->runtime->edit_mesh->bm->shapenr - 1);
|
2023-07-14 20:57:28 +03:00
|
|
|
}
|
|
|
|
|
|
2022-05-15 20:41:11 +02:00
|
|
|
Mesh *ED_mesh_context(bContext *C)
|
2019-07-16 15:06:25 +02:00
|
|
|
{
|
2022-05-15 20:41:11 +02:00
|
|
|
Mesh *mesh = static_cast<Mesh *>(CTX_data_pointer_get_type(C, "mesh", &RNA_Mesh).data);
|
|
|
|
|
if (mesh != nullptr) {
|
2019-07-16 15:06:25 +02:00
|
|
|
return mesh;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-28 01:30:38 +01:00
|
|
|
Object *ob = blender::ed::object::context_active_object(C);
|
2022-05-15 20:41:11 +02:00
|
|
|
if (ob == nullptr) {
|
|
|
|
|
return nullptr;
|
2019-07-16 15:06:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ID *data = (ID *)ob->data;
|
2022-05-15 20:41:11 +02:00
|
|
|
if (data == nullptr || GS(data->name) != ID_ME) {
|
|
|
|
|
return nullptr;
|
2019-07-16 15:06:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (Mesh *)data;
|
|
|
|
|
}
|
2022-12-13 18:39:36 -06:00
|
|
|
|
|
|
|
|
void ED_mesh_split_faces(Mesh *mesh)
|
|
|
|
|
{
|
|
|
|
|
using namespace blender;
|
2023-07-24 22:06:55 +02:00
|
|
|
const OffsetIndices polys = mesh->faces();
|
Mesh: Replace MLoop struct with generic attributes
Implements #102359.
Split the `MLoop` struct into two separate integer arrays called
`corner_verts` and `corner_edges`, referring to the vertex each corner
is attached to and the next edge around the face at each corner. These
arrays can be sliced to give access to the edges or vertices in a face.
Then they are often referred to as "poly_verts" or "poly_edges".
The main benefits are halving the necessary memory bandwidth when only
one array is used and simplifications from using regular integer indices
instead of a special-purpose struct.
The commit also starts a renaming from "loop" to "corner" in mesh code.
Like the other mesh struct of array refactors, forward compatibility is
kept by writing files with the older format. This will be done until 4.0
to ease the transition process.
Looking at a small portion of the patch should give a good impression
for the rest of the changes. I tried to make the changes as small as
possible so it's easy to tell the correctness from the diff. Though I
found Blender developers have been very inventive over the last decade
when finding different ways to loop over the corners in a face.
For performance, nearly every piece of code that deals with `Mesh` is
slightly impacted. Any algorithm that is memory bottle-necked should
see an improvement. For example, here is a comparison of interpolating
a vertex float attribute to face corners (Ryzen 3700x):
**Before** (Average: 3.7 ms, Min: 3.4 ms)
```
threading::parallel_for(loops.index_range(), 4096, [&](IndexRange range) {
for (const int64_t i : range) {
dst[i] = src[loops[i].v];
}
});
```
**After** (Average: 2.9 ms, Min: 2.6 ms)
```
array_utils::gather(src, corner_verts, dst);
```
That's an improvement of 28% to the average timings, and it's also a
simplification, since an index-based routine can be used instead.
For more examples using the new arrays, see the design task.
Pull Request: https://projects.blender.org/blender/blender/pulls/104424
2023-03-20 15:55:13 +01:00
|
|
|
const Span<int> corner_edges = mesh->corner_edges();
|
2023-02-16 16:59:58 -05:00
|
|
|
const bke::AttributeAccessor attributes = mesh->attributes();
|
2023-04-19 11:21:06 +02:00
|
|
|
const VArray<bool> mesh_sharp_edges = *attributes.lookup_or_default<bool>(
|
2023-12-20 13:13:16 -05:00
|
|
|
"sharp_edge", bke::AttrDomain::Edge, false);
|
2024-03-28 14:45:52 -04:00
|
|
|
const VArraySpan<bool> sharp_faces = *attributes.lookup<bool>("sharp_face",
|
|
|
|
|
bke::AttrDomain::Face);
|
2023-02-16 16:59:58 -05:00
|
|
|
|
2023-12-20 02:21:48 +01:00
|
|
|
Array<bool> sharp_edges(mesh->edges_num);
|
2023-02-16 16:59:58 -05:00
|
|
|
mesh_sharp_edges.materialize(sharp_edges);
|
2023-01-10 16:12:14 -05:00
|
|
|
|
2022-12-13 18:39:36 -06:00
|
|
|
threading::parallel_for(polys.index_range(), 1024, [&](const IndexRange range) {
|
2023-07-24 22:06:55 +02:00
|
|
|
for (const int face_i : range) {
|
2024-03-28 14:45:52 -04:00
|
|
|
if (!sharp_faces.is_empty() && sharp_faces[face_i]) {
|
2023-07-24 22:06:55 +02:00
|
|
|
for (const int edge : corner_edges.slice(polys[face_i])) {
|
Mesh: Replace MLoop struct with generic attributes
Implements #102359.
Split the `MLoop` struct into two separate integer arrays called
`corner_verts` and `corner_edges`, referring to the vertex each corner
is attached to and the next edge around the face at each corner. These
arrays can be sliced to give access to the edges or vertices in a face.
Then they are often referred to as "poly_verts" or "poly_edges".
The main benefits are halving the necessary memory bandwidth when only
one array is used and simplifications from using regular integer indices
instead of a special-purpose struct.
The commit also starts a renaming from "loop" to "corner" in mesh code.
Like the other mesh struct of array refactors, forward compatibility is
kept by writing files with the older format. This will be done until 4.0
to ease the transition process.
Looking at a small portion of the patch should give a good impression
for the rest of the changes. I tried to make the changes as small as
possible so it's easy to tell the correctness from the diff. Though I
found Blender developers have been very inventive over the last decade
when finding different ways to loop over the corners in a face.
For performance, nearly every piece of code that deals with `Mesh` is
slightly impacted. Any algorithm that is memory bottle-necked should
see an improvement. For example, here is a comparison of interpolating
a vertex float attribute to face corners (Ryzen 3700x):
**Before** (Average: 3.7 ms, Min: 3.4 ms)
```
threading::parallel_for(loops.index_range(), 4096, [&](IndexRange range) {
for (const int64_t i : range) {
dst[i] = src[loops[i].v];
}
});
```
**After** (Average: 2.9 ms, Min: 2.6 ms)
```
array_utils::gather(src, corner_verts, dst);
```
That's an improvement of 28% to the average timings, and it's also a
simplification, since an index-based routine can be used instead.
For more examples using the new arrays, see the design task.
Pull Request: https://projects.blender.org/blender/blender/pulls/104424
2023-03-20 15:55:13 +01:00
|
|
|
sharp_edges[edge] = true;
|
2022-12-13 18:39:36 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
BLI: refactor IndexMask for better performance and memory usage
Goals of this refactor:
* Reduce memory consumption of `IndexMask`. The old `IndexMask` uses an
`int64_t` for each index which is more than necessary in pretty much all
practical cases currently. Using `int32_t` might still become limiting
in the future in case we use this to index e.g. byte buffers larger than
a few gigabytes. We also don't want to template `IndexMask`, because
that would cause a split in the "ecosystem", or everything would have to
be implemented twice or templated.
* Allow for more multi-threading. The old `IndexMask` contains a single
array. This is generally good but has the problem that it is hard to fill
from multiple-threads when the final size is not known from the beginning.
This is commonly the case when e.g. converting an array of bool to an
index mask. Currently, this kind of code only runs on a single thread.
* Allow for efficient set operations like join, intersect and difference.
It should be possible to multi-thread those operations.
* It should be possible to iterate over an `IndexMask` very efficiently.
The most important part of that is to avoid all memory access when iterating
over continuous ranges. For some core nodes (e.g. math nodes), we generate
optimized code for the cases of irregular index masks and simple index ranges.
To achieve these goals, a few compromises had to made:
* Slicing of the mask (at specific indices) and random element access is
`O(log #indices)` now, but with a low constant factor. It should be possible
to split a mask into n approximately equally sized parts in `O(n)` though,
making the time per split `O(1)`.
* Using range-based for loops does not work well when iterating over a nested
data structure like the new `IndexMask`. Therefor, `foreach_*` functions with
callbacks have to be used. To avoid extra code complexity at the call site,
the `foreach_*` methods support multi-threading out of the box.
The new data structure splits an `IndexMask` into an arbitrary number of ordered
`IndexMaskSegment`. Each segment can contain at most `2^14 = 16384` indices. The
indices within a segment are stored as `int16_t`. Each segment has an additional
`int64_t` offset which allows storing arbitrary `int64_t` indices. This approach
has the main benefits that segments can be processed/constructed individually on
multiple threads without a serial bottleneck. Also it reduces the memory
requirements significantly.
For more details see comments in `BLI_index_mask.hh`.
I did a few tests to verify that the data structure generally improves
performance and does not cause regressions:
* Our field evaluation benchmarks take about as much as before. This is to be
expected because we already made sure that e.g. add node evaluation is
vectorized. The important thing here is to check that changes to the way we
iterate over the indices still allows for auto-vectorization.
* Memory usage by a mask is about 1/4 of what it was before in the average case.
That's mainly caused by the switch from `int64_t` to `int16_t` for indices.
In the worst case, the memory requirements can be larger when there are many
indices that are very far away. However, when they are far away from each other,
that indicates that there aren't many indices in total. In common cases, memory
usage can be way lower than 1/4 of before, because sub-ranges use static memory.
* For some more specific numbers I benchmarked `IndexMask::from_bools` in
`index_mask_from_selection` on 10.000.000 elements at various probabilities for
`true` at every index:
```
Probability Old New
0 4.6 ms 0.8 ms
0.001 5.1 ms 1.3 ms
0.2 8.4 ms 1.8 ms
0.5 15.3 ms 3.0 ms
0.8 20.1 ms 3.0 ms
0.999 25.1 ms 1.7 ms
1 13.5 ms 1.1 ms
```
Pull Request: https://projects.blender.org/blender/blender/pulls/104629
2023-05-24 18:11:41 +02:00
|
|
|
IndexMaskMemory memory;
|
|
|
|
|
const IndexMask split_mask = IndexMask::from_bools(sharp_edges, memory);
|
2022-12-13 18:39:36 -06:00
|
|
|
if (split_mask.is_empty()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-28 14:19:53 -04:00
|
|
|
geometry::split_edges(*mesh, split_mask, {});
|
2022-12-13 18:39:36 -06:00
|
|
|
}
|