Cleanup: Subdiv: Use Span and float3 for vertex positions
This commit is contained in:
@@ -8,6 +8,8 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "BLI_math_vector_types.hh"
|
||||
#include "BLI_span.hh"
|
||||
#include "BLI_sys_types.h"
|
||||
|
||||
struct Mesh;
|
||||
@@ -25,7 +27,6 @@ struct Subdiv;
|
||||
* vertex_cos are supposed to hold coordinates of the coarse mesh. */
|
||||
void deform_coarse_vertices(Subdiv *subdiv,
|
||||
const Mesh *coarse_mesh,
|
||||
float (*vertex_cos)[3],
|
||||
int num_verts);
|
||||
MutableSpan<float3> vert_positions);
|
||||
|
||||
} // namespace blender::bke::subdiv
|
||||
|
||||
@@ -8,6 +8,8 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "BLI_math_vector_types.hh"
|
||||
#include "BLI_span.hh"
|
||||
#include "BLI_sys_types.h"
|
||||
|
||||
struct Mesh;
|
||||
@@ -33,10 +35,10 @@ bool eval_begin(Subdiv *subdiv,
|
||||
* mesh. */
|
||||
bool eval_begin_from_mesh(Subdiv *subdiv,
|
||||
const Mesh *mesh,
|
||||
const float (*coarse_vertex_cos)[3],
|
||||
Span<float3> coarse_vert_positions,
|
||||
eSubdivEvaluatorType evaluator_type,
|
||||
OpenSubdiv_EvaluatorCache *evaluator_cache);
|
||||
bool eval_refine_from_mesh(Subdiv *subdiv, const Mesh *mesh, const float (*coarse_vertex_cos)[3]);
|
||||
bool eval_refine_from_mesh(Subdiv *subdiv, const Mesh *mesh, Span<float3> coarse_vert_positions);
|
||||
|
||||
/* Makes sure displacement evaluator is initialized.
|
||||
*
|
||||
|
||||
@@ -136,7 +136,7 @@ void multires_reshape_apply_base_refit_base_mesh(MultiresReshapeContext *reshape
|
||||
void multires_reshape_apply_base_refine_from_base(MultiresReshapeContext *reshape_context)
|
||||
{
|
||||
blender::bke::subdiv::eval_refine_from_mesh(
|
||||
reshape_context->subdiv, reshape_context->base_mesh, nullptr);
|
||||
reshape_context->subdiv, reshape_context->base_mesh, {});
|
||||
}
|
||||
|
||||
void multires_reshape_apply_base_refine_from_deform(MultiresReshapeContext *reshape_context)
|
||||
@@ -152,7 +152,5 @@ void multires_reshape_apply_base_refine_from_deform(MultiresReshapeContext *resh
|
||||
BKE_multires_create_deformed_base_mesh_vert_coords(depsgraph, object, mmd);
|
||||
|
||||
blender::bke::subdiv::eval_refine_from_mesh(
|
||||
reshape_context->subdiv,
|
||||
reshape_context->base_mesh,
|
||||
reinterpret_cast<float(*)[3]>(deformed_verts.data()));
|
||||
reshape_context->subdiv, reshape_context->base_mesh, deformed_verts);
|
||||
}
|
||||
|
||||
@@ -52,7 +52,7 @@ blender::bke::subdiv::Subdiv *multires_reshape_create_subdiv(Depsgraph *depsgrap
|
||||
BKE_multires_subdiv_settings_init(&subdiv_settings, mmd);
|
||||
subdiv::Subdiv *subdiv = subdiv::new_from_mesh(&subdiv_settings, base_mesh);
|
||||
if (!subdiv::eval_begin_from_mesh(
|
||||
subdiv, base_mesh, nullptr, subdiv::SUBDIV_EVALUATOR_TYPE_CPU, nullptr))
|
||||
subdiv, base_mesh, {}, subdiv::SUBDIV_EVALUATOR_TYPE_CPU, nullptr))
|
||||
{
|
||||
subdiv::free(subdiv);
|
||||
return nullptr;
|
||||
|
||||
@@ -49,7 +49,7 @@ static blender::bke::subdiv::Subdiv *subdiv_for_simple_to_catmull_clark(Object *
|
||||
subdiv::converter_free(&converter);
|
||||
|
||||
if (!subdiv::eval_begin_from_mesh(
|
||||
subdiv, base_mesh, nullptr, subdiv::SUBDIV_EVALUATOR_TYPE_CPU, nullptr))
|
||||
subdiv, base_mesh, {}, subdiv::SUBDIV_EVALUATOR_TYPE_CPU, nullptr))
|
||||
{
|
||||
subdiv::free(subdiv);
|
||||
return nullptr;
|
||||
|
||||
@@ -492,7 +492,7 @@ Mesh *BKE_subdiv_to_ccg_mesh(Subdiv &subdiv,
|
||||
{
|
||||
/* Make sure evaluator is ready. */
|
||||
stats_begin(&subdiv.stats, SUBDIV_STATS_SUBDIV_TO_CCG);
|
||||
if (!eval_begin_from_mesh(&subdiv, &coarse_mesh, nullptr, SUBDIV_EVALUATOR_TYPE_CPU, nullptr)) {
|
||||
if (!eval_begin_from_mesh(&subdiv, &coarse_mesh, {}, SUBDIV_EVALUATOR_TYPE_CPU, nullptr)) {
|
||||
if (coarse_mesh.faces_num) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@@ -33,8 +33,7 @@ struct SubdivDeformContext {
|
||||
const Mesh *coarse_mesh;
|
||||
Subdiv *subdiv;
|
||||
|
||||
float (*vertex_cos)[3];
|
||||
int num_verts;
|
||||
MutableSpan<float3> vert_positions;
|
||||
|
||||
/* Accumulated values.
|
||||
*
|
||||
@@ -84,10 +83,10 @@ static void subdiv_accumulate_vertex_displacement(SubdivDeformContext *ctx,
|
||||
/* NOTE: The storage for vertex coordinates is coming from an external world, not necessarily
|
||||
* initialized to zeroes. */
|
||||
if (ctx->accumulated_counters[vertex_index] == 0) {
|
||||
copy_v3_v3(ctx->vertex_cos[vertex_index], D);
|
||||
copy_v3_v3(ctx->vert_positions[vertex_index], D);
|
||||
}
|
||||
else {
|
||||
add_v3_v3(ctx->vertex_cos[vertex_index], D);
|
||||
add_v3_v3(ctx->vert_positions[vertex_index], D);
|
||||
}
|
||||
}
|
||||
++ctx->accumulated_counters[vertex_index];
|
||||
@@ -146,7 +145,7 @@ static void subdiv_mesh_vertex_corner(const ForeachContext *foreach_context,
|
||||
/* Displacement is accumulated in subdiv vertex position.
|
||||
* Needs to be backed up before copying data from original vertex. */
|
||||
float D[3] = {0.0f, 0.0f, 0.0f};
|
||||
float *vertex_co = ctx->vertex_cos[coarse_vertex_index];
|
||||
float *vertex_co = ctx->vert_positions[coarse_vertex_index];
|
||||
if (ctx->have_displacement) {
|
||||
copy_v3_v3(D, vertex_co);
|
||||
mul_v3_fl(D, inv_num_accumulated);
|
||||
@@ -184,13 +183,14 @@ static void setup_foreach_callbacks(const SubdivDeformContext *subdiv_context,
|
||||
|
||||
void deform_coarse_vertices(Subdiv *subdiv,
|
||||
const Mesh *coarse_mesh,
|
||||
float (*vertex_cos)[3],
|
||||
int num_verts)
|
||||
MutableSpan<float3> vert_positions)
|
||||
{
|
||||
stats_begin(&subdiv->stats, SUBDIV_STATS_SUBDIV_TO_MESH);
|
||||
/* Make sure evaluator is up to date with possible new topology, and that
|
||||
* is refined for the new positions of coarse vertices. */
|
||||
if (!eval_begin_from_mesh(subdiv, coarse_mesh, vertex_cos, SUBDIV_EVALUATOR_TYPE_CPU, nullptr)) {
|
||||
if (!eval_begin_from_mesh(
|
||||
subdiv, coarse_mesh, vert_positions, SUBDIV_EVALUATOR_TYPE_CPU, nullptr))
|
||||
{
|
||||
/* This could happen in two situations:
|
||||
* - OpenSubdiv is disabled.
|
||||
* - Something totally bad happened, and OpenSubdiv rejected our
|
||||
@@ -206,8 +206,7 @@ void deform_coarse_vertices(Subdiv *subdiv,
|
||||
SubdivDeformContext subdiv_context = {nullptr};
|
||||
subdiv_context.coarse_mesh = coarse_mesh;
|
||||
subdiv_context.subdiv = subdiv;
|
||||
subdiv_context.vertex_cos = vertex_cos;
|
||||
subdiv_context.num_verts = num_verts;
|
||||
subdiv_context.vert_positions = vert_positions;
|
||||
subdiv_context.have_displacement = (subdiv->displacement_evaluator != nullptr);
|
||||
|
||||
ForeachContext foreach_context;
|
||||
|
||||
@@ -211,7 +211,7 @@ static void get_mesh_evaluator_settings(OpenSubdiv_EvaluatorSettings *settings,
|
||||
|
||||
bool eval_begin_from_mesh(Subdiv *subdiv,
|
||||
const Mesh *mesh,
|
||||
const float (*coarse_vertex_cos)[3],
|
||||
const Span<float3> coarse_vert_positions,
|
||||
eSubdivEvaluatorType evaluator_type,
|
||||
OpenSubdiv_EvaluatorCache *evaluator_cache)
|
||||
{
|
||||
@@ -221,14 +221,16 @@ bool eval_begin_from_mesh(Subdiv *subdiv,
|
||||
if (!eval_begin(subdiv, evaluator_type, evaluator_cache, &settings)) {
|
||||
return false;
|
||||
}
|
||||
return eval_refine_from_mesh(subdiv, mesh, coarse_vertex_cos);
|
||||
return eval_refine_from_mesh(subdiv, mesh, coarse_vert_positions);
|
||||
#else
|
||||
UNUSED_VARS(subdiv, mesh, coarse_vertex_cos, evaluator_type, evaluator_cache);
|
||||
UNUSED_VARS(subdiv, mesh, coarse_vert_positions, evaluator_type, evaluator_cache);
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
bool eval_refine_from_mesh(Subdiv *subdiv, const Mesh *mesh, const float (*coarse_vertex_cos)[3])
|
||||
bool eval_refine_from_mesh(Subdiv *subdiv,
|
||||
const Mesh *mesh,
|
||||
const Span<float3> coarse_vert_positions)
|
||||
{
|
||||
#ifdef WITH_OPENSUBDIV
|
||||
if (subdiv->evaluator == nullptr) {
|
||||
@@ -237,12 +239,10 @@ bool eval_refine_from_mesh(Subdiv *subdiv, const Mesh *mesh, const float (*coars
|
||||
return false;
|
||||
}
|
||||
/* Set coordinates of base mesh vertices. */
|
||||
set_coarse_positions(
|
||||
subdiv,
|
||||
coarse_vertex_cos ?
|
||||
Span(reinterpret_cast<const float3 *>(coarse_vertex_cos), mesh->verts_num) :
|
||||
mesh->vert_positions(),
|
||||
mesh->verts_no_face());
|
||||
set_coarse_positions(subdiv,
|
||||
coarse_vert_positions.is_empty() ? mesh->vert_positions() :
|
||||
coarse_vert_positions,
|
||||
mesh->verts_no_face());
|
||||
|
||||
/* Set face-varying data to UV maps. */
|
||||
const int num_uv_layers = CustomData_number_of_layers(&mesh->corner_data, CD_PROP_FLOAT2);
|
||||
@@ -259,7 +259,7 @@ bool eval_refine_from_mesh(Subdiv *subdiv, const Mesh *mesh, const float (*coars
|
||||
stats_end(&subdiv->stats, SUBDIV_STATS_EVALUATOR_REFINE);
|
||||
return true;
|
||||
#else
|
||||
UNUSED_VARS(subdiv, mesh, coarse_vertex_cos);
|
||||
UNUSED_VARS(subdiv, mesh, coarse_vert_positions);
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -1162,7 +1162,7 @@ Mesh *subdiv_to_mesh(Subdiv *subdiv, const ToMeshSettings *settings, const Mesh
|
||||
stats_begin(&subdiv->stats, SUBDIV_STATS_SUBDIV_TO_MESH);
|
||||
/* Make sure evaluator is up to date with possible new topology, and that
|
||||
* it is refined for the new positions of coarse vertices. */
|
||||
if (!eval_begin_from_mesh(subdiv, coarse_mesh, nullptr, SUBDIV_EVALUATOR_TYPE_CPU, nullptr)) {
|
||||
if (!eval_begin_from_mesh(subdiv, coarse_mesh, {}, SUBDIV_EVALUATOR_TYPE_CPU, nullptr)) {
|
||||
/* This could happen in two situations:
|
||||
* - OpenSubdiv is disabled.
|
||||
* - Something totally bad happened, and OpenSubdiv rejected our
|
||||
|
||||
@@ -2137,7 +2137,7 @@ static bool draw_subdiv_create_requested_buffers(Object &ob,
|
||||
}
|
||||
|
||||
if (!bke::subdiv::eval_begin_from_mesh(
|
||||
subdiv, mesh_eval, nullptr, bke::subdiv::SUBDIV_EVALUATOR_TYPE_GPU, evaluator_cache))
|
||||
subdiv, mesh_eval, {}, bke::subdiv::SUBDIV_EVALUATOR_TYPE_GPU, evaluator_cache))
|
||||
{
|
||||
/* This could happen in two situations:
|
||||
* - OpenSubdiv is disabled.
|
||||
|
||||
@@ -939,9 +939,8 @@ static void refine_subdiv(Depsgraph *depsgraph,
|
||||
Array<float3> deformed_verts = BKE_multires_create_deformed_base_mesh_vert_coords(
|
||||
depsgraph, &object, ss.multires.modifier);
|
||||
|
||||
bke::subdiv::eval_refine_from_mesh(subdiv,
|
||||
static_cast<const Mesh *>(object.data),
|
||||
reinterpret_cast<float(*)[3]>(deformed_verts.data()));
|
||||
bke::subdiv::eval_refine_from_mesh(
|
||||
subdiv, static_cast<const Mesh *>(object.data), deformed_verts);
|
||||
}
|
||||
|
||||
static void restore_list(bContext *C, Depsgraph *depsgraph, StepData &step_data)
|
||||
|
||||
@@ -294,8 +294,7 @@ static void deform_matrices(ModifierData *md,
|
||||
return;
|
||||
}
|
||||
blender::bke::subdiv::displacement_attach_from_multires(subdiv, mesh, mmd);
|
||||
blender::bke::subdiv::deform_coarse_vertices(
|
||||
subdiv, mesh, reinterpret_cast<float(*)[3]>(positions.data()), positions.size());
|
||||
blender::bke::subdiv::deform_coarse_vertices(subdiv, mesh, positions);
|
||||
if (subdiv != runtime_data->subdiv) {
|
||||
blender::bke::subdiv::free(subdiv);
|
||||
}
|
||||
|
||||
@@ -297,8 +297,7 @@ static void deform_matrices(ModifierData *md,
|
||||
/* Happens on bad topology, but also on empty input mesh. */
|
||||
return;
|
||||
}
|
||||
blender::bke::subdiv::deform_coarse_vertices(
|
||||
subdiv, mesh, reinterpret_cast<float(*)[3]>(positions.data()), positions.size());
|
||||
blender::bke::subdiv::deform_coarse_vertices(subdiv, mesh, positions);
|
||||
if (!ELEM(subdiv, runtime_data->subdiv_cpu, runtime_data->subdiv_gpu)) {
|
||||
blender::bke::subdiv::free(subdiv);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user