[Bf-blender-cvs] [997dbc5c2be] temp-vert-normals-cleanup: Merge branch 'master' into temp-vert-normals-cleanup
Hans Goudey
noreply at git.blender.org
Mon Nov 1 04:43:32 CET 2021
Commit: 997dbc5c2be80caf398dc3ebff78e786442e4920
Author: Hans Goudey
Date: Sun Oct 31 22:42:55 2021 -0500
Branches: temp-vert-normals-cleanup
https://developer.blender.org/rB997dbc5c2be80caf398dc3ebff78e786442e4920
Merge branch 'master' into temp-vert-normals-cleanup
===================================================================
===================================================================
diff --cc source/blender/blenkernel/BKE_bvhutils.h
index a8b6d09ee46,bb95985ef4c..0963c7a51b2
--- a/source/blender/blenkernel/BKE_bvhutils.h
+++ b/source/blender/blenkernel/BKE_bvhutils.h
@@@ -71,8 -71,7 +71,8 @@@ typedef struct BVHTreeFromMesh
/* Vertex array, so that callbacks have instant access to data. */
const struct MVert *vert;
+ const float (*vert_normals)[3];
- const struct MEdge *edge; /* only used for BVHTreeFromMeshEdges */
+ const struct MEdge *edge; /* only used for #BVHTreeFromMeshEdges */
const struct MFace *face;
const struct MLoop *loop;
const struct MLoopTri *looptri;
diff --cc source/blender/blenkernel/intern/geometry_component_mesh.cc
index cde03002f73,c3e39c0b2cb..1d45063271c
--- a/source/blender/blenkernel/intern/geometry_component_mesh.cc
+++ b/source/blender/blenkernel/intern/geometry_component_mesh.cc
@@@ -1179,11 -1192,25 +1192,11 @@@ class NormalAttributeProvider final : p
return {};
}
- /* Use existing normals if possible. */
- if (!(mesh->runtime.cd_dirty_poly & CD_MASK_NORMAL) &&
- CustomData_has_layer(&mesh->pdata, CD_NORMAL)) {
- const void *data = CustomData_get_layer(&mesh->pdata, CD_NORMAL);
-
- return std::make_unique<fn::GVArray_For_Span<float3>>(
- Span<float3>((const float3 *)data, mesh->totpoly));
- }
-
- Array<float3> normals(mesh->totpoly);
- for (const int i : IndexRange(mesh->totpoly)) {
- const MPoly *poly = &mesh->mpoly[i];
- BKE_mesh_calc_poly_normal(poly, &mesh->mloop[poly->loopstart], mesh->mvert, normals[i]);
- }
-
- return std::make_unique<fn::GVArray_For_ArrayContainer<Array<float3>>>(std::move(normals));
+ Span<float3> face_normals{(float3 *)BKE_mesh_ensure_face_normals(mesh), mesh->totpoly};
+ return std::make_unique<fn::GVArray_For_GSpan>(face_normals);
}
- GVMutableArrayPtr try_get_for_write(GeometryComponent &UNUSED(component)) const final
+ WriteAttributeLookup try_get_for_write(GeometryComponent &UNUSED(component)) const final
{
return {};
}
diff --cc source/blender/blenkernel/intern/mesh_runtime.c
index 527e8aa65e1,1c8646a4bdd..61236764550
--- a/source/blender/blenkernel/intern/mesh_runtime.c
+++ b/source/blender/blenkernel/intern/mesh_runtime.c
@@@ -51,9 -51,9 +51,11 @@@ void BKE_mesh_runtime_reset(Mesh *mesh
{
memset(&mesh->runtime, 0, sizeof(mesh->runtime));
mesh->runtime.eval_mutex = MEM_mallocN(sizeof(ThreadMutex), "mesh runtime eval_mutex");
- BLI_mutex_init(mesh->runtime.eval_mutex);
+ mesh->runtime.normals_mutex = MEM_mallocN(sizeof(ThreadMutex), __func__);
+ mesh->runtime.render_mutex = MEM_mallocN(sizeof(ThreadMutex), "mesh runtime render_mutex");
+ BLI_mutex_init(mesh->runtime.eval_mutex);
+ BLI_mutex_init(mesh->runtime.normals_mutex);
+ BLI_mutex_init(mesh->runtime.render_mutex);
}
/* Clear all pointers which we don't want to be shared on copying the datablock.
@@@ -72,9 -72,10 +74,11 @@@ void BKE_mesh_runtime_reset_on_copy(Mes
runtime->shrinkwrap_data = NULL;
mesh->runtime.eval_mutex = MEM_mallocN(sizeof(ThreadMutex), "mesh runtime eval_mutex");
- BLI_mutex_init(mesh->runtime.eval_mutex);
-
+ mesh->runtime.normals_mutex = MEM_mallocN(sizeof(ThreadMutex), __func__);
+ mesh->runtime.render_mutex = MEM_mallocN(sizeof(ThreadMutex), "mesh runtime render_mutex");
+ BLI_mutex_init(mesh->runtime.eval_mutex);
+ BLI_mutex_init(mesh->runtime.normals_mutex);
+ BLI_mutex_init(mesh->runtime.render_mutex);
}
void BKE_mesh_runtime_clear_cache(Mesh *mesh)
@@@ -84,11 -85,11 +88,16 @@@
MEM_freeN(mesh->runtime.eval_mutex);
mesh->runtime.eval_mutex = NULL;
}
+ if (mesh->runtime.normals_mutex != NULL) {
+ BLI_mutex_end(mesh->runtime.normals_mutex);
+ MEM_freeN(mesh->runtime.normals_mutex);
+ mesh->runtime.normals_mutex = NULL;
+ }
+ if (mesh->runtime.render_mutex != NULL) {
+ BLI_mutex_end(mesh->runtime.render_mutex);
+ MEM_freeN(mesh->runtime.render_mutex);
+ mesh->runtime.render_mutex = NULL;
+ }
if (mesh->runtime.mesh_eval != NULL) {
mesh->runtime.mesh_eval->edit_mesh = NULL;
BKE_id_free(NULL, mesh->runtime.mesh_eval);
diff --cc source/blender/blenkernel/intern/subdiv_mesh.c
index 9c8b4f17d2a,29e7d0a1a3c..a66444a8430
--- a/source/blender/blenkernel/intern/subdiv_mesh.c
+++ b/source/blender/blenkernel/intern/subdiv_mesh.c
@@@ -105,11 -107,10 +105,11 @@@ static void subdiv_mesh_prepare_accumul
}
/* TODO(sergey): Technically, this is overallocating, we don't need memory
* for an inner subdivision vertices. */
- ctx->accumulated_normals = MEM_calloc_arrayN(
- num_vertices, sizeof(*ctx->accumulated_normals), "subdiv accumulated normals");
+ ctx->accumulated_normals = (float(*)[3])CustomData_add_layer(
+ &ctx->subdiv_mesh->vdata, CD_NORMAL, CD_DEFAULT, NULL, num_vertices);
+ memset(ctx->accumulated_normals, 0, sizeof(float[3]) * num_vertices);
ctx->accumulated_counters = MEM_calloc_arrayN(
- sizeof(*ctx->accumulated_counters), num_vertices, "subdiv accumulated counters");
+ num_vertices, sizeof(*ctx->accumulated_counters), "subdiv accumulated counters");
}
static void subdiv_mesh_context_free(SubdivMeshContext *ctx)
diff --cc source/blender/blenloader/intern/versioning_300.c
index 1e8a72a96f6,68faa4c0672..636fb6b6497
--- a/source/blender/blenloader/intern/versioning_300.c
+++ b/source/blender/blenloader/intern/versioning_300.c
@@@ -56,7 -56,7 +57,8 @@@
#include "BKE_idprop.h"
#include "BKE_lib_id.h"
#include "BKE_main.h"
+#include "BKE_mesh.h"
+ #include "BKE_modifier.h"
#include "BKE_node.h"
#include "RNA_access.h"
diff --cc source/blender/nodes/geometry/nodes/node_geo_input_normal.cc
index 14e446b1114,92b89313d23..5965989dc5f
--- a/source/blender/nodes/geometry/nodes/node_geo_input_normal.cc
+++ b/source/blender/nodes/geometry/nodes/node_geo_input_normal.cc
@@@ -28,19 -28,70 +28,19 @@@ namespace blender::nodes
static void geo_node_input_normal_declare(NodeDeclarationBuilder &b)
{
- b.add_output<decl::Vector>("Normal").field_source();
+ b.add_output<decl::Vector>(N_("Normal")).field_source();
}
-static GVArrayPtr mesh_face_normals(const Mesh &mesh,
- const Span<MVert> verts,
- const Span<MPoly> polys,
- const Span<MLoop> loops,
- const IndexMask mask)
+static GVArrayPtr mesh_face_normals_gvarray(const Mesh &mesh)
{
- /* Use existing normals to avoid unnecessarily recalculating them, if possible. */
- if (!(mesh.runtime.cd_dirty_poly & CD_MASK_NORMAL) &&
- CustomData_has_layer(&mesh.pdata, CD_NORMAL)) {
- const void *data = CustomData_get_layer(&mesh.pdata, CD_NORMAL);
-
- return std::make_unique<fn::GVArray_For_Span<float3>>(
- Span<float3>((const float3 *)data, polys.size()));
- }
-
- auto normal_fn = [verts, polys, loops](const int i) -> float3 {
- float3 normal;
- const MPoly &poly = polys[i];
- BKE_mesh_calc_poly_normal(&poly, &loops[poly.loopstart], verts.data(), normal);
- return normal;
- };
-
- return std::make_unique<
- fn::GVArray_For_EmbeddedVArray<float3, VArray_For_Func<float3, decltype(normal_fn)>>>(
- mask.min_array_size(), mask.min_array_size(), normal_fn);
+ Span<float3> face_normals{(float3 *)BKE_mesh_ensure_face_normals(&mesh), mesh.totpoly};
+ return std::make_unique<fn::GVArray_For_GSpan>(face_normals);
}
-static GVArrayPtr mesh_vertex_normals(const Mesh &mesh,
- const Span<MVert> verts,
- const Span<MPoly> polys,
- const Span<MLoop> loops,
- const IndexMask mask)
+static GVArrayPtr mesh_vert_normals_gvarray(const Mesh &mesh)
{
- /* Use existing normals to avoid unnecessarily recalculating them, if possible. */
- if (!(mesh.runtime.cd_dirty_vert & CD_MASK_NORMAL) &&
- CustomData_has_layer(&mesh.vdata, CD_NORMAL)) {
- const void *data = CustomData_get_layer(&mesh.pdata, CD_NORMAL);
-
- return std::make_unique<fn::GVArray_For_Span<float3>>(
- Span<float3>((const float3 *)data, mesh.totvert));
- }
-
- /* If the normals are dirty, they must be recalculated for the output of this node's field
- * source. Ideally vertex normals could be calculated lazily on a const mesh, but that's not
- * possible at the moment, so we take ownership of the results. Sadly we must also create a copy
- * of MVert to use the mesh normals API. This can be improved by adding mutex-protected lazy
- * calculation of normals on meshes.
- *
- * Use mask.min_array_size() to avoid calculating a final chunk of data if possible. */
- Array<MVert> temp_verts(verts);
- Array<float3> normals(verts.size()); /* Use full size for accumulation from faces. */
- BKE_mesh_calc_normals_poly_and_vertex(temp_verts.data(),
- mask.min_array_size(),
- loops.data(),
- loops.size(),
- polys.data(),
- polys.size(),
- nullptr,
- (float(*)[3])normals.data());
-
- return std::make_unique<fn::GVArray_For_ArrayContainer<Array<float3>>>(std::move(normals));
+ Span<float3> vert_normals{(float3 *)BKE_mesh_ensure_vertex_normals(&mesh), mesh.totvert};
+ return std::make_unique<fn::GVArray_For_GSpan>(vert_normals);
}
static const GVArray *construct_mesh_normals_gvarray(const MeshComponent &mesh_component,
More information about the Bf-blender-cvs
mailing list