Cleanup: Miscellaneous improvements to subdivision geometry nodes

- Implement mesh creation in GeometrySet agnostic way, with less context
- Remove useless comments
- Use a field to clamp crease values instead of clamping when copying
- Small variable name tweaks
- Use `BKE_subdiv_new_from_mesh` instead of "update"
- Remove crease layers before writing, avoiding potential copy
This commit is contained in:
Hans Goudey
2023-04-23 15:24:25 -04:00
parent e7eb8fd908
commit 993d24c174
4 changed files with 137 additions and 139 deletions

View File

@@ -15,6 +15,14 @@ namespace blender::array_utils {
* grain-size.
*/
void copy(const GVArray &src, GMutableSpan dst, int64_t grain_size = 4096);
template<typename T>
inline void copy(const VArray<T> &src, MutableSpan<T> dst, const int64_t grain_size = 4096)
{
BLI_assert(src.size() == dst.size());
threading::parallel_for(src.index_range(), grain_size, [&](const IndexRange range) {
src.materialize_to_uninitialized(range, dst);
});
}
/**
* Fill the destination span by copying all values from the `src` array. Threaded based on
@@ -28,6 +36,7 @@ inline void copy(const Span<T> src, MutableSpan<T> dst, const int64_t grain_size
dst.slice(range).copy_from(src.slice(range));
});
}
/**
* Fill the destination span by copying masked values from the `src` array. Threaded based on
* grain-size.

View File

@@ -571,7 +571,7 @@ static Mesh *subdivide_edit_mesh(const Object *object,
mesh_settings.resolution = (1 << smd->levels) + 1;
mesh_settings.use_optimal_display = (smd->flags & eSubsurfModifierFlag_ControlEdges);
Subdiv *subdiv = BKE_subdiv_update_from_mesh(nullptr, &settings, me_from_em);
Subdiv *subdiv = BKE_subdiv_new_from_mesh(&settings, me_from_em);
Mesh *result = BKE_subdiv_to_mesh(subdiv, &mesh_settings, me_from_em);
BKE_id_free(nullptr, me_from_em);
BKE_subdiv_free(subdiv);

View File

@@ -18,14 +18,8 @@ static void node_declare(NodeDeclarationBuilder &b)
b.add_output<decl::Geometry>(N_("Mesh")).propagate_all();
}
static void geometry_set_mesh_subdivide(GeometrySet &geometry_set, const int level)
static Mesh *simple_subdivide_mesh(const Mesh &mesh, const int level)
{
if (!geometry_set.has_mesh()) {
return;
}
const Mesh *mesh_in = geometry_set.get_mesh_for_read();
/* Initialize mesh settings. */
SubdivToMeshSettings mesh_settings;
mesh_settings.resolution = (1 << level) + 1;
@@ -42,43 +36,38 @@ static void geometry_set_mesh_subdivide(GeometrySet &geometry_set, const int lev
subdiv_settings.fvar_linear_interpolation = BKE_subdiv_fvar_interpolation_from_uv_smooth(0);
/* Apply subdivision from mesh. */
Subdiv *subdiv = BKE_subdiv_update_from_mesh(nullptr, &subdiv_settings, mesh_in);
/* In case of bad topology, skip to input mesh. */
if (subdiv == nullptr) {
return;
Subdiv *subdiv = BKE_subdiv_new_from_mesh(&subdiv_settings, &mesh);
if (!subdiv) {
return nullptr;
}
Mesh *mesh_out = BKE_subdiv_to_mesh(subdiv, &mesh_settings, mesh_in);
MeshComponent &mesh_component = geometry_set.get_component_for_write<MeshComponent>();
mesh_component.replace(mesh_out);
Mesh *result = BKE_subdiv_to_mesh(subdiv, &mesh_settings, &mesh);
BKE_subdiv_free(subdiv);
return result;
}
static void node_geo_exec(GeoNodeExecParams params)
{
GeometrySet geometry_set = params.extract_input<GeometrySet>("Mesh");
#ifndef WITH_OPENSUBDIV
params.error_message_add(NodeWarningType::Error,
TIP_("Disabled, Blender was compiled without OpenSubdiv"));
params.set_default_remaining_outputs();
return;
#endif
#ifdef WITH_OPENSUBDIV
/* See CCGSUBSURF_LEVEL_MAX for max limit. */
const int subdiv_level = clamp_i(params.extract_input<int>("Level"), 0, 11);
if (subdiv_level == 0) {
const int level = clamp_i(params.extract_input<int>("Level"), 0, 11);
if (level == 0) {
params.set_output("Mesh", std::move(geometry_set));
return;
}
geometry_set.modify_geometry_sets(
[&](GeometrySet &geometry_set) { geometry_set_mesh_subdivide(geometry_set, subdiv_level); });
geometry_set.modify_geometry_sets([&](GeometrySet &geometry_set) {
if (const Mesh *mesh = geometry_set.get_mesh_for_read()) {
geometry_set.replace_mesh(simple_subdivide_mesh(*mesh, level));
}
});
#else
params.error_message_add(NodeWarningType::Error,
TIP_("Disabled, Blender was compiled without OpenSubdiv"));
#endif
params.set_output("Mesh", std::move(geometry_set));
}

View File

@@ -1,12 +1,12 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */
#include "BLI_array_utils.hh"
#include "BLI_task.hh"
#include "DNA_mesh_types.h"
#include "DNA_meshdata_types.h"
#include "DNA_modifier_types.h"
#include "BKE_attribute.hh"
#include "BKE_lib_id.h"
#include "BKE_mesh.hh"
#include "BKE_subdiv.h"
#include "BKE_subdiv_mesh.hh"
@@ -54,136 +54,136 @@ static void node_init(bNodeTree * /*tree*/, bNode *node)
}
#ifdef WITH_OPENSUBDIV
static void materialize_and_clamp_creases(const VArray<float> &crease_varray,
MutableSpan<float> creases)
static void write_vert_creases(Mesh &mesh, const VArray<float> &creases)
{
threading::parallel_for(creases.index_range(), 1024, [&](IndexRange range) {
crease_varray.materialize(range, creases);
for (const int i : range) {
creases[i] = std::clamp(creases[i], 0.0f, 1.0f);
}
});
CustomData_free_layers(&mesh.vdata, CD_CREASE, mesh.totvert);
float *layer = static_cast<float *>(
CustomData_add_layer(&mesh.vdata, CD_CREASE, CD_CONSTRUCT, mesh.totvert));
array_utils::copy(creases, {layer, mesh.totvert});
}
static void write_vertex_creases(Mesh &mesh, const VArray<float> &crease_varray)
static void write_edge_creases(Mesh &mesh, const VArray<float> &creases)
{
float *crease;
if (CustomData_has_layer(&mesh.vdata, CD_CREASE)) {
crease = static_cast<float *>(
CustomData_get_layer_for_write(&mesh.vdata, CD_CREASE, mesh.totvert));
bke::MutableAttributeAccessor attributes = mesh.attributes_for_write();
attributes.remove("crease");
attributes.add<float>("crease", ATTR_DOMAIN_EDGE, bke::AttributeInitVArray(creases));
}
static bool varray_is_single_zero(const VArray<float> &varray)
{
if (const std::optional<float> value = varray.get_if_single()) {
return *value == 0.0f;
}
else {
crease = static_cast<float *>(
CustomData_add_layer(&mesh.vdata, CD_CREASE, CD_CONSTRUCT, mesh.totvert));
return false;
}
static fn::Field<float> clamp_crease(fn::Field<float> crease_field)
{
static auto clamp_fn = mf::build::SI1_SO<float, float>(
"Clamp",
[](float value) { return std::clamp(value, 0.0f, 1.0f); },
mf::build::exec_presets::AllSpanOrSingle());
return fn::Field<float>(fn::FieldOperation::Create(clamp_fn, {std::move(crease_field)}));
}
static Mesh *mesh_subsurf_calc(const Mesh *mesh,
const int level,
const Field<float> &vert_crease_field,
const Field<float> &edge_crease_field,
const int boundary_smooth,
const int uv_smooth)
{
const bke::MeshFieldContext point_context{*mesh, ATTR_DOMAIN_POINT};
FieldEvaluator point_evaluator(point_context, mesh->totvert);
point_evaluator.add(clamp_crease(vert_crease_field));
point_evaluator.evaluate();
const bke::MeshFieldContext edge_context{*mesh, ATTR_DOMAIN_EDGE};
FieldEvaluator edge_evaluator(edge_context, mesh->totedge);
edge_evaluator.add(clamp_crease(edge_crease_field));
edge_evaluator.evaluate();
const VArray<float> vert_creases = point_evaluator.get_evaluated<float>(0);
const VArray<float> edge_creases = edge_evaluator.get_evaluated<float>(0);
const bool use_creases = !varray_is_single_zero(vert_creases) ||
!varray_is_single_zero(edge_creases);
Mesh *mesh_copy = nullptr;
if (use_creases) {
/* Due to the "BKE_subdiv" API, the crease layers must be on the input mesh. But in this node
* they are provided as separate inputs, not as custom data layers. When needed, retrieve the
* mesh with write access and store the new crease values there. */
mesh_copy = BKE_mesh_copy_for_eval(mesh);
write_vert_creases(*mesh_copy, vert_creases);
write_edge_creases(*mesh_copy, edge_creases);
mesh = mesh_copy;
}
materialize_and_clamp_creases(crease_varray, {crease, mesh.totvert});
SubdivToMeshSettings mesh_settings;
mesh_settings.resolution = (1 << level) + 1;
mesh_settings.use_optimal_display = false;
SubdivSettings subdiv_settings;
subdiv_settings.is_simple = false;
subdiv_settings.is_adaptive = false;
subdiv_settings.use_creases = use_creases;
subdiv_settings.level = level;
subdiv_settings.vtx_boundary_interpolation = BKE_subdiv_vtx_boundary_interpolation_from_subsurf(
boundary_smooth);
subdiv_settings.fvar_linear_interpolation = BKE_subdiv_fvar_interpolation_from_uv_smooth(
uv_smooth);
Subdiv *subdiv = BKE_subdiv_new_from_mesh(&subdiv_settings, mesh);
if (!subdiv) {
return nullptr;
}
Mesh *result = BKE_subdiv_to_mesh(subdiv, &mesh_settings, mesh);
BKE_subdiv_free(subdiv);
if (use_creases) {
/* Remove the layer in case it was created by the node from the field input. The fact
* that this node uses #CD_CREASE to input creases to the subdivision code is meant to be
* an implementation detail ideally. */
CustomData_free_layers(&result->edata, CD_CREASE, result->totedge);
}
if (mesh_copy) {
BKE_id_free(nullptr, mesh_copy);
}
return result;
}
static void write_edge_creases(Mesh &mesh, const VArray<float> &crease_varray)
{
bke::SpanAttributeWriter<float> attribute =
mesh.attributes_for_write().lookup_or_add_for_write_only_span<float>("crease",
ATTR_DOMAIN_EDGE);
materialize_and_clamp_creases(crease_varray, attribute.span);
attribute.finish();
}
static bool varray_is_nonzero(const VArray<float> &varray)
{
return !(varray.is_single() && varray.get_internal_single() == 0.0f);
}
#endif
static void node_geo_exec(GeoNodeExecParams params)
{
GeometrySet geometry_set = params.extract_input<GeometrySet>("Mesh");
#ifndef WITH_OPENSUBDIV
params.error_message_add(NodeWarningType::Error,
TIP_("Disabled, Blender was compiled without OpenSubdiv"));
#else
Field<float> edge_crease_field = params.extract_input<Field<float>>("Edge Crease");
Field<float> vertex_crease_field = params.extract_input<Field<float>>("Vertex Crease");
#ifdef WITH_OPENSUBDIV
const Field<float> vert_crease = params.extract_input<Field<float>>("Vertex Crease");
const Field<float> edge_crease = params.extract_input<Field<float>>("Edge Crease");
const NodeGeometrySubdivisionSurface &storage = node_storage(params.node());
const int uv_smooth = storage.uv_smooth;
const int boundary_smooth = storage.boundary_smooth;
const int subdiv_level = clamp_i(params.extract_input<int>("Level"), 0, 30);
/* Only process subdivision if level is greater than 0. */
if (subdiv_level == 0) {
const int level = std::clamp(params.extract_input<int>("Level"), 0, 11);
if (level == 0) {
params.set_output("Mesh", std::move(geometry_set));
return;
}
geometry_set.modify_geometry_sets([&](GeometrySet &geometry_set) {
const Mesh *mesh = geometry_set.get_mesh_for_read();
if (!mesh) {
return;
if (const Mesh *mesh = geometry_set.get_mesh_for_read()) {
geometry_set.replace_mesh(
mesh_subsurf_calc(mesh, level, vert_crease, edge_crease, boundary_smooth, uv_smooth));
}
if (mesh->totvert == 0 || mesh->totedge == 0) {
return;
}
bke::MeshFieldContext point_context{*mesh, ATTR_DOMAIN_POINT};
FieldEvaluator point_evaluator(point_context, mesh->totvert);
point_evaluator.add(vertex_crease_field);
point_evaluator.evaluate();
const VArray<float> vertex_creases = point_evaluator.get_evaluated<float>(0);
bke::MeshFieldContext edge_context{*mesh, ATTR_DOMAIN_EDGE};
FieldEvaluator edge_evaluator(edge_context, mesh->totedge);
edge_evaluator.add(edge_crease_field);
edge_evaluator.evaluate();
const VArray<float> edge_creases = edge_evaluator.get_evaluated<float>(0);
const bool use_creases = varray_is_nonzero(vertex_creases) || varray_is_nonzero(edge_creases);
if (use_creases) {
/* Due to the "BKE_subdiv" API, the crease layers must be on the input mesh. But in this node
* they are provided as separate inputs, not as custom data layers. When needed, retrieve the
* mesh with write access and store the new crease values there. */
Mesh &mesh_for_write = *geometry_set.get_mesh_for_write();
write_vertex_creases(mesh_for_write, vertex_creases);
write_edge_creases(mesh_for_write, edge_creases);
mesh = &mesh_for_write;
}
/* Initialize mesh settings. */
SubdivToMeshSettings mesh_settings;
mesh_settings.resolution = (1 << subdiv_level) + 1;
mesh_settings.use_optimal_display = false;
/* Initialize subdivision settings. */
SubdivSettings subdiv_settings;
subdiv_settings.is_simple = false;
subdiv_settings.is_adaptive = false;
subdiv_settings.use_creases = use_creases;
subdiv_settings.level = subdiv_level;
subdiv_settings.vtx_boundary_interpolation =
BKE_subdiv_vtx_boundary_interpolation_from_subsurf(boundary_smooth);
subdiv_settings.fvar_linear_interpolation = BKE_subdiv_fvar_interpolation_from_uv_smooth(
uv_smooth);
/* Apply subdivision to mesh. */
Subdiv *subdiv = BKE_subdiv_update_from_mesh(nullptr, &subdiv_settings, mesh);
/* In case of bad topology, skip to input mesh. */
if (subdiv == nullptr) {
return;
}
Mesh *result = BKE_subdiv_to_mesh(subdiv, &mesh_settings, mesh);
BKE_subdiv_free(subdiv);
if (use_creases) {
/* Remove the layer in case it was created by the node from the field input. The fact
* that this node uses #CD_CREASE to input creases to the subdivision code is meant to be
* an implementation detail ideally. */
CustomData_free_layers(&result->edata, CD_CREASE, result->totedge);
}
geometry_set.replace_mesh(result);
});
#else
params.error_message_add(NodeWarningType::Error,
TIP_("Disabled, Blender was compiled without OpenSubdiv"));
#endif
params.set_output("Mesh", std::move(geometry_set));
}